目的:安装Dubbo服务,用zookeeper作为注册中心实践。
一.部署环境准备
CentOS Linux release 7.2.1511 (Core) + jdk 1.7.0_79 + zookeeper-3.4.9 + apache-tomcat-7.0.67
以上可官网下载
zookeeper伪分布式集群(dell720:2181,dell720:2182,dell720:2183)
二.开发调试环境
pom.xml
1)spring必要依赖
2)dubbo依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
3)zkclient依赖
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
三.dubbo admin安装(web管理工具)
1.下载dubbo-admin-*.war包,部署到tomcat下,更改dubbo.properties 文件
/dubbo-admin-2.5.4/WEB-INF/dubbo.propertiesdubbo.registry.aaaddress=zookeeper://127.0.0.1:2183 dubbo.admin.root.password=root dubbo.admin.guest.password=guest
修改相关zk地址和管理后台的访问密码。
注意:启动报错,需要jdk版本1.8以下
四.调试代码
1)Provider提供者
- Provider:接口
public interface DemoService { public void sayHello(); }
- Provider:实现
public class DemoServiceImpl implements DemoService { public void sayHello() { System.out.println("Hello"); } }
- Provider配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="hello-world-app" /> <dubbo:registry protocol="zookeeper" address="dell720:2181,dell720:2182,dell720:2183" /> <dubbo:protocol name="dubbo" port="20880" /> <!-- 和本地bean一样实现服务 --> <dubbo:service interface="com.tain.dubbo.provider.DemoService" ref="demoService" /> <bean id="demoService" class="com.tain.dubbo.provider.DemoServiceImpl" /> </beans>
- Provider启动类
public class DubboProviderDemo { public static void main(String[] args) throws InterruptedException { new ClassPathXmlApplicationContext(new String[]{"provider.xml" }); while (true) { } } }
- Consumer消费类
public class DubboComsumeDemo { public static void main(String[] args) throws InterruptedException { ApplicationContext factory = new ClassPathXmlApplicationContext( new String[] {"consumer.xml"});
DemoService demoService =(DemoService)factory.getBean("demoService"); demoService.sayHello(); } }
结果:Provider控制台打印出“Hello”为全部流程调试通过。
注意:如执行过程中报链接错误,请检查是否是zk端口被防火墙拦截,请开启相关权限或者关闭防火墙再重试。
参考资料:http://dubbo.io/Home-zh.htm
源码下载:
1.demo源码,我的云盘(http://pan.baidu.com/s/1hskhiBE)
2.dubbo源码,我的云盘(http://pan.baidu.com/s/1jHEpbnS)
3.dubbo-admin-2.5.4, 我的云盘(http://pan.baidu.com/s/1geNOBaN)
发表评论