今年年初时,阿里巴巴开源的高性能服务框架
dubbo
又开始了新一轮的更新,还加入了Apache孵化器
。原先项目使用了spring cloud
以后,已经比较少用dubbo
。目前又抽调回原来的行业应用部门,可能还会使用dubbo
进行服务调用。趁着编写教材的机会来进行学习下。并且目前Dubbo
也出了springboot
的starter
项目了,借着SpringBoot
的东风,集成起来很方便,基本上就一个依赖包引入的问题了。废话很少说,开始吧~html
对于没有接触过Dubbo
的同窗,能够先了解下相关知识。java
Dubbo 是阿里巴巴公司一个开源的高性能服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,以及 SOA 服务治理方案,使得应用可经过高性能 RPC 实现服务的输出、输入功能和 Spring 框架无缝集成。Dubbo 包含远程通信、集群容错和自动发现三个核心部分。git
它提供透明化的远程方法调用,实现像调用本地方法同样调用远程方法,只需简单配置,没有任何 API 侵入。同时它具有软负载均衡及容错机制,可在内网替代 F5 等硬件负载均衡器,下降成本,减小单点。它能够实现服务自动注册与发现,再也不须要写死服务提供方地址,注册中心基于接口名查询服务提供者的 IP 地址,而且可以平滑添加或删除服务提供者。github
2011 年底,阿里巴巴在 GitHub 上开源了基于 Java 的分布式服务治理框架 Dubbo,以后它成为了国内该类开源项目的佼佼者,许多开发者对其表示青睐。同时,前后有很多公司在实践中基于 Dubbo 进行分布式系统架构。目前在 GitHub 上,它的 fork、star 数均已破万。web
Dubbo核心功能:redis
Dubbo架构算法
部署阶段spring
运行阶段数据库
调用关系说明apache
Dubbo 架构具备如下几个特色,分别是连通性、健壮性、伸缩性、以及向将来架构的升级性。
当服务集群规模进一步扩大,带动IT治理结构进一步升级,须要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力。下图是将来可能的一种架构:
节点角色说明
节点 | 角色说明 |
---|---|
Deployer |
自动部署服务的本地代理 |
Repository |
仓库用于存储服务应用发布包 |
Scheduler |
调度中心基于访问压力自动增减服务提供者 |
Admin |
统一管理控制台 |
Registry |
服务注册与发现的注册中心 |
Monitor |
统计服务的调用次数和调用时间的监控中心 |
你们可访问官网文档:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html,里面有详细说明和使用说明的。这里就再也不阐述了。
基于官方的
incubator-dubbo-spring-boot-project
项目,在SpringBoot
中集成起来很简单。
注意:因为本系列仍是使用1.5.x
版本进行讲解,因此使用的版本为0.1.x
。若使用SpringBoot2.x
的同窗,可使用0.2.x
版本。
这里为了方便,直接建立了一个接口工程,spring-boot-dubbo-api
。
IHelloService.java
/** * 定义一个接口 * @author oKong * */ public interface IHelloService { String hello(String name); }
建立一个spring-boot-dubbo-provider
工程。 0.引入pom依赖。
<!-- 引入api --> <dependency> <groupId>cn.lqdev.learning</groupId> <artifactId>spring-boot-dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 引入dubbo依赖 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.1</version> </dependency> <!-- 引入redis做为注册中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
注意:这里直接选用了redis
做为注册中心使用。默认是zookeeper
。
1.编写接口实现类。 HelloServiceImpl.java
/** * 定义一个服务实现类 * @author oKong * */ // 这里注意 此类@service是dubbo的 @Service( version = "${demo.service.version}", //版本 application = "${dubbo.application.id}", //应用ID protocol = "${dubbo.protocol.id}", //协议id registry = "${dubbo.registry.id}")//注册中心id @Slf4j public class HelloServiceImpl implements IHelloService { @Override public String hello(String name) { log.info("dubbo提供者,参数name:{}", name); return "hello " + name + ",this is a dubbo provider!"; } }
说明下:这里的@Service
是包路径com.alibaba.dubbo.config.annotation.Service
下的注解类,其指定了接口版本、协议id、注册中心id等基本信息。这里注意仍是版本号有用,由于会一个接口多版本共存问题,因此通常上都会设置版本信息的。 2.设置配置文件信息,添加dubbo
相关信息,好比注册中心类型,地址等。
# 应用名称 便于识别 dubbo.application.id=spring-boot-dubbo-provider dubbo.application.name=spring-boot-dubbo-provider server.port=8686 # 设置版本 demo.service.version=1.0.0 #协议 可选dubbo redis、http、thrift等 dubbo.protocol.id=dubbo dubbo.protocol.name=dubbo dubbo.protocol.port=20880 #设置扫描路径 被注解@service和@Reference 等 dubbo.scan.basePackages=cn.lqdev.learning.springboot.dubbo.provider.service # 注册中心配置 dubbo.registry.id=okong-registry #注册中心类型 这里使用redis做为注册中心 # zookeeper://127.0.0.1:2181 dubbo.registry.address=redis://127.0.0.1:6379 # 设置用户名密码 如有的话 #dubbo.registry.username=oKong #dubbo.registry.password=oKong # 设置redis参数 # 链接池中的最大空闲链接 dubbo.registry.parameters.max.idle=8 # 链接池最大链接数(使用负值表示没有限制) dubbo.registry.parameters.max-active=8 # 链接池最大阻塞等待时间(使用负值表示没有限制) dubbo.registry.parameters.max-wait=-1 # 链接池中的最大空闲链接 dubbo.registry.parameters.max-idle=8 # 链接池中的最小空闲链接 dubbo.registry.parameters.min-idle=0
注意:这里为了方便,直接使用了Redis
做为了注册中心。对于redis
链接相关配置参数,能够经过dubbo.registry.parameters.xxx
的形式来进行设置,因为parameters
是个Map
对象,因此添加的key是不会进行大小写转换的,填写了什么就是什么。具体的registry
配置对象,能够查看com.alibaba.dubbo.config.RegistryConfig
类。而对于redis
相关参数配置,能够查看com.alibaba.dubbo.registry.redis.RedisRegistry
类。
其余的注册中心,也是相似的,你们可在包com.alibaba.dubbo.registry
找到都要的注册中心配置类。
3.启动类编写。
DubboProviderApplication.java
/** * dubbo-提供者 * @author oKong * */ @SpringBootApplication @Slf4j public class DubboProviderApplication { public static void main(String[] args) throws Exception { //因为dubbo提供者只是单纯提供服务的 能够为一个非web环境 new SpringApplicationBuilder(DubboProviderApplication.class).web(false).run(args); log.info("spring-boot-dubbo-provider启动!"); } }
4.启动应用,能够访问下redis
服务,能够看见已经有服务列表信息了。
建立spring-boot-dubbo-consumer
工程。 0.引入pom依赖
<!-- 引入api --> <dependency> <groupId>cn.lqdev.learning</groupId> <artifactId>spring-boot-dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 引入dubbo依赖 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.1</version> </dependency> <!-- 引入redis做为注册中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
1.配置文件添加注册中心及服务版本相关信息
# 应用名称 便于识别 dubbo.application.id=spring-boot-dubbo-consumer dubbo.application.name=spring-boot-dubbo-consumer server.port=9696 #设置扫描路径 被注解@service和@Reference 等 dubbo.scan.basePackages=cn.lqdev.learning.springboot.dubbo.consumer # 注册中心配置 dubbo.registry.id=okong-registry #注册中心类型 这里使用redis做为注册中心 # zookeeper://127.0.0.1:2181 dubbo.registry.address=redis://127.0.0.1:6379 # 设置用户名密码 如有的话 #dubbo.registry.username=oKong #dubbo.registry.password=oKong # 设置redis参数 # 链接池中的最大空闲链接 dubbo.registry.parameters.max.idle=8 # 链接池最大链接数(使用负值表示没有限制) dubbo.registry.parameters.max-active=8 # 链接池最大阻塞等待时间(使用负值表示没有限制) dubbo.registry.parameters.max-wait=-1 # 链接池中的最大空闲链接 dubbo.registry.parameters.max-idle=8 # 链接池中的最小空闲链接 dubbo.registry.parameters.min-idle=0
2.启动类编写
DubboConsumerApplication.java
/** * dubbo-消费者实例 * @author oKong * */ @SpringBootApplication @Slf4j public class DubboConsumerApplication { public static void main(String[] args) throws Exception { SpringApplication.run(DubboConsumerApplication.class, args); log.info("spring-boot-dubbo-consumer启动!"); } }
3.编写一个rest
api接口服务,进行服务调用。
/** * 调用实例 * @author oKong * */ @RestController @Slf4j public class DemoController { /** * 申明为一个reference,其实就是设置一个bean类了, * 将原来xml配置替换成注解而已 * <dubbo:reference id=“xxxService” interface=“com.xxx.XxxService” /> */ @Reference(version = "1.0.0") IHelloService helloService; @GetMapping("/hello") public String hello(String name) { log.info("调用提供者服务,参数name:{}", name); return helloService.hello(name); } }
4.启动应用,访问:http://127.0.0.1:9696/hello?name=oKong ,能够看见服务调用成功了。
官方监控默认支持了zookeeper。并且官方文档也说了,对于
redis
桥接实现只为开源版本提供,其可靠性依赖于 Redis 自己的可靠性。建议你们仍是使用zookeeper
吧,redis
仍是做为缓存使用吧。
监控台地址:https://github.com/apache/incubator-dubbo-ops 你们可自行安装说明进行编译运行下。
新的监控界面:
加入了Apache孵化器
后,界面都是英文的了。。。仍是原来的看的舒服呀!
本章节主要介绍了
dubbo
的集成和简单的使用。具体其余的使用其实和原先是同样的,并无什么区别。建议你们仍是去看看官方文档,目前改版后内容丰富多了,干货不少,建议仍是去看看。这下终因而中文版的了,看的不头疼了,⊙﹏⊙‖∣
目前互联网上不少大佬都有
SpringBoot
系列教程,若有雷同,请多多包涵了。原创不易,码字不易,还但愿你们多多支持。若文中有所错误之处,还望提出,谢谢。
499452441
lqdevOps
我的博客:http://blog.lqdev.cn
完整示例:https://github.com/xie19900123/spring-boot-learning/tree/master/chapter-29
原文地址:http://blog.lqdev.cn/2018/09/28/springboot/chapter-twenty-nine/