淘淘商城第16讲——发布Dubbo服务

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。说了这么一些,那么Dubbo应该怎么使用呢?下面我将揭晓答案。

如果不用Dubbo,单一工程中Spring的配置可能如下:

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

一旦用了Dubbo,在本地服务的基础上,只需做简单配置,即可完成远程化服务。例如,将上面的配置文件拆分成两份,将服务定义部分放在服务提供方(例如remote-provider.xml),将服务引用部分放在服务消费方(例如remote-consumer.xml),并在提供方增加暴露服务配置<dubbo:service>,在消费方增加引用服务配置<dubbo:reference>

所以,我们要是使用了Dubbo的话,那么就要分为发布服务和调用服务两部分了,发布服务的方式如下:

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

(表现层)调用服务的方式如下:

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

下面我们便以一个具体的例子来说明Dubbo的使用。我们的需求是这样的,想根据商品的id来查询商品的详细信息,商品表是tb_item,如下图所示。
在这里插入图片描述
既然要查询就要有接口,我们在taotao-manager-interface工程下的com.taotao.service包中新建一个ItemService接口,在该接口中添加一个根据id查商品的方法,如下图所示。
在这里插入图片描述
由于taotao-manager-service和taotao-manager-interface这两个工程都是独立的工程,那么要想在taotao-manager-service工程中引用taotao-manager-interface工程中的接口,就需要在taotao-manager-service工程的pom文件中添加对taotao-manager-interface的依赖,如下图所示,如果我记得没错的话,之前我就已添加过了。
在这里插入图片描述
然后我们在taotao-manager-service工程的com.taotao.service.impl包下新建一个实现类,如下图所示。
在这里插入图片描述
接下来,我们就来发布Dubbo服务,当然,在此之前我们需要先把Dubbo相关的包给引进来,我们需要在taotao-manager-service工程的pom文件中添加这三者的依赖。

<!-- 与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>
<!-- Zookeeper的客户端,你要连接Zookeeper,需要把以下两个jar包加进来 -->
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
	<groupId>com.github.sgroschupf</groupId>
	<artifactId>zkclient</artifactId>
</dependency>

这儿,大家要注意,dubbo-2.5.3.jar包下面依赖了一个spring-2.5.6.SEC03.jar包,要知道我们整个工程现在所使用的Spring都是4.2.4这个版本的了,这儿冒出个2.5.6版本,这就有可能会产生冲突。你可能想问,这个2.5.6版本的jar包是从哪儿来的呢?其实它是依赖传递过来的。所以,我们应在dubbo-2.5.3.jar包中去掉对spring-2.5.6.SEC03.jar包的依赖。同理,我们还要在dubbo-2.5.3.jar包中去掉对netty-3.2.5.jar包的依赖。

maven更新之后,我们来查看下taotao-manager-service工程目前所依赖的jar包情况,如下图所示。
在这里插入图片描述
如此一来,当前taotao-manager-service工程的pom文件的全部内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-manager</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>taotao-manager-service</artifactId>
	<packaging>war</packaging>

	<dependencies>
		<!-- 依赖taotao-manager-dao -->
		<dependency>
			<groupId>com.taotao</groupId>
			<artifactId>taotao-manager-dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- 依赖taotao-manager-interface -->
		<dependency>
			<groupId>com.taotao</groupId>
			<artifactId>taotao-manager-interface</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<!-- 与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>
		<!-- Zookeeper的客户端,你要连接Zookeeper,需要把以下两个jar包加进来 -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
	</dependencies>
</project>

现在我们开始发布Dubbo服务,需要说明一点的是,我们应在taotao-manager-service工程的applicationContext-service.xml文件中发布Dubbo服务。

首先,我们需要在文件头部添加对Dubbo的引用及约束,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<context:component-scan base-package="com.taotao.service"></context:component-scan>

</beans>

效果如下图所示:
在这里插入图片描述
然后,为了能让Eclipse能够识别Dubbo的约束,我们需要手动配置一下该约束。如果你还是不会配置,那么请参考我的这篇文章——《淘淘商城第18讲——关于发布和引用Dubbo服务时,报错cvc-complex-type.2.4.c: The matching wildcard is strict···》进行学习。

配置好了Dubbo的约束之后,我们再看看是否还有别的约束需要配置,如果还有其他约束需要配置那就配置一下,这里也不再赘述。

接着,我们在applicationContext-service.xml文件中添加如下配置:

<!-- 使用Dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-manager" />
<dubbo:registry protocol="zookeeper" address="192.168.81.131:2181" />
<!-- 用dubbo协议在20880端口(端口可以随便写,但默认是20880)暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" />

其中<dubbo:application name="taotao-manager" />是用来配置在注册中心的名字,标识我们当前应用的一个名称,你可以随便起,但是最好不要跟其他应用的名称重复,所以最好跟你的工程名相对应。

从以上配置中,我们还能注意到我们在声明需要暴露的服务接口的时候,有ref="itemServiceImpl"这句话,但我们并没有声明过id为itemServiceImpl的类啊,为什么可以直接使用呢?这是因为我们配置了<context:component-scan base-package="com.taotao.service"/>扫描范围,我们在com.taotao.service包下新建了一个ItemServiceImpl的实现类,并且我们没有明确指定其id,这样的话,系统会默认它的首字母小写作为id,这样虽然我们没有明确定义id为itemServiceImpl的bean,我们依然还是可以使用的。

另外需要注意<dubbo:registry protocol="zookeeper" address="192.168.81.131:2181" />配置的IP地址是我们安装Zookeeper的虚拟机的IP地址,其中2181是Zookeeper默认运行的端口号,即客户端连接服务端的端口号是2181。

这样一来,当前applicationContext-service.xml文件的全部内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<context:component-scan base-package="com.taotao.service"></context:component-scan>
	
	<!-- 使用Dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="taotao-manager" />
	<dubbo:registry protocol="zookeeper" address="192.168.81.131:2181" />
	<!-- 用dubbo协议在20880端口(端口可以随便写,但默认是20880)暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" />

</beans>

至此,我们便发布了一个Dubbo服务。

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李阿昀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值