当前位置: 首页>编程语言>正文

dubbo复习:(13)把服务划分为不同的group 和version,只有服务端和客户端group和version匹配才能通信

一、接口定义

package cn.edu.tju.service;

public interface DevelopService {
    String invoke(String param);
}

二、两个版本的实现:

package cn.edu.tju.service;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService(group = "group1",version = "1.0")
public class DevelopProviderServiceV1 implements DevelopService{
    @Override
    public String invoke(String param) {
        StringBuilder s = new StringBuilder();
        s.append("ServiceV1 param:").append(param);
        return s.toString();
    }
}
package cn.edu.tju.service;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService(group = "group2",version = "2.0")
public class DevelopProviderServiceV2 implements DevelopService{
    @Override
    public String invoke(String param) {
        StringBuilder s = new StringBuilder();
        s.append("ServiceV2 param:").append(param);
        return s.toString();
    }
}

通过@DubboService注解指定了group和version
三、客户端分别调用不同的group&版本

package cn.edu.tju.service;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


@Component
public class MyConsumer implements CommandLineRunner {
    @DubboReference(group = "group1",version = "1.0")
    private DevelopService developServiceV1;

    @DubboReference(group = "group2",version = "2.0")
    private DevelopService developServiceV2;


    public void run(String ...args){
        //调用DevelopService的 group1 1.0版本
        System.out.println("Dubbo Remote Return ======> " + developServiceV1.invoke("1"));
        //调用DevelopService的 group2 2.0版本
        System.out.println("Dubbo Remote Return ======> " + developServiceV2.invoke("2"));

    }
}



https://www.xamrdz.com/lan/5te1963857.html

相关文章: