SpringBoot整合Dubbo,并实现dubbo实现动态调用
在一些业务场景中,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实现动态调用的更多相关文章
- SpringBoot整合分布式ZooKeeper和Dubbo
ZooKeeper ZooKeeper是一个分布式的,开放远吗的分布式应用程序协调服务.它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护.域名服务.分布式同步.组服务等. 服务提供者 ...
- Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “看看星空,会觉得自己很渺小,可能我们在宇宙中从来就是一个偶然.所以,无论什么事情,仔细想一 ...
- SpringBoot 整合 Dubbo 进行分布式开发
自从Dubbo支持SpringBoot后,Dubbo与Spring的整合变得更加的简单了,下面就是完整的步骤: 1. 引入依赖 <dependency> <groupId>co ...
- springboot整合dubbo\zookeeper做注册中心
springboot整合dubbo发布服务,zookeeper做注册中心.前期的安装zookeeper以及启动zookeeper集群就不说了. dubbo-admin-2.5.4.war:dubbo服 ...
- SpringBoot整合dubbo
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成. 以上介绍来源于百度百科,具体dubbo相关可以自行查 ...
- SpringBoot 整合使用dubbo
这里主要是按照teaey作者的spring-boot-starter-dubbo框架进行一些变化的使用 依赖包: <dependency> <groupId>com.aliba ...
- 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...
- SpringBoot整合Zookeeper和Dubbo
一.Dubbo 1. Dubbo定义 Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来 ...
- spring-boot整合dubbo启动demo
参考资料: https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/ https://github.com/apach ...
随机推荐
- 一、Swagger配置
一.Swagger配置 1.注解不显示 SwaggerConfig文件下 //c.IncludeXmlComments(GetXmlCommentsPath()): 内下面添加: c.Inclu ...
- spoj 839-Optimal Marks
Description SPOJ.com - Problem OPTM Solution 容易发现各个位之间互不影响, 因此分开考虑每一位. 考虑题中是怎样的一个限制: 对每个点确定一个0/1的权值; ...
- Django_ORM_字段属性
Django_ORM_字段属性 常用字段 AutoField int自增列,必填参 primary_key=True 默认会自动创建一个列名为id的列 IntegerField 一个整数类型,范围在 ...
- postgreSQL学习(二):pgsql的一些基础操作
在上一篇文章中我们学习了怎么安装pgsql,安装好了后,我们来学习一下怎么对pgsql进行创建操作以及相关的crud的操作啦 一 创建数据库 $ createdb test 然后你可能会遇到如下的错误 ...
- Django分布式任务队列celery的实践
不使用数据库作为 Broker Broker 的选择大致有消息队列和数据库两种,这里建议尽量避免使用数据库作为 Broker,除非你的业务系统足够简单.在并发量很高的复杂系统中,大量 Workers ...
- response 输出中文数据 文件下载
使用OutputStream或者PrintWriter向客户端浏览器输出中文数据 package com.xc.response; import java.io.IOException; import ...
- 一张图认识Python(附基本语法总结)
一张图带你了解Python,更快入门, Python基础语法总结: 1.Python标识符 在 Python 里,标识符有字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以 ...
- python中 yield的用法和生成器generator的说明
详情: https://www.cnblogs.com/python-life/articles/4549996.html
- 腾讯云服务器tomcat端口无法访问
第一种情况: 如题:https://console.cloud.tencent.com/cvm/securitygroup 需要去这个地址设置安全组. 说实话,一句mmp不知当讲不当讲.使用说明这块太 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 I Lattice's basics in digital electronics(模拟)
https://nanti.jisuanke.com/t/31450 题意 给出一个映射(左为ascll值),然后给出一个16进制的数,要求先将16进制转化为2进制然后每9位判断,若前8位有奇数个1且 ...