一、接口定义
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{@Overridepublic 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{@Overridepublic 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"));}
}