dubbo+zookeeper 分布式项目搭建

一、简介

1.1 dubbo介绍

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和  Spring框架无缝集成。

1.2主要核心部件

Remoting: 网络通信框架,实现了 sync-over-async 和 request-response 消息机制.
RPC: 一个 远程过程调用的抽象,支持 负载均衡容灾集群功能
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅

1.3 工作原理


Provider
暴露服务方称之为“服务提供者”。
Consumer
调用 远程服务方称之为“服务消费者”。
Registry
服务注册与发现的中心目录服务称之为“服务注册中心”。
Monitor
统计服务的调用次数和调用时间的日志服务称之为“服务监控中心”。
(1) 连通性:
注册中心负责服务地址的注册与查找,相当于 目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小
监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以报表展示
服务提供者向注册中心注册其提供的服务,并汇报调用时间到监控中心,此时间不包含网络开销
服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,同时汇报调用时间到监控中心,此时间包含网络开销
注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外
注册中心通过 长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者
注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在 本地缓存了提供者列表
注册中心和监控中心都是可选的,服务消费者可以直连服务提供者
(2) 健壮性:
监控中心宕掉不影响使用,只是丢失部分 采样数据
数据库宕掉后,注册中心仍能通过 缓存提供服务列表查询,但不能注册新服务
注册中心对等 集群,任意一台宕掉后,将自动切换到另一台
注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯
服务提供者无状态,任意一台宕掉后,不影响使用
服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复
(3) 伸缩性:
注册中心为对等集群,可动态增加机器部署实例,所有客户端将自动发现新的注册中心
服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者

二、案列分析

2.1 架构

在dubbo-test中创建了三个modle,分别为service接口层,service实现层以及MVC层,dao层这里省略。

 

 2.2 service层

  这里的service层只有写了个简单的接口,后续复杂的同理既可。


2.3 serviceimpl接口层(服务提供者:provider)

     主要是实现2.2中的接口,需要在serviceimpl中导入service生成的jar包,可以直接使用maven安装到仓库,然后在pom中直接添加依赖既可。


项目中使用到的jar包和依赖,在本项目中我直接全部使用依赖的时候出现jar包冲突,所有我把一部分jar包和依赖分开了。


<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.bsoft.cn</groupId>
		<artifactId>dubbo-test</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.bsoft.cn</groupId>
	<artifactId>dubbo-serviceimpl</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>dubbo-serviceimpl Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.bsoft.cn.dubbo</groupId>
			<artifactId>dubbo-service</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>2.8.0</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>dubbo-serviceimpl</finalName>
	</build>
</project>

applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    	http://www.springframework.org/schema/task  
		http://www.springframework.org/schema/task/spring-task-3.1.xsd
		http://dubbo.apache.org/schema/dubbo 
		http://dubbo.apache.org/schema/dubbo/dubbo.xsd"
	    default-lazy-init="true">

	<!-- 自动扫描 --> 
    <context:component-scan base-package="com.bsfot.dubbo.serviceimpl" /> 
   
   <!--1. 配置别名,目的在后台可以看到这个服务的别名,名字可以任意取 -->
   <dubbo:application name="testDubbo"/>
   <!--2.配置注册中心 -->
   <dubbo:registry address="192.168.20.131:2181" protocol="zookeeper"/>
   <!-- 
      3.告诉注册中心我是谁
      interface:接口
      ref:地表的是到底具体发布哪个服务(接口实现类)
      timeout:连接超时时间
    -->
   <dubbo:service interface="com.bsoft.cn.dubbo.service.TestDubboService" ref="dubboServiceImpl" timeout="60000"/>
   <!--4.配置端口 
       消费者要想连接我们,必须通过我们提供的ip和端口
    -->
    <dubbo:protocol name="dubbo" port="12345"/>
    
	
</beans>

提供者在dubbo图像化界面显示如下:



2.4 MVC层(消费者:consumer)

      mvc层也要使用到service接口,所以也要引入service层生成的jar,同样直接引入依赖既可。

代码:

依赖:

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.bsoft.cn</groupId>
		<artifactId>dubbo-test</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.bsoft.cn</groupId>
	<artifactId>dubbo-web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>dubbo-web Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.bsoft.cn.dubbo</groupId>
			<artifactId>dubbo-service</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>2.8.0</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>dubbo-web</finalName>
	</build>
</project>

applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    	http://www.springframework.org/schema/task  
		http://www.springframework.org/schema/task/spring-task-3.1.xsd
		http://dubbo.apache.org/schema/dubbo 
		http://dubbo.apache.org/schema/dubbo/dubbo.xsd"
	    default-lazy-init="true">

<!-- 自动扫描 --> 
    <context:component-scan base-package="com.bsoft.cn.dubbo" /> 
	<!-- 
	    查找远程服务,找到对应的注册中心
	 -->
	 <!--1. 配置别名,目的在后台可以看到这个服务的别名,名字可以任意取 -->
   <dubbo:application name="consumer"/>
   <!--2.配置注册中心 -->
   <dubbo:registry address="192.168.20.131:2181" protocol="zookeeper"/>
   <!--3.告诉注册中心我要什么 -->
   <dubbo:reference interface="com.bsoft.cn.dubbo.service.TestDubboService" id="testService"/>
	
</beans>

消费者在dubbo图像化界面显示如下:


三 总结

在此次的学习中,遇到最大的问题是jar包的冲突,全部使用依赖引入jar时tomcat会启动异常,所以最后有些jar包是在lib下直接引入的。这个地方坑了好长时间,经验不足,写的不好的地方还请指出相互讨论。

四 案列资源下载

上面的案列已经放到GitHub里面,有需要的可以去下载:源码地址

dubbo-admin下载地址:百度网盘链接:https://pan.baidu.com/s/1vpAhfnsmDgjPNsIMNDTGwg 密码:zm7d

猜你喜欢

转载自blog.csdn.net/dc282614966/article/details/81037901