在一些業務場景中,CP定單提交過來,需要提交到不同的通道進行業務處理
本文通過Dubbo以定義一個interface,各個通道方來實現這個接口。通過group來區分不同的通道
有需要的同學可以下載 示例代碼
項目結構如下:interface,provider,consumer
1.創建接口
public interface HelloService { String sayHello(String name); //支付接口 PayResult pay(PayInfo pay); }
2.創建provider
pom中添加依賴
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.1-SNAPSHOT</version> </dependency> <!-- Dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.5</version> </dependency> <!-- Spring Context Extras --> <dependency> <groupId>com.alibaba.spring</groupId> <artifactId>spring-context-support</artifactId> <version>1.0.2</version> </dependency>
編寫接口兩個實現
@Service( version = "1.0.0", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}", group = "pay1" ) public class DefaultHello1Service implements HelloService { @Override public String sayHello(String name) { return name+"aaaaaaas"; } @Override public PayResult pay(PayInfo pay) { PayResult result=new PayResult(); result.setResult(true); result.setMsg("from pay1"); return result; } }
添加Dubbo配置項
dubbo.scan.basePackages = com.glory.study.provider1.service # Dubbo Config properties ## ApplicationConfig Bean dubbo.application.id = dubbo-provider-demo dubbo.application.name = dubbo-provider-demo ## ProtocolConfig Bean dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 30102 ## RegistryConfig Bean dubbo.registry.id = my-registry dubbo.registry.address = zookeeper://127.0.0.1:2181
啟動,成功,通過dubboadmin可以看到剛才的兩個服務分別屬於pay1和pay2兩個分組
3.服務好了,下面消費都通過ReferenceConfig動態調用
public class PayUtils { private static ApplicationConfig application = new ApplicationConfig(); private static Map<String, RegistryConfig> registryConfigCache = new ConcurrentHashMap<>(); private static Map<String, ReferenceConfig<HelloService>> referenceCache = new ConcurrentHashMap<>(); static { application.setName("test"); } private static RegistryConfig getRegistryConfig(String address,String group, String version) { String key = address + "-" + group + "-" + version; RegistryConfig registryConfig = registryConfigCache.get(key); if (null == registryConfig) { registryConfig = new RegistryConfig(); registryConfig.setAddress(address); registryConfigCache.put(key, registryConfig); } return registryConfig; } /** * 獲取服務的代理對象 * */ private static ReferenceConfig<HelloService> getReferenceConfig( String group,String address, String version) { String referenceKey = group; ReferenceConfig<HelloService> referenceConfig = referenceCache.get(referenceKey); if (null == referenceConfig) { referenceConfig = new ReferenceConfig<>(); referenceConfig.setApplication(application); referenceConfig.setRegistry(getRegistryConfig(address, group, version)); referenceConfig.setInterface(HelloService.class); referenceConfig.setVersion(version); referenceConfig.setGroup(group); referenceCache.put(referenceKey, referenceConfig); } return referenceConfig; } /** * 調用遠程服務 * */ public static PayResult invoke(PayInfo dto) { String group=dto.getType(); String add=dto.getAddress(); String version=dto.getVersion(); ReferenceConfig<HelloService> reference = getReferenceConfig(group, add, version); if (null != reference) { HelloService helloService = reference.get(); if (null != helloService) { return helloService.pay(dto); } } return null; } }
頁面調用
@RequestMapping("/dubbotest1") public PayResult dubbotest1() { PayInfo p=new PayInfo(); p.setAddress(address); p.setVersion(version); p.setType("pay1"); return PayUtils.invoke(p); } @RequestMapping("/dubbotest2") public PayResult dubbotest2() { PayInfo p=new PayInfo(); p.setAddress(address); p.setVersion(version); p.setType("pay2"); return PayUtils.invoke(p); }
啟動服務成功,我們看到了我們的消費者
不同的參數訪問不同的接口