Dubbo zookeeper 初探

时间:2023-11-18 08:28:44

先把zookeeper在本地给安装好,

安装方法参考:http://blog.****.net/wxwzy738/article/details/16330253

这里的话讲述了两个工程一个工程是提供服务的,一个工程是调用服务的,因为dubbo是跟spring进行无缝连接的,故功能配置在spring的配置文件中,跟spring进行整合开发

1、工程是以maven进行构建的,使用的jar包如下:

Dubbo zookeeper 初探

2、服务提供者的工程

a、dubbo-demo-api  定义接口

  1. public interface IProcessData {
  2. public String deal(String data);
  3. }

b、dubbo-demo-provider 服务提供者

  1. public class ProcessDataImpl implements IProcessData {
  2. /*
  3. * @see com.xxx.bubbo.provider.IProcessData#deal(java.lang.String)
  4. */
  5. @Override
  6. public String deal(String data) {
  7. try {
  8. Thread.sleep(1000);
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. }
  12. return "Finished:" + data;
  13. }
  14. }

c、applicationProvider.xml配置

  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. <!-- Application name -->
  10. <dubbo:application name="hello-world-app" />
  11. <!-- registry address, used for service to register itself -->
  12. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  13. <!-- expose this service through dubbo protocol, through port 20880 -->
  14. <!--
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <dubbo:protocol name="dubbo" port="9090" server="netty"
  17. client="netty" codec="dubbo" serialization="hessian2" charset="UTF-8"
  18. threadpool="fixed" threads="100" queues="0" iothreads="9" buffer="8192"
  19. accepts="1000" payload="8388608" />
  20. -->
  21. <!-- Service interface   Concurrent Control  -->
  22. <dubbo:service interface="com.huangjie.dubbo_Service.service.IProcessData"
  23. ref="demoService" executes="10" />
  24. <!-- Default Protocol -->
  25. <!--
  26. <dubbo:protocol server="netty" />
  27. -->
  28. <!-- designate implementation -->
  29. <bean id="demoService" class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl" />
  30. </beans>

d、启动服务

  1. public class DubboProviderMain {
  2. public static void main(String[] args) throws Exception {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  4. new String[]{"applicationProvider.xml"});
  5. context.start();
  6. System.out.println("Press any key to exit.");
  7. System.in.read();
  8. }
  9. }

3、服务调用者的工程

a、调用类

  1. public class ConsumerThd implements Runnable {
  2. /*
  3. * @see java.lang.Runnable#run()
  4. */
  5. @Override
  6. public void run() {
  7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  8. new String[]{"applicationConsumer.xml"});
  9. context.start();
  10. IProcessData demoService = (IProcessData) context.getBean("demoService"); // get
  11. // service
  12. // invocation
  13. // proxy
  14. String hello = demoService.deal("nihao"); // do invoke!
  15. System.out.println(Thread.currentThread().getName() + " "+hello);
  16. }
  17. public static void main(String[] args) {
  18. new Thread(new ConsumerThd()).start();
  19. /**
  20. * 输出结果:
  21. * Thread-0 Finished:nihao
  22. */
  23. }
  24. }

b、applicationConsumer.xml配置

  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. <!-- consumer application name -->
  10. <dubbo:application name="consumer-of-helloworld-app" />
  11. <!-- registry address, used for consumer to discover services -->
  12. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  13. <dubbo:consumer timeout="5000"/>
  14. <!-- which service to consume? -->
  15. <dubbo:reference id="demoService" interface="com.huangjie.dubbo_Service.service.IProcessData" />
  16. </beans>

4、最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行

Dubbo zookeeper 初探
这样整个调用就完成了,这样的好处是只要远程提供ip地址及端口号,以及对外调用的类,客户端就可以调用,客户端不必知道服务端的地址之类的

而且服务端可以开多个zookeeper服务,这样如果其中一个zookeeper 服务死掉了,其他服务还能正常运行