本文开始前已搭好以下环境:
1.zookeeper集群:http://blog.csdn.net/lishirong/article/details/52880946
2.dubbo控制台管理工具
3.用IntellijIdea2016 搭建的基本dubbo项目框架
本实例中dubbo服务提供者以项目启动中在applicationContext.xml中进行注入,其中web.xml配置如下:
- <?xml version="1.0" encoding="utf-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="WebApp_ID" version="3.0">
- <!-- WEB应用名称 -->
- <display-name>demo.provider</display-name>
- <!-- WEB应用说明 -->
- <description>此WEB应用用于展示DUBBO的应用结构</description>
- <!-- 配置Spring配置文件路径 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:applicationContext.xml
- </param-value>
- </context-param>
- <!-- 配置Spring上下文监听器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置Spring字符编码过滤器 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 配置log4j配置文件路径、检测日志配置文件变化 -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>30000</param-value>
- </context-param>
- <!-- 配置Log4j监听器 -->
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- 首页 -->
- <welcome-file-list>
- <welcome-file>/index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 错误页 -->
- <error-page>
- <error-code>404</error-code>
- <location>/error/404.jsp</location>
- </error-page>
- <error-page>
- <error-code>500</error-code>
- <location>/error/500.jsp</location>
- </error-page>
- </web-app>
applicationContext.xml的配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- ">
- <!-- 具体的实现bean -->
- <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />
- <!-- 提供方应用信息,用于计算依赖关系 -->
- <dubbo:application name="demo_provider" />
- <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234"
- /> -->
- <!-- 使用zookeeper注册中心暴露服务地址 -->
- <dubbo:registry address="zookeeper://192.168.0.104:2181?backup=192.168.0.105:2181,192.168.0.111:2181" />
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.unj.dubbotest.provider.DemoService"
- ref="demoService" />
- </beans>
主要是看zookeeper中心的集群注册,其它的服务实现类和实现方法往上也有很多,此处不做探究。项目启动以后,在dubbo管理控制台查看到如下效果:
我本机IP是192.168.0.125,我选择在20880端口暴露dubbo服务,同样的,服务关闭以后,此处就不能查到125的服务了。
下面描述一下消费者的配置,先看一下消费者的applicationContext.xml配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- ">
- <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
- <dubbo:application name="demo_consumer" />
- <!-- 使用zookeeper注册中心暴露服务地址 -->
- <dubbo:registry address="zookeeper://192.168.0.104:2181?backup=192.168.0.105:2181,192.168.0.111:2181" />
- <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
- <dubbo:reference id="demoService"
- interface="com.unj.dubbotest.provider.DemoService" />
- </beans>
消费者示例类Consumer.java
- package com.alibaba.dubbo.demo.pp;
- import com.unj.dubbotest.provider.DemoService;
- import com.unj.dubbotest.provider.User;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import java.util.List;
- public class Consumer {
- public static void main(String[] args) throws Exception {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[] { "applicationContext.xml" });
- context.start();
- DemoService demoService = (DemoService) context.getBean("demoService");
- String hello = demoService.sayHello("tom");
- System.out.println(hello);
- List<User> list = demoService.getUsers();
- if (list != null && list.size() > 0) {
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i).getName());
- }
- }System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
- }
- }
至此,可以看到,我的zookeeper集群,dubbo服务提供者,dubbo服务消费者,dubbo服务管理控制台监控中心,均工作正常,dubbo服务的简单架构运行较好。
个人感觉,dubbo的这种通过zookeeper发现服务,并自动负载均衡的调用服务的方式,还是蛮先进的,有很多容灾方面的考虑,效率也很高,安全性较好,可以继续研究。后面再加上缓存和数据库方面的性能优化,总体架构性能应该会相当之高,后续再研究吧!