阿里开源dubbo+zookeeper配置详解

以下内容亲测过!!!!!!!!


Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册
服务提供者:

1. 下载zookeeper注册中心,下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/  下载后解压即可,进入D:\apach-zookeeper-3.4.5\bin,

双击zkServer.cmd启动注册中心服务。 (要将conf/zoo_sample.cfg改为conf/zoo.cfg才能启动)


依赖jar包:
        <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>

2. 定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)

  1. package com.unj.dubbotest.provider;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface DemoService {  
  6.   
  7.     String sayHello(String name);  
  8.   
  9.     public List getUsers();  
  10.   
  11. }   

  12.  
     在服务提供方实现接口:(对服务消费方隐藏实现) 
 
  1. package com.unj.dubbotest.provider;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.LinkedList;  
  5. import java.util.List;  
  6.   
  7.   
  8. public class DemoServiceImpl implements DemoService{  
  9.       
  10.      public String sayHello(String name) {  
  11.             return "Hello " + name;  
  12.      }  
  13.      public List getUsers() {  
  14.          List list = new ArrayList();  
  15.          User u1 = new User();  
  16.          u1.setName("jack");  
  17.          u1.setAge(20);  
  18.          u1.setSex("男");  
  19.            
  20.          User u2 = new User();  
  21.          u2.setName("tom");  
  22.          u2.setAge(21);  
  23.          u2.setSex("女");  
  24.            
  25.          User u3 = new User();  
  26.          u3.setName("rose");  
  27.          u3.setAge(19);  
  28.          u3.setSex("女");  
  29.            
  30.          list.add(u1);  
  31.          list.add(u2);  
  32.          list.add(u3);  
  33.          return list;  
  34.      }  
  35. }  

  36. 用Spring配置声明暴露服务:
    接口
  37.  
  38. <?xml version="1.0" encoding="UTF-8"?>  
  39. <beans xmlns="http://www.springframework.org/schema/beans"  
  40.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  41.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  42.     xsi:schemaLocation="http://www.springframework.org/schema/beans  ;
  43.         http://www.springframework.org/schema/beans/spring-beans.xsd  ;
  44.         http://code.alibabatech.com/schema/dubbo  ;
  45.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  ;
  46.         ">  
  47.    
  48.     <!-- 具体的实现bean -->  
  49.     <bean id="demoService" class="com.unj.dubbotest.provider.DemoServiceImpl" />  
  50.       
  51.     <!-- 提供方应用信息,用于计算依赖关系 -->  
  52.     <dubbo:application name="xixi_provider"  />  
  53.    
  54.     <!-- 使用multicast广播注册中心暴露服务地址   
  55.     <dubbo:registry address="multicast://224.5.6.7:1234" />-->  
  56.     
  57.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  58.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />   
  59.     
  60.     <!-- 用dubbo协议在20880端口暴露服务 -->  
  61.     <dubbo:protocol name="dubbo" port="20880" />  
  62.    
  63.     <!-- 声明需要暴露的服务接口 -->  
  64.     <dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" />  
  65.       
  66. </beans>

  67. 加载Spring配置,启动服务:
  68. package com.unj.dubbotest.provider;  
  69.   
  70. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  71.   
  72. public class Provider {  
  73.    
  74.     public static void main(String[] args) throws Exception {  
  75.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});  
  76.         context.start();  
  77.    
  78.         System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟  
  79.     }  
  80.    
  81.    

    服务消费者:
    1.通过Spring配置引用远程服务: 

    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:dubbo="http://code.alibabatech.com/schema/dubbo"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  ;
    5.         http://www.springframework.org/schema/beans/spring-beans.xsd  ;
    6.         http://code.alibabatech.com/schema/dubbo  ;
    7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  ;
    8.         ">  
    9.   
    10.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
    11.     <dubbo:application name="hehe_consumer" />  
    12.   
    13.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
    14.     <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->  
    15.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
    16.   
    17.     <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
    18.     <dubbo:reference id="demoService"  
    19.         interface="com.unj.dubbotest.provider.DemoService" />  
    20.   
    21. </beans>  
    2.加载Spring配置,并调用远程服务:
  82. package com.alibaba.dubbo.demo.pp;  
  83.   
  84. import java.util.List;  
  85.   
  86. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  87.   
  88. import com.unj.dubbotest.provider.DemoService;  
  89.   
  90. public class Consumer {  
  91.   
  92.     public static void main(String[] args) throws Exception {  
  93.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  94.                 new String[] { "applicationContext.xml" });  
  95.         context.start();  
  96.   
  97.         DemoService demoService = (DemoService) context.getBean("demoService"); //  
  98.         String hello = demoService.sayHello("tom"); // ִ  
  99.         System.out.println(hello); //   
  100.   
  101.         //   
  102.         List list = demoService.getUsers();  
  103.         if (list != null && list.size() > 0) {  
  104.             for (int i = 0; i < list.size(); i++) {  
  105.                 System.out.println(list.get(i));  
  106.             }  
  107.         }  
  108.         // System.out.println(demoService.hehe());  
  109.         System.in.read();  
  110.     }  
  111.   
  112. }   


     
    调用结果为:

猜你喜欢

转载自blog.csdn.net/sunmingf/article/details/52562630