dubbo注册zookeeper集群配置,dubbo控制台查看提供者和消费者

来源:互联网 发布:arcgis软件介绍 编辑:程序博客网 时间:2024/06/07 03:55

本文开始前已搭好以下环境:

1.zookeeper集群:http://blog.csdn.net/lishirong/article/details/52880946

2.dubbo控制台管理工具

3.用IntellijIdea2016 搭建的基本dubbo项目框架

本实例中dubbo服务提供者以项目启动中在applicationContext.xml中进行注入,其中web.xml配置如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  4.          id="WebApp_ID" version="3.0">  
  5.     <!-- WEB应用名称 -->  
  6.     <display-name>demo.provider</display-name>  
  7.     <!-- WEB应用说明 -->  
  8.     <description>此WEB应用用于展示DUBBO的应用结构</description>  
  9.     <!-- 配置Spring配置文件路径 -->  
  10.     <context-param>  
  11.       <param-name>contextConfigLocation</param-name>  
  12.       <param-value>  
  13.         classpath:applicationContext.xml  
  14.       </param-value>  
  15.     </context-param>  
  16.     <!-- 配置Spring上下文监听器 -->  
  17.     <listener>  
  18.       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  19.     </listener>  
  20.   
  21.     <!-- 配置Spring字符编码过滤器 -->  
  22.     <filter>  
  23.       <filter-name>encodingFilter</filter-name>  
  24.       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  25.       <init-param>  
  26.         <param-name>encoding</param-name>  
  27.         <param-value>UTF-8</param-value>  
  28.       </init-param>  
  29.       <init-param>  
  30.         <param-name>forceEncoding</param-name>  
  31.         <param-value>true</param-value>  
  32.       </init-param>  
  33.     </filter>  
  34.     <filter-mapping>  
  35.       <filter-name>encodingFilter</filter-name>  
  36.       <url-pattern>/*</url-pattern>  
  37.     </filter-mapping>  
  38.   
  39.     <!-- 配置log4j配置文件路径、检测日志配置文件变化 -->  
  40.     <context-param>  
  41.       <param-name>log4jConfigLocation</param-name>  
  42.       <param-value>classpath:log4j.properties</param-value>  
  43.       <param-name>log4jRefreshInterval</param-name>  
  44.       <param-value>30000</param-value>  
  45.     </context-param>  
  46.     <!-- 配置Log4j监听器 -->  
  47.     <listener>  
  48.       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  49.     </listener>  
  50.   
  51.     <!-- 首页 -->  
  52.     <welcome-file-list>  
  53.       <welcome-file>/index.jsp</welcome-file>  
  54.     </welcome-file-list>  
  55.   
  56.     <!-- 错误页 -->  
  57.     <error-page>  
  58.       <error-code>404</error-code>  
  59.       <location>/error/404.jsp</location>  
  60.     </error-page>  
  61.     <error-page>  
  62.       <error-code>500</error-code>  
  63.       <location>/error/500.jsp</location>  
  64.     </error-page>  
  65.   </web-app>  

applicationContext.xml的配置如下:

[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: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.     <!-- 具体的实现bean -->  
  10.     <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />  
  11.   
  12.     <!-- 提供方应用信息,用于计算依赖关系 -->  
  13.     <dubbo:application name="demo_provider" />  
  14.   
  15.     <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234"  
  16.         /> -->  
  17.   
  18.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  19.     <dubbo:registry address="zookeeper://192.168.0.104:2181?backup=192.168.0.105:2181,192.168.0.111:2181" />  
  20.   
  21.     <!-- 用dubbo协议在20880端口暴露服务 -->  
  22.     <dubbo:protocol name="dubbo" port="20880" />  
  23.   
  24.     <!-- 声明需要暴露的服务接口 -->  
  25.     <dubbo:service interface="com.unj.dubbotest.provider.DemoService"  
  26.                    ref="demoService" />  
  27.   
  28. </beans>  

主要是看zookeeper中心的集群注册,其它的服务实现类和实现方法往上也有很多,此处不做探究。项目启动以后,在dubbo管理控制台查看到如下效果:


我本机IP是192.168.0.125,我选择在20880端口暴露dubbo服务,同样的,服务关闭以后,此处就不能查到125的服务了。

下面描述一下消费者的配置,先看一下消费者的applicationContext.xml配置:

[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: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="demo_consumer" />  
  12.   
  13.   
  14.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  15.     <dubbo:registry address="zookeeper://192.168.0.104:2181?backup=192.168.0.105:2181,192.168.0.111:2181"  />  
  16.   
  17.     <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
  18.     <dubbo:reference id="demoService"  
  19.                      interface="com.unj.dubbotest.provider.DemoService" />  
  20.   
  21. </beans>  

消费者示例类Consumer.java

[java] view plain copy
  1. package com.alibaba.dubbo.demo.pp;  
  2.   
  3. import com.unj.dubbotest.provider.DemoService;  
  4. import com.unj.dubbotest.provider.User;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import java.util.List;  
  8.   
  9. public class Consumer {  
  10.   
  11.     public static void main(String[] args) throws Exception {  
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  13.                 new String[] { "applicationContext.xml" });  
  14.         context.start();  
  15.   
  16.         DemoService demoService = (DemoService) context.getBean("demoService");  
  17.         String hello = demoService.sayHello("tom");  
  18.         System.out.println(hello);  
  19.   
  20.         List<User> list = demoService.getUsers();  
  21.         if (list != null && list.size() > 0) {  
  22.             for (int i = 0; i < list.size(); i++) {  
  23.                 System.out.println(list.get(i).getName());  
  24.             }  
  25.         }System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟  
  26.     }  
  27.   
  28. }  
运行主函数以后,在控制台可以看到:

至此,可以看到,我的zookeeper集群,dubbo服务提供者,dubbo服务消费者,dubbo服务管理控制台监控中心,均工作正常,dubbo服务的简单架构运行较好。

个人感觉,dubbo的这种通过zookeeper发现服务,并自动负载均衡的调用服务的方式,还是蛮先进的,有很多容灾方面的考虑,效率也很高,安全性较好,可以继续研究。后面再加上缓存和数据库方面的性能优化,总体架构性能应该会相当之高,后续再研究吧!


阅读全文
0 0
原创粉丝点击