dubbo zookeeper注册模型结构

 

流程说明:

  • 服务提供者启动时    

  • 向/dubbo/com.foo.BarService/providers目录下写入自己的URL地址。

  • 服务消费者启动时    

  • 订阅/dubbo/com.foo.BarService/providers目录下的提供者URL地址。

    • 并向/dubbo/com.foo.BarService/consumers目录下写入自己的URL地址。
  • 监控中心启动时    

  • 订阅/dubbo/com.foo.BarService目录下的所有提供者和消费者URL地址

1.新建一个maven项目dubboTest,修改pom文件,增加以下依赖

1 <dependency> 2 <groupId> 3 org.springframework 4 </groupId> 5 <artifactId> 6 spring-context 7 </artifactId> 8 <version> 9 3.2.5.RELEASE 10 </version> 11 </dependency> 12 <dependency> 13 <groupId> 14 com.alibaba 15 </groupId> 16 <artifactId> 17 dubbo 18 </artifactId> 19 <version> 20 2.4.9 21 </version> 22 </dependency> 23 <dependency> 24 <groupId> 25 org.apache.zookeeper 26 </groupId> 27 <artifactId> 28 zookeeper 29 </artifactId> 30 <version> 31 3.3.3 32 </version> 33 <exclusions> 34 <exclusion> 35 <groupId> 36 com.sun.jmx 37 </groupId> 38 <artifactId> 39 jmxri 40 </artifactId> 41 </exclusion> 42 <exclusion> 43 <groupId> 44 com.sun.jdmk 45 </groupId> 46 <artifactId> 47 jmxtools 48 </artifactId> 49 </exclusion> 50 <exclusion> 51 <groupId> 52 javax.jms 53 </groupId> 54 <artifactId> 55 jms 56 </artifactId> 57 </exclusion> 58 </exclusions> 59 </dependency> 60 <dependency> 61 <groupId> 62 com.github.sgroschupf 63 </groupId> 64 <artifactId> 65 zkclient 66 </artifactId> 67 <version> 68 0.1 69 </version> 70 </dependency> 71 <dependency> 72 <groupId> 73 com.netflix.curator 74 </groupId> 75 <artifactId> 76 curator-framework 77 </artifactId> 78 <version> 79 1.1.16 80 </version> 81 </dependency> 82

 

2.创建相关服务接口

新建一个DemoService接口

1package com.alibaba.dubbo.demo; 2 3public interface DemoService { 4 String sayHello(String name); 5} 6

 

 

3.实现相关服务

实现DemoService接口

1package com.alibaba.dubbo.demo.provider; 2 3import com.alibaba.dubbo.demo.DemoService; 4 5public class DemoServiceImpl implements DemoService { 6 7 public String sayHello(String name) { 8 return "Hello " + name; 9 } 10} 11

 

4.创建测试服务提供者和消费者

创建服务提供者:

1package com.alibaba.dubbo.demo; 2 3import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5public class Provider { 6 7 public static void main(String[] args) throws Exception { 8 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 9 new String[] { "provider.xml" } 10 ); 11 12 context.start(); 13 14 System.out.println("请按任意键退出"); 15 16 System.in.read(); // 按任意键退出 17 } 18} 19

 

创建消费者

1package com.alibaba.dubbo.demo; 2 3import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5public class Consumer { 6 7 public static void main(String[] args) throws Exception { 8 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 9 new String[] { "consumer.xml" } 10 ); 11 12 context.start(); 13 14 DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理 15 16 String res = demoService.sayHello("world"); // 执行远程方法 17 18 System.out.println(res); // 显示调用结果 19 } 20} 21

 

 

5.通过xml文件来配置

在resources下创建provider.xml

1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 9 <!-- 提供方应用信息,用于计算依赖关系 --> 10 <application name="dubbo-test-service" /> 11 <!-- 使用multicast广播注册中心暴露服务地址 --> 12 <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 13 <registry address="zookeeper://127.0.0.1:2181" client="zkclient" /> 14 <!-- 用dubbo协议在20880端口暴露服务 --> 15 <protocol name="dubbo" port="20880" /> 16 <!-- 声明需要暴露的服务接口 --> 17 <service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> 18 <!-- 和本地bean一样实现服务 --> 19 <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" /> 20</beans> 21 22

 

在resources下创建consumer.xml  

1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd 9 "> 10 <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> 11 <application name="dubbo-test-consumer" /> 12 <!-- 使用multicast广播注册中心暴露发现服务地址 --> 13 <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 14 <registry address="zookeeper://127.0.0.1:2181" /> 15 <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> 16 <reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /> 17</beans> 18

 

   ps:这里是把服务器方和提供方都注册到了zookeeper上统一管理。上面的IP为安装了zookeeper的服务器地址。如果不想用zookeeper管理的话,可以改为

    <dubbo:registry address="multicast://224.5.6.7:1234" />

 

6.测试

先保证zookeeper已经启动,在通过provider.java启动服务,最后运行consumer.java来测试。测试输出:

1log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). 2log4j:WARN Please initialize the log4j system properly. 3Hello world 4

代码交流 2021