Dubbo环境搭建

来源:互联网 发布:三国演义电视剧 知乎 编辑:程序博客网 时间:2024/06/07 00:07

原文地址:http://blog.csdn.net/sxyandapp/article/details/50622900


本文是基于maven的,预先使用,先装maven。
dubbo是一个分布式服务框架,提供一个SOA的解决方案。简单的说,dubbo就像在生产者和消费者中间架起了一座桥梁,使之能透明交互。
本文旨在搭建一个可供使用和测试的dubbo环境,使用了spring框架;使用了zookeeper和dubbo服务。
准备:
zookeeper:直接去官方网站下载即可,下载后解压,不需要改任何配置即可使用。
dubbo:直接去阿里巴巴下载即可,下载后解压并放到tomcat的webapps目录下,修改WEB-INF/dubbo.properties中属性如下:
#zookeeper的地址和端口
dubbo.registry.address=zookeeper://127.0.0.1:2181
#登录dubbo管理页面时的root用户和guest用户的密码
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

代码:
如果你清楚生产者-消费者模型,那么将会很容易理解dubbo的使用步骤。
一个生产者-消费者模型的代码实现需要3部分:生产者代码,消费者代码,中间接口。
中间接口:创建一个mavan项目,然后添加一个接口如下:

[html] view plain copy
  1. public interface ITest {  
  2. public void sayHello(String message);  
  3. }  

生产者代码:
创建一个mavan项目,并引入spring依赖、中间接口依赖、zookeeper依赖及dubbo依赖如下:
[html] view plain copy
  1. <dependency>  
  2. <groupId>org.springframework</groupId>  
  3. <artifactId>spring-context</artifactId>  
  4. <version>4.1.6.RELEASE</version>  
  5. </dependency>  
  6. <!-- 中间接口,根据你的情况进行修改 -->  
  7. <dependency>  
  8. <groupId>com.dubbo.demo</groupId>  
  9. <artifactId>DubboIServiceDemo</artifactId>  
  10. <version>0.0.1</version>  
  11. </dependency>  
  12. <dependency>  
  13. <groupId>org.apache.zookeeper</groupId>  
  14. <artifactId>zookeeper</artifactId>  
  15. <version>3.4.6</version>  
  16. </dependency>  
  17. <dependency>  
  18. <groupId>com.alibaba</groupId>  
  19. <artifactId>dubbo</artifactId>  
  20. <version>2.5.3</version>  
  21. </dependency>  
  22. <!-- 连接zookeeper的客户端 -->  
  23. <dependency>  
  24. <groupId>com.github.sgroschupf</groupId>  
  25. <artifactId>zkclient</artifactId>  
  26. <version>0.1</version>  
  27. </dependency>  


编写中间接口的实现,代码如下,注意这里通过注解的方式将此类标记为了spring服务:
[html] view plain copy
  1. @Service("springservice")  
  2. public class Test implements ITest {  
  3. /*  
  4. * @authorShixy  
  5. *@date 2016年1月5日  
  6. * @seecom.dubbo.iservice.ITest#sayHello(Java.lang.String)  
  7. */  
  8. @Override  
  9. public void sayHello(String message) {  
  10. System.out.println("service say:" +message);  
  11. }  
  12. }  

编写spring配置文件,注意需要引入dubbo的schema:
[html] view plain copy
  1. <?xml version="1.0"encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"  
  4. xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5. xmlns:context="http://www.springframework.org/schema/context"  
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8. http://www.springframework.org/schema/tx  
  9. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  10. http://www.springframework.org/schema/jee  
  11. http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
  12. http://code.alibabatech.com/schema/dubbo  
  13. http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  14. http://www.springframework.org/schema/context  
  15. http://www.springframework.org/schema/context/spring-context-3.1.xsd"  
  16. default-lazy-init="false">  
  17.   
  18. <bean id="springservice" class="com.dubbo.iservice.impl.Test">  
  19. </bean>  
  20. <!--提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->  
  21. <dubbo:application name="dubbo_provider"></dubbo:application>  
  22. <!--使用zookeeper注册中心暴露服务地址 -->  
  23. <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"subscribe="false" register=""></dubbo:registry>  
  24. <!--要暴露的服务接口 -->  
  25. <dubbo:service interface="com.dubbo.iservice.ITest" ref="springservice"/>  
  26. </beans>  

编写测试类:
[html] view plain copy
  1. public class Demo {  
  2. public static void main(String[] args) {  
  3. //这里注意spring配置文件的名字和路径  
  4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] {"classpath*:config/applicationContext-commons.xml" });  
  5. while(true);  
  6. }  
  7. }  

消费者代码:
创建一个mavan项目,并引入spring依赖、中间接口依赖、zookeeper依赖及dubbo依赖,这里和生产者是完全相同的。
编写spring配置文件,注意需要引入dubbo的schema:
[html] view plain copy
  1. <?xml version="1.0"encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"  
  4. xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5. xmlns:context="http://www.springframework.org/schema/context"  
  6. xsi:schema Location="http://www.springframework.org/schema/beans  
  7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8. http://www.springframework.org/schema/tx  
  9. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  10. http://www.springframework.org/schema/jee  
  11. http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
  12. http://code.alibabatech.com/schema/dubbo  
  13. http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  14. http://www.springframework.org/schema/context  
  15. http://www.springframework.org/schema/context/spring-context-3.1.xsd"  
  16. default-lazy-init="false">  
  17. <dubbo:application name="dubbo_consumer"></dubbo:application>  
  18. <!--使用zookeeper注册中心暴露服务地址 -->  
  19. <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"></dubbo:registry>  
  20. <!--要引用的服务 -->  
  21. <dubbo:reference interface="com.dubbo.iservice.ITest" id="springservice" ></dubbo:reference>  
  22. </beans>  

编写测试类:
[html] view plain copy
  1. public class ClientDemo {  
  2. public static void main(String[] args) {  
  3. //这里注意spring配置文件的名字和路径  
  4. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] {"classpath*:config/applicationContext-commons.xml" });  
  5. ITest test = (ITest) applicationContext.getBean("springservice");  
  6. test.sayHello("abcdefg");  
  7. while(true);  
  8. }  
  9. }  


开始测试:

1.启动zookeeper。执行zookeeper目录下:bin/zkServer.cmd启动服务
2.启动dubbo服务。启动dubbo所在tomcat(详见本文开头),dubbo启动后,可通过http://127.0.0.1:8080/dubbo-admin来查看dubbo服务状态
3.启动生产者服务。运行生产者测试类Client.java
4.启动消费者服务。运行消费者服务代码ClientDemo.java,此时可以在生产者的控制台看到服务被调用了。
此时整个dubbo测试已经完成。同时我们可以在dubbo的web端看到生产者、消费者的状态以及各个服务的调用情况。

为了方便大家,本测试用例以及需要的工具都可以在这里下载:

zookeeper下载:zookeeper下载
dubbo下载(配置文件已修改,可直接解压后放到tomcat中使用):dubbo下载地址
实例代码下载(包含接口、生产者、消费者):实例代码下载