Installation and use of dubbo

Citation: http://alibaba.github.io/dubbo-doc-static/User+Guide-zh.htm

 

background

# )

With the development of the Internet, the scale of website applications continues to expand, and the conventional vertical application architecture can no longer cope. Distributed service architecture and mobile computing architecture are imperative, and a governance system is urgently needed to ensure the orderly evolution of the architecture.



 

  • single application architecture
    • When the website traffic is small, only one application is needed, and all functions are deployed together to reduce deployment nodes and costs.
    • At this point, a data access framework (ORM) that simplifies CRUD workloads     is key.
  • Vertical Application Architecture
    • When the number of visits gradually increases, the acceleration brought by adding a single application to the machine is getting smaller and smaller, and the application is divided into several applications that are not related to each other to improve efficiency.
    • At this point, a web framework (MVC) for accelerating front-end page development     is key.
  • Distributed Service Architecture
    • When there are more and more vertical applications, the interaction between applications is inevitable. The core business is extracted as an independent service, and a stable service center is gradually formed, so that front-end applications can respond more quickly to changing market demands.
    • At this time, the distributed service framework (RPC) for improving business reuse and integration     is the key.
  • Mobile Computing Architecture
    • When there are more and more services, problems such as capacity evaluation and waste of small service resources gradually emerge, it is necessary to add a dispatch center to manage the cluster capacity in real time based on the access pressure and improve the cluster utilization rate.
    • At this point, a Resource Scheduling and Governance Center (SOA) for improving machine utilization     is key.

# )



 
 Before large-scale service, applications may simply expose and reference remote services through tools such as RMI or Hessian, call them by configuring the URL address of the service, and perform load balancing through hardware such as F5.

(1) When there are more and more services, service URL configuration management becomes very difficult, and the single point pressure of F5 hardware load balancer is also increasing.

此时需要一个服务注册中心,动态的注册和发现服务,使服务的位置透明。

并通过在消费方获取服务提供方地址列表,实现软负载均衡和Failover,降低对F5硬件负载均衡器的依赖,也能减少部分成本。

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

这时,需要自动画出应用间的依赖关系图,以帮助架构师理清理关系。

(3) 接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?

为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。

其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容量。

以上是Dubbo最基本的几个需求,更多服务治理问题参见:

http://code.alibabatech.com/blog/experience_1402/service-governance-process.html

# )


 
 节点角色说明:

  • Provider:  暴露服务的服务提供方。
  • Consumer:  调用远程服务的服务消费方。
  • Registry:  服务注册与发现的注册中心。
  • Monitor:  统计服务的调用次调和调用时间的监控中心。
  • Container:  服务运行容器。

调用关系说明:

  • 0. 服务容器负责启动,加载,运行服务提供者。
  • 1. 服务提供者在启动时,向注册中心注册自己提供的服务。
  • 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  • 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  • 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  • 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

(1) 连通性:

  • 注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小
  • 监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以报表展示
  • 服务提供者向注册中心注册其提供的服务,并汇报调用时间到监控中心,此时间不包含网络开销
  • 服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,同时汇报调用时间到监控中心,此时间包含网络开销
  • 注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外
  • 注册中心通过长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者
  • 注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表
  • 注册中心和监控中心都是可选的,服务消费者可以直连服务提供者

(2) 健状性:

  • 监控中心宕掉不影响使用,只是丢失部分采样数据
  • 数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务
  • 注册中心对等集群,任意一台宕掉后,将自动切换到另一台
  • 注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯
  • 服务提供者无状态,任意一台宕掉后,不影响使用
  • 服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复

(3) 伸缩性:

  • 注册中心为对等集群,可动态增加机器部署实例,所有客户端将自动发现新的注册中心
  • 服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者

(4) 升级性:

  • 当服务集群规模进一步扩大,带动IT治理结构进一步升级,需要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力:


 

  • Deployer:  自动部署服务的本地代理。
  • Repository:  仓库用于存储服务应用发布包。
  • Scheduler:  调度中心基于访问压力自动增减服务提供者。
  • Admin:  统一管理控制台。

安装

一、本地服务

 1、 定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)

public interface CustomerService {
  public String getName();
}

 

   2、 在服务提供方实现接口:(对服务消费方隐藏实现)

public class CustomerServiceImpl implements CustomerService{
  @Override
  public String getName() {
    System.out.print("我打印");
    return "打印结果";
  }
}

  

   3、然后引入dubbo的几个包

          dubbo-2.5.3.jar

         log4j.jar

     netty-3.5.7.Final.jar

        slf4j.jar

     slf4j-log4j.jar

     zkclient.jar

     zookeeper.jar

 

 4、 用Spring配置声明暴露服务:

      新建applicationProvider.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.jinbin.service.customer.CustomerServiceImpl" />  
  <!-- 提供方应用信息,用于计算依赖关系 -->  
  <dubbo:application name="xixi_provider"  />	
  <!-- 使用multicast广播注册中心暴露服务地址   
  <dubbo:registry address="multicast://localhost:1234" />-->   
  <!-- 使用zookeeper注册中心暴露服务地址 -->  
  <dubbo:registry address="zookeeper://192.168.1.3:2181" />	 
  <!-- 用dubbo协议在20880端口暴露服务 -->  
  <dubbo:protocol name="dubbo" port="20880" />  
  <!-- 声明需要暴露的服务接口 -->  
  <dubbo:service interface="com.jinbin.service.customer.CustomerService" ref="demoService" />  
</beans>

   我这里暴露服务器的地址交由zookeeper来管理的,使用者首先先要安装zookeeper应用才能使用此功能,相关安装步骤请参看相关博文

 

5、 加载Spring配置,并调用远程服务:(也可以使用IoC注入) 

public class DubooProvider {
  public static void main(String[] args) {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  new String[]{"applicationProvider.xml"});  
        context.start();  
        System.out.println("Press any key to exit.");  
        try {
      System.in.read();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }  
  }
}

 并且启动,使其进入启动状态。以上为服务器提供者的完整步骤,功能接口都已经写好,下面我们就开始怎么远程调用

 

二、 服务消费者

 1、新建个配置文件applicationConsumer.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="consumer-of-helloworld-app" />	 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->  
  <dubbo:registry  protocol="zookeeper" address="zookeeper://192.168.1.3:2181" />	   
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
  <dubbo:reference id="demoService" interface="com.jinbin.service.customer.CustomerService" />  
</beans>

 

为了在web中使用,我们在web.xml中配置在spring启动读取过程中

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application.xml /WEB-INF/applicationConsumer.xml</param-value>
    </context-param>

 

 2、接口调用

  调用过程很简单,先把接口文件打成jar包,然后在此工程中进行引用

 在springmvc调用程序如下:

@Autowired CustomerService demoService ;	

 

@RequestMapping(value="duboo1")
  public void duboo1(){
       demoService.getName();
}

 即可执行成功

 

三、dubbo-admin的使用

    下载dubbo-admin-2.5.3.war

    将其放到tomcat下面,配置 dubbo.properties,

vi webapps/ROOT/WEB-INF/dubbo.properties

dubbo.properties

dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.admin.root.password=root

dubbo.admin.guest.password=guest

      修改zookeeper的URL和端口

    启动:

./bin/startup.sh

   打开,直接访问首页如下:

 

 
 
 
 
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038180&siteId=291194637
Recommended