在一些业务场景中,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);
}

启动服务成功,我们看到了我们的消费者

不同的参数访问不同的接口

SpringBoot整合Dubbo,并实现dubbo实现动态调用的更多相关文章

  1. SpringBoot整合分布式ZooKeeper和Dubbo

    ZooKeeper ZooKeeper是一个分布式的,开放远吗的分布式应用程序协调服务.它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护.域名服务.分布式同步.组服务等. 服务提供者 ...

  2. Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!   “看看星空,会觉得自己很渺小,可能我们在宇宙中从来就是一个偶然.所以,无论什么事情,仔细想一 ...

  3. SpringBoot 整合 Dubbo 进行分布式开发

    自从Dubbo支持SpringBoot后,Dubbo与Spring的整合变得更加的简单了,下面就是完整的步骤: 1. 引入依赖 <dependency> <groupId>co ...

  4. springboot整合dubbo\zookeeper做注册中心

    springboot整合dubbo发布服务,zookeeper做注册中心.前期的安装zookeeper以及启动zookeeper集群就不说了. dubbo-admin-2.5.4.war:dubbo服 ...

  5. SpringBoot整合dubbo

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成. 以上介绍来源于百度百科,具体dubbo相关可以自行查 ...

  6. SpringBoot 整合使用dubbo

    这里主要是按照teaey作者的spring-boot-starter-dubbo框架进行一些变化的使用 依赖包: <dependency> <groupId>com.aliba ...

  7. 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)

    http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...

  8. SpringBoot整合Zookeeper和Dubbo

    一.Dubbo 1. Dubbo定义 Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来 ...

  9. spring-boot整合dubbo启动demo

    参考资料: https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/ https://github.com/apach ...

随机推荐

  1. [SimplePlayer] 5. 向音频设备输出音频

    两种SDL音频输出方式 我们这里采用SDL来进行音频输出.SDL提供两种音频输出的方式: 如果在SDL_OpenAudio时不指定callback,那么可以调用SDL_QueueAudio主动地向音频 ...

  2. Linux之文本编辑器Vim

    一.什么是vim vi是一种模式编辑器.vi 是Unix世界里极为普遍的全屏幕文本编辑器,几乎可以说任何一台Unix机器都会提供这套软体,其他的文本编辑器则不一定会存在,但是目前我们使用比较多的是 v ...

  3. StringUtils常用方法+StringUtils详细介绍

    StringUtils常用方法+StringUtils详细介绍   StringUtils用法+StringUtils详细介绍博文来源:http://yijianfengvip.blog.163.co ...

  4. 2.2 collection 模块

    2.2.1 定义命名元祖 2.2.2 定义双端队列 2.2.3 定义有序的字典 2.2.4 定义有默认值的字典

  5. python学习日记(OOP——类的内置方法)

    __str__和__repr__ 改变对象的字符串显示__str__,__repr__ 我们先定义一个Student类,打印一个实例: class Student(object): def __ini ...

  6. wiki

    GRANT ALL PRIVILEGES ON confluence.* TO 'confluence'@'localhost' IDENTIFIED BY '%SaRK%TDpU#CyT6i';

  7. <二>企业级开源仓库nexus3实战应用–使用nexus3配置docker私有仓库

    1,安装nexus3. 这个地方略了,安装部署可以参考:nexus3安装配置. 2,配置走起. 1,创建blob存储. 登陆之后,先创建一个用于存储镜像的空间. 定义一个name,下边的内容会自动补全 ...

  8. 第一节: Timer的定时任务的复习、Quartz.Net的入门使用、Aop思想的体现

    一. 前奏-Timer类实现定时任务 在没有引入第三方开源的定时调度框架之前,我们处理一些简单的定时任务同时都是使用Timer类, DotNet中的Timer类有三个,分别位于不同的命名空间下,分别是 ...

  9. [物理学与PDEs]第5章习题3 第二 Piola 应力张量的对称性

    试证明: 在物质描述下, 动量矩守恒定律等价于第二 Piola 应力张量的对称性. 证明: 由 $$\beex \bea \int_{G_t}\rho\sex{{\bf y}\times\cfrac{ ...

  10. [物理学与PDEs]第2章第1节 理想流体力学方程组 1.3 理想流体力学方程组的数学结构

    1.  局部音速 $c$: $c^2=\cfrac{\p p}{\p \rho}>0$. 2.  将理想流体力学方程组 $$\beex \bea \rho\cfrac{\p {\bf u}}{\ ...