dubbo作为开源的rpc框架,其功能和优点本文不做描述,其开源项目地址:http://dubbo.io/ 。本文主要讲述搭建一个dubbo开发环境和样例,包括基于 zookeeper服务注册中心,provider,consumer和服务治理后台。
- 搭建zookeeper服务注册中心
zookeeper部署分单机部署和机群部署,具体部署步骤可以自己度娘,也可以参考:http://blog.csdn.net/shirdrn/article/details/7183503 编写provider样例
需要依赖spring,zookeeper client,dubbo-all jar包,pom文件如下:<dependencies> <dependency > <groupId >com.alibaba.external </groupId > <artifactId >sourceforge.spring </artifactId > <version >2.5.6 </version > </dependency > <dependency > <groupId >junit</groupId> <artifactId >junit</artifactId> <version >3.8.1 </version > <scope >test </scope > </dependency > <dependency > <groupId >com.alibaba.external </groupId > <artifactId >eclipse.aspectj.weaver </artifactId > <version >1.6.10 </version > </dependency > <dependency > <groupId >com.alibaba </groupId > <artifactId >dubbo</artifactId> <version >2.5.3 </version > </dependency > <dependency > <groupId >org.apache.zookeeper </groupId > <artifactId >zookeeper</artifactId> <version >3.4.5 </version > </dependency > <dependency > <groupId >com.github.sgroschupf </groupId > <artifactId >zkclient</artifactId> <version >0.1 </version > </dependency > </ dependencies>
编写服务接口:
public interface DemoService { public String sayHello(String name); }
编写服务实现:
public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { System. out.println( "hello "+name); return "hello "+name; } }
写配置文件:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" <span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span> xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" default-lazy-init="false" > <dubbo:application name="hello-world-app"></dubbo:application> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://10.10.53.183:4180?backup=10.10.53.183:4181,10.10.53.183:4182" check="false"></dubbo:registry> <!-- 要引用的服务 --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"></dubbo:service> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" /> </beans>
编写consumer样例
pom文件同2中的provider
写测试类:public class DubboConsumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("conf/dubbo_consumer.xml" ); context .start(); DemoService demoService = (DemoService)context.getBean( "demoService"); // 获取远程服务代理 String hello = demoService.sayHello( "world"); // 执行远程方法 System. out.println( hello ); // 显示调用结果 System. in.read(); } }
写配置文件:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" <span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span> xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" default-lazy-init="false" > <dubbo:application name="consumer-of-helloworld-app"></dubbo:application> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://10.10.53.183:4180?backup=10.10.53.183:4181,10.10.53.183:4182" check="false"></dubbo:registry> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /> </beans>
- 搭建服务治理后台
搭建好后zookeeper注册中心,下载dubbo-admin.war 包,我从csdn上下载的,地址:http://download.csdn.net/detail/liweifengwf/7784901
部署到tomcat或者jetty下面,还需要配置dubbo.properties配置文件,文件目录地址:/WEB-INF/dubbo.properties或者/${user.home}/dubbo.properties下,如果dubbo再WEB-INF下没有找到,就会到用户的目录下找。文件内容:
dubbo.registry.address=zookeeper://127.0.0.1:4180 ##zookeeper地址
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
启动 jetty,在浏览器输入ip:8080/dubbo/index.html 即可以看见dubbo管理员页面。