Spring整合Dubbo+zookeeper

下载安装Zookeeper

1.Dubbo依赖于Zookeepe来当注册中心,所以先安装Zookeeper
下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
我下载的是zookeeper-3.1.14.tar.gz版本
2.解压zookeeper-3.1.14.tar.gz得到zookeeper-3.1.14文件夹
在这里插入图片描述
3.在同级目录创建zookeeper-data文件夹
4.进入zookeeper-3.1.14–>conf–>zoo_sample.cfg更名为zoo.cfg文件夹,
在这里插入图片描述
5.打开zoo.cfg文件,修改dataDir=所创建的zookeeper-data所在的路径
在这里插入图片描述
6.进入bin文件夹,点击zkServer.cmd,启动zookeeper,默认端口为2181在这里插入图片描述
在这里插入图片描述
到此zookeeper安装完毕。

Spring整合Dubbo

1.导入jar

	<!-- dubbo -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>2.5.3</version>
	</dependency>
	<!-- zookeeper -->
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.3.3</version>
		<exclusions>
			<exclusion>
				<artifactId>log4j</artifactId>
				<groupId>log4j</groupId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.1</version>
	</dependency>

2.新建dubbo.xml,在web.xml中添加启动扫描
在这里插入图片描述
在这里插入图片描述
3.配置服务提供者,暴露接口,服务提供者只保留接口的实现类,暴露的接口提取到公共的dubbo配置工程中

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方信息,项目名 -->
	<dubbo:application name="daigou-search" />

	<!-- 使用zookeeper注册中心 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"
		client="zkclient" />

	<!-- 使用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20883" />

	<!-- 暴露接口 -->
	<dubbo:service interface="com.daigou.search.service.ItemService"
		ref="itemService" />
	<bean id="itemService" class="com.daigou.search.service.impl.ItemServiceImpl" />
	<dubbo:service interface="com.daigou.search.service.SearchService"
		ref="searchService" />
	<bean id="searchService" class="com.daigou.search.service.impl.SearchServiceImpl" />
</beans>

4.新建dubbo接口配置工程,定义暴露的接口,由服务的提供方和服务的消费方来引用该工程
在这里插入图片描述
5.定义服务的消费方,先初始化第2步,添加配置文件和启动扫描

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方信息,项目名 -->
	<dubbo:application name="daigou-rest" />

	<!-- 使用zookeeper注册中心 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"
		client="zkclient" />
		
	<dubbo:reference id="itemService" interface="com.daigou.search.service.ItemService"/>
	<dubbo:reference id="searchService" interface="com.daigou.search.service.SearchService"/>
</beans>

配置完了,注解当本地的bean一样注入就好,暴露的接口有什么方法,直接调用方法就ok

	@Autowired
	private SearchService searchService;
	
	@Override
	public JsonMessage findItemByKeyWords(String keywords, Integer page_index, Integer page_size) throws Exception {
		
		JsonMessage json = searchService.query(keywords, page_index, page_size);
		return json;
	}

6.切记不同的项目dubbo服务名字不能相同,暴露的dubbo的端口也不能相同,默认端口为20880
集群同一项目,需要更改端口启动,否则也会启动报错,请记好项目启动的dubbo暴露端口,方便维护项目

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值