以前,咱们讨论过Nginx+tomcat组成的集群,这已是很是灵活的集群技术,可是当咱们的系统遇到更大的瓶颈,所有应用的单点服务器已经不能知足咱们的需求,这时,咱们要考虑另一种,咱们熟悉的内容,就是分布式,而当下流行的Dubbo框架,不容咱们忽视,这里,我们一块儿来探讨一下这个框架的使用。html
之前咱们须要远程调用他人的接口,咱们是这么作的:linux
咱们遇到的问题:git
(1) 当服务愈来愈多时,服务URL配置管理变得很是困难,F5硬件负载均衡器的单点压力也愈来愈大。
此时须要一个服务注册中心,动态的注册和发现服务,使服务的位置透明。
并经过在消费方获取服务提供方地址列表,实现软负载均衡和Failover,下降对F5硬件负载均衡器的依赖,也能减小部分红本。
(2) 当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪一个应用要在哪一个应用以前启动,架构师都不能完整的描述应用的架构关系。
这时,须要自动画出应用间的依赖关系图,以帮助架构师理清理关系。
(3) 接着,服务的调用量愈来愈大,服务的容量问题就暴露出来,这个服务须要多少机器支撑?何时该加机器?
为了解决这些问题,第一步,要将服务如今天天的调用量,响应时间,都统计出来,做为容量规划的参考指标。
其次,要能够动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程当中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容量。 github
为解决这些问题,Dubbo为咱们作了什么呢:web
负载均衡:算法
这就是所谓的软负载均衡!spring
如今让咱们一块儿来接触下这个优秀的框架:数据库
架构如图:apache
节点角色说明:windows
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:
0. 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册本身提供的服务。
2. 服务消费者在启动时,向注册中心订阅本身所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,若是有变动,注册中心将基于长链接推送变动数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,若是调用失败,再选另外一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Dubbo 提供了不少协议, Dubbo 协 议、RMI 协议 、 Hessian 协议, 咱们查看 Dubbo 源代码,有各类协议的实现,如图所示:
咱们以前没用 Dubbo 以前时,大部分都使用 Hessian来使用咱们服务的暴露和调用,利用 HessianProxyFactory 调用远程接口。
上面是参考了 Dubbo 官方网介绍,接下来咱们来介绍 SpringMVC 、 Dubbo 、 Zookeeper 整合使用。
Zookeeper 做为 Dubbo 服务的注册中心, Dubbo 原先基于数据库的注册中心,没采用 Zookeeper , Zookeeper 一个分布式的服务框架,是树型的目录服务 的数据存储,能作到集群管理数据 ,这里能很好的做为 Dubbo 服务的注册中心, Dubbo 能与 Zookeeper 作到集群部署,当提供者出现断电等异常停机时, Zookeeper 注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。咱们先在 linux 上安装 Zookeeper ,咱们安装最简单的单点,集群比较麻烦。
先须要安装JdK,从Oracle的Java网站下载,安装很简单,就再也不详述。
单机模式
单机安装很是简单,只要获取到 Zookeeper 的压缩包并解压到某个目录如:C:\zookeeper-3.4.5\下,Zookeeper 的启动脚本在 bin 目录下,Windows 下的启动脚本是 zkServer.cmd。
在你执行启动脚本以前,还有几个基本的配置项须要配置一下,Zookeeper 的配置文件在 conf 目录下,这个目录下有 zoo_sample.cfg 和 log4j.properties,你须要作的就是将 zoo_sample.cfg 更名为 zoo.cfg,由于 Zookeeper 在启动时会找这个文件做为默认配置文件。下面详细介绍一下,这个配置文件中各个配置项的意义。
1 <span style="font-size:18px;"># The number of milliseconds of each tick 2 tickTime=2000 3 # The number of ticks that the initial 4 # synchronization phase can take 5 initLimit=10 6 # The number of ticks that can pass between 7 # sending a request and getting an acknowledgement 8 syncLimit=5 9 # the directory where the snapshot is stored. 10 # do not use /tmp for storage, /tmp here is just 11 # example sakes. 12 dataDir=C:\\zookeeper-3.4.5\\data 13 dataLogDir=C:\\zookeeper-3.4.5\\log 14 # the port at which the clients will connect 15 clientPort=2181 16 # 17 # Be sure to read the maintenance section of the 18 # administrator guide before turning on autopurge. 19 # 20 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance 21 # 22 # The number of snapshots to retain in dataDir 23 #autopurge.snapRetainCount=3 24 # Purge task interval in hours 25 # Set to "0" to disable auto purge feature 26 #autopurge.purgeInterval=1</span>
当这些配置项配置好后,你如今就能够启动 Zookeeper 了,启动后要检查 Zookeeper 是否已经在服务,能够经过 netstat – ano 命令查看是否有你配置的 clientPort 端口号在监听服务。
(1)下载 dubbo-admin-2.4.1.war 包,在 windows 的 tomcat 部署,先把 dubbo-admin-2.4.1 放在 tomcat 的 webapps/ROOT 下,而后进行解压
(2)而后到 webapps/ROOT/WEB-INF 下,有一个 dubbo.properties 文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心,如图所示:
1 <span style="font-size:18px;">dubbo.registry.address=zookeeper://127.0.0.1:2181 2 dubbo.admin.root.password=root 3 dubbo.admin.guest.password=guest</span>
(3)而后启动 tomcat 服务,用户名和密码: root,并访问服务,显示登录页面,说明dubbo-admin部署成功, 如图所示:
第一: 咱们先开发服务注册的,就是提供服务,项目结构如图所示:
(1)test-maven-api项目加入了一个服务接口,代码以下:
1 public interface TestRegistryService { 2 public String hello(String name); 3 }
(2)test-maven-console在pom .xml 加入 Dubbo 和Zookeeper的 jar 包、引用 test-maven-api 的 jar 包,代码以下:
1 <span style="font-size:18px;"><dependency> 2 <groupId>cn.test</groupId> 3 <artifactId>test-maven-api</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 </dependency> 6 <dependency> 7 <groupId>com.alibaba</groupId> 8 <artifactId>dubbo</artifactId> 9 <version>2.5.3</version> 10 </dependency> 11 <dependency> 12 <groupId>org.apache.zookeeper</groupId> 13 <artifactId>zookeeper</artifactId> 14 <version>3.4.6</version> 15 </dependency> 16 <dependency> 17 <groupId>com.github.sgroschupf</groupId> 18 <artifactId>zkclient</artifactId> 19 <version>0.1</version> 20 </dependency></span>
(3)test-maven-console实现具体的服务,代码以下:
1 @Service("testRegistryService") 2 public class TestRegistryServiceImpl implements TestRegistryService { 3 public String hello(String name) { 4 return "hello"+name; 5 } 6 }
(4)咱们服务以及实现好了,这时要暴露服务,代码以下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 <span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span> 7 xmlns:context="http://www.springframework.org/schema/context" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 10 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 11 <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" 13 default-lazy-init="false" > 14 <!-- 提供方应用名称信息,这个至关于起一个名字,咱们dubbo管理页面比较清晰是哪一个应用暴露出来的 --> 15 <dubbo:application name="dubbo_provider"></dubbo:application> 16 <!-- 使用zookeeper注册中心暴露服务地址 --> 17 <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry> 18 <!-- 要暴露的服务接口 --> 19 <dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" /> 20 </beans>
说明:
dubbo:registry 标签一些属性的说明:
1 ) register 是否向此注册中心注册服务,若是设为 false ,将只订阅,不注册 。
2 ) check 注册中心不存在时,是否报错。
3 ) subscribe 是否向此注册中心订阅服务,若是设为 false ,将只注册,不订阅 。
4 ) timeout 注册中心请求超时时间 ( 毫秒 ) 。
5 ) address 能够 Zookeeper 集群配置 ,地址能够多个以逗号隔开等。
dubbo:service 标签的一些属性说明:
1 ) interface服务接口的路径
2 ) ref引用对应的实现类的 Bean 的 ID
3 ) registry向指定注册中心注册,在多个注册中心时使用,值为 <dubbo:registry> 的 id 属性,多个注册中心 ID 用逗号分隔,若是不想将该服务注册到任何 registry ,可将值设为 N/A
4 ) register 默认true ,该协议的服务是否注册到注册中心。
(5)启动项目,而后咱们在 Dubbo 管理页面上显示,已经暴露的服务,但显示尚未消费者,由于咱们还没实现消费者服务,如图所示:
第二:咱们在开发服务消费者,就是调用服务,咱们在新建一个新的消费者项目:
(1)test-maven-server-console的 pom.xml 引入 Dubbo 和Zookeeper的 jar 包、 test-maven-api 的 jar 包,由于引入 test-maven-api 的 jar 包,咱们在项目中调用像在本地调用同样。代码以下:
1 <span style="font-size:18px;"><dependency> 2 <groupId>cn.test</groupId> 3 <artifactId>test-maven-api</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 </dependency> 6 <dependency> 7 <groupId>com.alibaba</groupId> 8 <artifactId>dubbo</artifactId> 9 <version>2.5.3</version> 10 </dependency> 11 <dependency> 12 <groupId>org.apache.zookeeper</groupId> 13 <artifactId>zookeeper</artifactId> 14 <version>3.4.6</version> 15 </dependency> 16 <dependency> 17 <groupId>com.github.sgroschupf</groupId> 18 <artifactId>zkclient</artifactId> 19 <version>0.1</version> 20 </dependency></span>
(2)test-maven-server-console项目的具体实现,代码以下:
1 @Controller 2 public class IndexController { 3 4 @Autowired 5 private TestRegistryService testRegistryService; 6 7 @RequestMapping("/hello") 8 public String index(Model model){ 9 String name=testRegistryService.hello("zz"); 10 System.out.println("xx=="+name); 11 return ""; 12 } 13 14 }
(3)咱们要引用的地址,代码以下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 <span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span> 7 xmlns:context="http://www.springframework.org/schema/context" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 10 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 11 <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" 13 default-lazy-init="false" > 14 15 <dubbo:application name="dubbo_consumer"></dubbo:application> 16 <!-- 使用zookeeper注册中心暴露服务地址 --> 17 <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry> 18 <!-- 要引用的服务 --> 19 <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference> 20 </beans>
说明:
dubbo:reference 的一些属性的说明:
1 ) interface调用的服务接口
2 ) check 启动时检查提供者是否存在, true 报错, false 忽略
3 ) registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为 <dubbo:registry> 的 id 属性,多个注册中心 ID 用逗号分隔
4 ) loadbalance 负载均衡策略,可选值: random,roundrobin,leastactive ,分别表示:随机,轮循,最少活跃调用
(4)项目启动, Dubbo 管理页面,能看到消费者,如图所示:
(5)而后访问消费者项目, Controller 层能像调用本地同样调用服务的具体实现,如图所示:
通过一系列的操做以后,咱们感受,的确很简单,dubbo给咱们封装了不少操做,让咱们不须要过多考虑具体的实现细节,配置化生成我们的应用,这样的思想,如今在IT行业正在盛行!