使用dubbo作为服务中间件通信

最近做的一个分布式项目用到了dubbo,写一下使用过程
需要在linux中安装zookeeper-3.4.6.tar.gz来管理dubbo,将服务注册到注册中心

1、安装zookeeper
将zookeeper用xftp传到wmvare虚拟机的linux
2. tar -zxvf zookeeper-3.4.6.tar.gz 解压zookeeper
3. cd zookeeper-3.4.6
4. mkdir data
5. cd conf
6. mv zoo_sample.cfg zoo.cfg (改个名字为zoo.cfg)
7. vi zoo.cfg
8. 将dataDir的路径改为
dataDir=/root/zookeeper-3.4.6/data
9.保存退出wq
10.cd
11.cd bin
9. 启动zookeeper 命令: ./zkServer.sh start
10. 查看启动状态: ./zkServer.sh status
11. 出现Mode:standlone 表示正常启动了
12.看看ip地址:ifconfig
13.记住ip地址

在spring中配置dubbo服务

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	<!-- 将该服务注册到注册中心(zookeeper) -->
	<!-- 服务提供者的名字 -->
	<dubbo:application name="jd-manager"/>
	<!-- 注册到注册中心 -->
	<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"></dubbo:registry>
	<!-- 配置协议,用来暴漏服务(配置暴漏服务的端口号) -->
	<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
	<!-- 暴漏服务 -->
	<dubbo:service interface="org.java.service.ItemService" ref="itemServiceImpl"></dubbo:service>
	<dubbo:service interface="org.java.service.ItemCatService" ref="itemCatServiceImpl"></dubbo:service>
</beans>

在web的springmvc.xml中引用服务

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	<!-- 扫描用@Controller注解的类,并且加载到spring中进行管理 -->
	<context:component-scan base-package="org.java.controller"></context:component-scan>
	<!-- 配置注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="1073741824"></property>
	</bean>
	
	<!-- 不拦截静态资源 
	<mvc:resources location="/css/" mapping="/css/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>-->
	<mvc:default-servlet-handler/>
	<!-- 引用服务 -->
	<dubbo:application name="jd-manager-web"/>
	<!-- 去注册中心拿服务 -->
	<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"></dubbo:registry>
	<!-- 引用服务 -->
	<dubbo:reference interface="org.java.service.ItemService" id="itemService"></dubbo:reference>
	<dubbo:reference interface="org.java.service.ItemCatService" id="itemCatService"></dubbo:reference>
	<dubbo:reference interface="org.java.content.service.ContentCatService" id="contentCatService"></dubbo:reference>
	<dubbo:reference interface="org.java.content.service.ContentService" id="contentService"></dubbo:reference>
</beans>

这样可以正常启动服务器了。

常见错误:
1、连接超时 timeout
分析:可能是zookeeper没有正常开启,或者虚拟机网络断开连接
可使用命令./zkServer.sh status (在zookeeper在bin目录中输入命令)查看启动状态
2、no provider avalible 没有服务特供者
这就很蛋疼了,也许是配置文件配错了,端口号一定要对应上,通常都是ip地址写错了。默认端口20880
3、message can not send ,beacuse the channel is closed,信息无法送达,因为通道被关闭。
可能是网络端口了,或者是service没有启动,就直接启动web导致通道关闭。正常启动service即可。
如果service正常启动,据说这个问题可能是vm虚拟机的虚拟网卡改变了ip地址,在计算机–>管理–>设备管理器–>网络适配器–>禁用vm虚拟网卡 试试。

猜你喜欢

转载自blog.csdn.net/weixin_42982636/article/details/84755599