Dubbo+Zookeeper+Spring整合应用

1、Dubbo简介

        Dubbo (http://dubbo.io/)是阿里巴巴公司开源的一个高性能优秀的分布式服务框架,高性能和透明化的RPC远程服务调用方案 、 SOA服务治理方案、并且和 Spring框架无缝集成。

        主要核心部件:
                Remoting:网络通信框架,实现了 sync-over-async 和 request-response 消息机制。
                RPC:一个远程过程调用的抽象,支持负载均衡、容灾和集群功能。
                Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。



              本项目更新在GitHub:地址(https://github.com/AndyCZY/dubbo-first)

 

2、Dubbo应用的场景

        传统软件中,重视的是需求及功能的实现,用户量不大,单台服务器足够应付用户的需求量,最多来一个双机热备。随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。

 

 

 

单一应用架构

  1. 当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
  2. 此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。

垂直应用架构

  1. 当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
  2. 此时,用于加速前端页面开发的 Web框架(MVC) 是关键。

分布式服务架构

  1. 当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
  2. 此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。

流动计算架构

  1. 当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
  2. 此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。



3、Dubbo服务治理

 

 

 

 

        在大规模服务化之前,应用可能只是通过RMI或Hessian等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过F5等硬件进行负载均衡。

 

(1) 当服务越来越多时,服务URL配置管理变得非常困难,F5硬件负载均衡器的单点压力也越来越大。

此时需要一个服务注册中心,动态的注册和发现服务,使服务的位置透明。并通过在消费方获取服务提供方地址列表,实现软负载均衡和Failover,降低对F5硬件负载均衡器的依赖,也能减少部分成本。

 

       (2) 当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。这时,需要自动画出应用间的依赖关系图,以帮助架构师理清理关系。

 

       (3) 接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?
       为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。
其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容量。


4、Dubbo能做什么?

A、透明化的远程方法调用 :就像调用本地方法一样调用远程方法 – 只需简单配置,没有任何API侵入。
B、软负载均衡及容错机制 :可在内网替代F5等硬件负载均衡器 。
C、服务自动注册与发现 :不再需要写死服务提供方地址,注册中心基于接口名查询服务提 供者的IP地址,并且能够平滑添加或删除服务提供者。



5、Dubbo工作原理

 

 

6、Dubbo+Zookeeper+Spring整合应用(zookeeper搭建看这篇文章

 

代码下载地址:https://github.com/AndyCZY/dubbo-first以后代码更新就在GitHub

 

 

6.1服务提供者

package com.czy.dubbo.provider;  
  
/** 
 * @auther 陈郑游 
 * @create 2017/3/25 0025 
 * @功能  定义服务接口: (该接口需单独打包,在服务提供方和消费方共享) 
 * @问题 
 * @说明 
 * @URL地址 
 * @进度描述 
 */  
public interface HelloService {  
  
    String sayHello(String name);  
  
}  

6.2服务提供者的实现接口类

 

package com.czy.dubbo.provider.impl;  
  
import com.czy.dubbo.provider.HelloService;  
  
  
/** 
 * @auther 陈郑游 
 * @create 2017/3/25 0025 
 * @功能     在服务提供方实现接口:(对服务消费方隐藏实现) 
 * @问题 
 * @说明 
 * @URL地址 
 * @进度描述 
 */  
public class HelloServiceImpl implements HelloService {  
  
    public String sayHello(String name) {  
        return "Hello " + name;  
    }  
}    

6.3服务提供者的配置

<?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:context="http://www.springframework.org/schema/context"  
       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="hello-world-app"/>  
  
    <!-- 使用multicast广播注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://10.250.194.195:2181"/>  
  
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20881"/>  
  
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service version="1.0.0" interface="com.czy.dubbo.provider.HelloService" ref="HelloService"/>  
  
    <!-- 和本地bean一样实现服务 -->  
    <bean id="HelloService" class="com.czy.dubbo.provider.impl.HelloServiceImpl"/>  
  
</beans>

6.4消费者

package com.czy.dubbo.consumer;  
  
import com.czy.dubbo.provider.HelloService;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
  
/** 
 * @auther 陈郑游 
 * @create 2017/3/25 0025 
 * @功能   消费者 
 * @问题 
 * @说明 
 * @URL地址 
 * @进度描述 
 */  
public class ConsumerTest {  
  
    public static void main(String[] args) throws Exception {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationConsumer.xml"});  
        context.start();  
  
        // 获取远程服务代理  
        HelloService helloService = (HelloService) context.getBean("HelloService");  
        // 执行远程方法  
        String hello = helloService.sayHello("ChenZhengYou!");  
        // 显示调用结果  
        System.out.println(hello);  
  
        //为保证服务一直开着,利用输入流的阻塞来模拟  
        System.in.read();  
    }  
}    

6.5消费者的配置

<?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="hello-world"/>  
  
    <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->  
    <dubbo:registry address="zookeeper://10.250.194.195:2181"/>  
  
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
    <dubbo:reference id="HelloService" version="1.0.0" interface="com.czy.dubbo.provider.HelloService"/>  
  
  
</beans>

7、Dubbo服务运行方式

      这里举例两种运行方式。先提供服务、消费者才能使用!

 

1.1、main运行方式---Spring容器(建议只用于本地调试)

import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
  
/** 
 * @auther 陈郑游 
 * @create 2017/3/25 0025 
 * @功能    Main方法运行(Spring容器) 
 * @问题 
 * @说明 
 * @URL地址 
 * @进度描述 
 */  
public class DubboProviderTest {  
  
    public static void main(String[] args) throws Exception {    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationProvider.xml"});  
  
        // 按任意键退出  
        context.start();  
    
        System.out.println("Press any key to exit!");  
        //为保证服务一直开着,利用输入流的阻塞来模拟  
        System.in.read();  
    }    
}   

运行后的效果:

 

 

1.2消费者进行消费

package com.czy.dubbo.consumer;  
  
import com.czy.dubbo.provider.HelloService;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
  
/** 
 * @auther 陈郑游 
 * @create 2017/3/25 0025 
 * @功能   消费者 
 * @问题 
 * @说明 
 * @URL地址 
 * @进度描述 
 */  
public class ConsumerTest {  
  
    public static void main(String[] args) throws Exception {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationConsumer.xml"});  
        context.start();  
  
        // 获取远程服务代理  
        HelloService helloService = (HelloService) context.getBean("HelloService");  
        // 执行远程方法  
        String hello = helloService.sayHello("ChenZhengYou!");  
        // 显示调用结果  
        System.out.println(hello);  
  
        //为保证服务一直开着,利用输入流的阻塞来模拟  
        System.in.read();  
    }  
}    

运行后的效果

 

 

2、使用服务器运行----Spring容器运行、web.xml配置(建议使用)

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"  
         version="3.1">  
  
    <display-name>Archetype Created Web Application</display-name>  
  
  
    <welcome-file-list>  
        <welcome-file>index.html</welcome-file>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>  
  
  
    <!--log4j日志配置-->  
    <context-param>  
        <param-name>log4jConfigLocation</param-name>  
        <param-value>classpath:log4j.properties</param-value>  
    </context-param>  
    <context-param>  
        <param-name>log4jLocation</param-name>  
        <param-value>6000</param-value>  
    </context-param>  
    <!-- 定义LOG4J监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    </listener>  
  
  
    <!-- Spring配置文件 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath*:applicationProvider.xml</param-value>  
    </context-param>  
    <!-- Spring监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- 防止Spring内存溢出监听器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener>  
  
    <!-- 配置SESSION超时,单位是分钟 -->  
    <session-config>  
        <session-timeout>15</session-timeout>  
    </session-config>  
  
</web-app>  

启动服务提供者服务器

 


dubbo的控制台也是跟上面一样!


 

项目代码地址:https://github.com/AndyCZY/dubbo-first/

posted @ 2018-01-09 15:33  journeyIT  阅读(15)  评论(0编辑  收藏  举报