参考资料:https://segmentfault.com/a/1190000009568509
https://segmentfault.com/a/1190000004654903
0. 环境
Java: jdk1.8.0_144
OS: Windows 10(宿主机192.168.1.105)/Ubuntu 15.10(虚拟机192.168.1.21)
Tomcat: apache-tomcat-7.0.68
1. 下载zookeeper部署到虚拟机
- 下载:`wget http://www.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz`
- 解压:`tar -zxvf zookeeper-3.4.9.tar.gz`
- 配置zookeeper:`mv /usr/zookeeper-3.4.9/conf/zoo_sample.cfg /usr/zookeeper-3.4.9/conf/zoo.cfg`
- 启动:在/usr/zookeeper-3.4.9/bin目录下执行`./zkServer.sh start`
2. 下载dubbo源码后打包dubbo-admin并部署到虚拟机
- git下载dubbo源码:https://github.com/alibaba/dubbo/tree/dubbo-2.5.5
- 修改dubbo-admin/src/main/webapp/WEB-INF/dubbo.properties文件的注册地址:dubbo.registry.address=zookeeper://192.168.1.21:2181
- 打包生成war文件并部署到虚拟机(注意:需要隐藏项目名,否则内部跳转易出错)
- 启动tomcat后访问http://192.168.1.21:8080/,用户名密码:root
3. 创建项目并完成配置
- 完成发布和消费服务的编码,见https://github.com/hivsuper/demos/tree/master/dubbo-demo
- 发布服务与zookeeper的连接配置applicationProvider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <dubbo:application name="dubbo-demo-provider" /> <!-- zookeeper注册中心 --> <dubbo:registry address="zookeeper://192.168.1.21:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService1" class="org.lxp.dubbo.service.DemoServiceImpl" /> <bean id="demoService2" class="org.lxp.dubbo.service.DemoServiceImpl2" /> <!-- 向注册中心注册暴漏服务地址,注册服务 --> <dubbo:service interface="org.lxp.dubbo.service.DemoService" ref="demoService1" executes="10" version="1.0.1" /> <dubbo:service interface="org.lxp.dubbo.service.DemoService" ref="demoService2" executes="10" version="1.0.2" /> </beans>
- 消费服务与zookeeper的连接配置applicationConsumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="dubbo-demo-consumer" /> <dubbo:registry address="zookeeper://192.168.1.21:2181" /> <!-- 向注册中心订阅服务 --> <dubbo:reference id="demoService1" interface="org.lxp.dubbo.service.DemoService" version="1.0.1" /> <dubbo:reference id="demoService2" interface="org.lxp.dubbo.service.DemoService" version="1.0.2" /> </beans>
4. 启动发布服务
- 运行dubbo-demo-provider模块BootStrap类的main方法
- 服务已注册成功,见http://192.168.1.21:8080/governance/services
5. 启动消费服务并测试
- tomcat7-maven-plugin插件运行dubbo-demo-consumer应用成功后,可以见到消费者已注册
- 访问http://192.168.1.105:8080/dubbo-consumer/swagger-ui.html可测试API