实战--RPC框架之Dubbo快速入门

dubbo是阿里巴巴一个分布式开源框架。非常简单易用。是作为分布式系统架构时的不错自选!笔者接下来将详细介绍dubbo的具体使用。

首先,理论知识准备。

要使用dubbo,我们先来了解一下dubbo框架的原理。如图:

我们来了解一下图中的几个流转节点

 

·        Provider:暴露服务的服务提供方。

·        Consumer:调用远程服务的服务消费方。

·        Registry:服务注册与发现的注册中心。

·        Monitor:统计服务的调用次调和调用时间的监控中心。

·        Container:服务运行容器。

接着来分析一下各个流转节点的调用关系

 

调用关系说明:

·        0. 服务容器负责启动,加载,运行服务提供者。

·        1. 服务提供者在启动时,向注册中心注册自己提供的服务。

·        2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

·        3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

·        4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

·        5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

再有了相关dubbo框架的相关知识理论后,接下来我们就可以使用dubbo了。

dubbo的使用使用方法

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

 

单一工程中spring的配置

 

<bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<bean id="xxxAction" class="com.xxx.XxxAction">
	<property name="xxxService" ref="xxxService" />
</bean>

 

远程服务:

在本地服务的基础上,只需做简单配置,即可完成远程化:

将上面的local.xml配置拆分成两份,将服务定义部分放在服务提供方remote-provider.xml,将服务引用部分放在服务消费方remote-consumer.xml。

并在提供方增加暴露服务配置<dubbo:service>,在消费方增加引用服务配置<dubbo:reference>。

发布服务:

<!-- 和本地服务一样实现远程服务 -->
<bean id="xxxService" class="com.xxx.XxxServiceImpl" />
<!-- 增加暴露远程服务配置 -->
<dubbo:service interface="com.xxx.XxxService" ref="xxxService" />

调用服务:

<!-- 增加引用远程服务配置 -->
<dubbo:reference id="xxxService" interface="com.xxx.XxxService" />
<!-- 和本地服务一样使用远程服务 -->
<bean id="xxxAction" class="com.xxx.XxxAction">
	<property name="xxxService" ref="xxxService" />
</bean>

上面的使用看起来有点抽象,对初学者来说比较难以理解。接下来,笔者以一个实际项目来实战演示。

首先,笔者先介绍一下要演示的项目,项目采用SOA架构,即是面向服务架构。简单来说,就是分成两个工程,一个是表现层工程(相当于三层架构中的web层),一个是服务层工程(聚合工程)(包括三层架构的service层,dao层以及pojo、interface等等,这里面的每个又都是一个工程,),工程与工程之间采用maven来管理。表现层工程与服务层工程是没有关联,中间需要中间件dubbo来传输关联.后台采用springMVC、spring、mybatis框架。

dubbo框架原理中,有一个注册中心register。官方推荐zookeeper来作为注册中心。zookeeper的安装以及启动,笔者在之前的文章写过,这里不再赘述。
1.启动zookeeper

2.在表现层工程的pom.xml中配置相关dubbo的依赖以及tomcat插件

 

<!-- dubbo相关 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.jboss.netty</groupId>
					<artifactId>netty</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>

 

 

<build>
		<plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<path>/</path>
					<port>8083</port>
				</configuration>
			</plugin>
		</plugins>
	</build>

 

 

 

3.在表现层工程的的springmcv配置文件配置dubbo的相关约束以及调用dubbo服务

 

 

 

<!-- 引用dubbo服务 -->
	<dubbo:application name="e3-manager-web"/>
	<dubbo:registry protocol="zookeeper" address="192.168.183.xxx:2181"/>	
	<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />

address是你安装注册中心zookeeper的远程服务器ip地址

 

笔者这里附送一个dubbo的本地约束,免积分供读者使用。

 

如何具体引入dubbo本地约束,可以参见笔者的文章--引入dubbo本地约束

4.服务层工程也是同理的。在pom.xml中配置配置相关dubbo的依赖以及tomcat插件

 

<!-- dubbo相关 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.jboss.netty</groupId>
					<artifactId>netty</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
<!-- 配置Tomcat插件 -->
	<build>
		<plugins>
				<plugin>
					<groupId>org.apache.tomcat.maven</groupId>
					<artifactId>tomcat7-maven-plugin</artifactId>
					<configuration>
						<path>/</path>
						<port>8082</port>
					</configuration>
				</plugin>
		</plugins>
	</build>

5.在服务层的spring配置文件中配置dubbo的相关约束以及提供服务

 

配置约束跟上面一样。

提供服务则为

 

<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="e3-manager" />
	<dubbo:registry protocol="zookeeper"
		address="192.168.183.xxx:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" />

到此,整合完成,分别启动服务层,表现层,

分别出现http-bio-8082  http-bio-8083就证明dubbo整合进去成功!如果,老是不出现,又不报错,可能你没有启动zookeeper。
 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊杰eboy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值