原创作者:田超凡(程序员田宝宝)
版权所有,转载请注明原作者,严禁复制转载
Dubbo是一个面向服务设计的SOA框架,提供了高性能、透明化的RPC远程服务调用的能力、出色的SOA服务治理能力,可以支撑起上千个服务每天超过几十亿次的访问,被广泛应用于阿里巴巴及其成员子公司和外界公司的面向服务设计的项目中。
Dubbo有三个核心组成部分:服务提供方、服务消费方、注册中心
服务提供方:负责提供服务,将服务发布到注册中心中
服务消费方:负责从注册中心中获取需要的服务进行消费
注册中心:负责服务注册和发现、服务治理。
对于服务提供方:负责发布服务,随着项目规模的日益扩大,需要发布的服务数量、类型也会不断膨胀,降低了服务发布的执行效率。
对于服务消费方:他只关心如何获取需要的服务,随着项目规模的日益扩大,需要从注册中心获取的服务数量也会不断增多,降低了服务调用的执行效率。
还有一种情况,就是在使用dubbo的项目中,很多服务会同时兼具两种角色,即既是服务提供方,也是服务消费方,既需要发布服务到注册中心,也需要从注册中心获取需要的服务进行消费。
综上所述,只有对服务进行统一管理,才能有效的优化内部应用的服务发布和订阅的流程和管理,注册中心可以通过特定协议实现服务对外的统一。
Multicast注册中心
Zookeeper注册中心
Redis注册中心
Simple注册中心
优点:
1 提供了高性能、透明化的RPC远程服务调用能力
在Dubbo中,可以像调用本地方法一样进行RPC远程服务调用,真正实现了API零侵入。
2 出色的SOA服务治理能力
在Dubbo中,基于内置的dubbo-admin和dubbo-monitor模块,实现了较为完整的接口管理和监控服务,对于同一个应用中的接口,可以实现多类型、多版本、多注册中心的管理。
3 软负载均衡和容错机制
缺点:语言局限性,只支持Java语言。
dubbo支持dubbo、rmi、http、webservice等多种协议,但是官方只推荐我们使用dubbo协议。
相同点:SpringCloud和dubbo都是面向服务设计的框架,都提供了RPC远程服务调用能力,以及服务注册和发现、服务治理的能力。
不同点:
总结:在使用dubbo的项目中,如果需要使用其他的微服务解决方案,则需要自行寻找组件并自行组装,可能还会存在兼容性问题。但是如果使用SpringCloud,因为它内部已经实现了常见的很多微服务解决方案,是一个较为完善的微服务全家桶,容易拆箱即用。
环境步骤:
Dubbo注册中心采用的是Zookeeper。为什么采用Zookeeper呢?
Zookeeper是一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据,能很好的作为Dubbo服务的注册中心。
Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求
项目结构:
主要分三大模块:
Ittcf-dubbo-api : 存放公共接口;
Ittcf-dubbo-consumer : 调用远程服务;
Ittcf-dubbo-provider : 提供远程服务。
| public interface DemoApiService { public String getUser(Long userId); } |
| public class DemoApiServiceImpl implements DemoApiService { public String getUser(Long userId) { System.out.println("生产者调用消费者服务接口userId:" + userId); return "tianbaobao"; } } |
| <dependencies> <dependency> <groupId>com.ittcfgroupId> <artifactId>ittcf-dubbo-apiartifactId> <version>0.0.1-SNAPSHOTversion> dependency> <dependency> <groupId>com.101tecgroupId> <artifactId>zkclientartifactId> <version>0.10version> dependency> <dependency> dependency> <dependency> <groupId>org.jboss.nettygroupId> <artifactId>nettyartifactId> <version>3.2.5.Finalversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>4.3.9.RELEASEversion> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>dubboartifactId> <version>2.5.3version> <exclusions> <exclusion> <groupId>org.springframeworkgroupId> <artifactId>springartifactId> exclusion> <exclusion> <groupId>org.jboss.nettygroupId> <artifactId>nettyartifactId> exclusion> exclusions> dependency> dependencies> |
| xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demotest-provider" />
<dubbo:registry address="zookeeper://localhost:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.ittcf.demo.api.service.DemoApiService" ref="demoService" protocol="dubbo" />
<bean id="demoService" class="com.ittcf.demo.api.service.impl.DemoApiServiceImpl" /> beans> |
| ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); System.out.println(context.getDisplayName() + ": here"); context.start(); System.out.println("服务已经启动..."); System.in.read(); |
| <dependencies> <dependency> <groupId>com.ittcfgroupId> <artifactId>ittcf-dubbo-apiartifactId> <version>0.0.1-SNAPSHOTversion> dependency> <dependency> <groupId>com.101tecgroupId> <artifactId>zkclientartifactId> <version>0.10version> dependency> <dependency> <groupId>commons-logginggroupId> <artifactId>commons-loggingartifactId> <version>1.2version> dependency> <dependency> <groupId>org.jboss.nettygroupId> <artifactId>nettyartifactId> <version>3.2.5.Finalversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>4.3.9.RELEASEversion> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>dubboartifactId> <version>2.5.3version> <exclusions> <exclusion> <groupId>org.springframeworkgroupId> <artifactId>springartifactId> exclusion> <exclusion> <groupId>org.jboss.nettygroupId> <artifactId>nettyartifactId> exclusion> exclusions> dependency> dependencies> |
| ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); context.start(); System.out.println("consumer start"); DemoApiService demoApiService = context.getBean(DemoApiService.class); String result = demoApiService.getUser(1l); System.out.println("result:" + result); |
本文部分素材转载自蚂蚁课堂