DUBBO研究与学习一:了解

DUBBO概述

是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点:


DUBBO是一个开源分布式服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成。

Logo

来看一下DUBBO的Logo。


核心部分

Remoting:a network communication framework provicdes sync-over-async and request-response messaging.

Clustering: a remote procedure call abastraction with load-balancing/failover/clustering capabilities.

Registry:a service directory framework for service registration and service event publish/subscription.

Dubbo能做什么?

Integrate different types of RPC solutions(RMI/Hessian...) with unified behavior by the abstraction layer of RPC.

Support out-of-box, plug-able load balancing and fault tolerance strategies.

Achieve granceful service upgrade/downgrade with service registry.


架构


节点角色说明

Provider:暴露服务的服务提供者

Consumer调用远程服务的服务消费者

Registry:服务注册与发现的注册中心

Monitor统计服务的调用次数和调用时间的监控中心

Container:服务运行容器

调用关系说明:

0.服务容器负责启动,加载,运行服务提供者。

1.服务提供者在启动时,向注册中心注册自己提供的服务。

2.服务消费者在启动时,向注册中心订阅自己所需的服务。

3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计统计数据到监控中心,定时每分钟发送一次统计数据到监控中心。


连通性

注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。

监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以报表展示服务提供者向注册中心注册其提供的服务,并汇报调用时间到监控中心,此时间不包含网络开销。

服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,同时汇报调用时间到监控中心,此时间包含网络开销

注册中心,服务提供者,服务消费者三者均为长连接,监控中心除外

注册中心通过长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者

注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表

注册中心和监控中心都是可选的,服务消费者可以直连服务提供者

健壮性

监控中心宕掉不影响使用,只是丢失部分采样数据。

监控中心宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务

注册中心对等集群,任意一台宕掉后,将自动切换到另一台

注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯

服务提供者无状态,任意一台宕掉后,不影响使用

服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复

伸缩性

注册中心为对等集群,可动态增加机器部署实例,所有客户端将自动发现新的注册中心

服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者

升级性

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


Deployer:自动部署服务的本地代理。

Repository:仓库用于存储服务应用发布包。

Scheduler:调度中心基于访问压力自动增减服务提供者。

Admin:统一管理控制台。

Demo

服务端

定义一个Service Interface:该接口需单独打包,在服务提供方和消费方共享(HelloService.java)

package com.alibaba.hello.api;
public interface HelloService
{
    String sayHello(String name);
}

在服务提供方实现接口:对服务消费方隐藏实现(HelloServiceImpl.java)

package com.alibaba.hello.impl;
import com.alibaba.hello.api.HelloService;
public class HelloServiceImpl implements HelloService
{
  public String sayHello(String name)
  {
     return"Hello"+name;
  }
}

用Spring配置声明暴露服务:(provide.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="hello-world-app"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.hello.HelloService" ref="helloService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="helloService" class="com.alibaba.dubbo.hello.HelloServiceImpl" />
 
</beans>

加载Spring配置:(Provider.java)

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider
{
   public static void main(String[]args)
   {
      ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(newString[]{"provider.xml"});//启动成功,监听端口为20880
      System.in.read();//按任意键退出
   }
}

服务消费者

通过Spring配置引用远程服务:(consumer.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 address="multicast://224.5.6.7:1234" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用helloService -->
    <dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" />
 
</beans>


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

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.hello.api.HelloService;
public class Consumer
{
  public static void main(String[]args)
  { 
    ClassPathXmlApplicationContext context=newClassPathXmlApplicationContext(newString[]{"consumer.xml"});
    HelloService helloService=(HelloService)context.getBean("helloService");//获取远程服务代理
    String hello=helloService.sayHello("world");//执行远程方法
    System.out.println(hello);//显示调用结果
   }
}

小结

先对其有个大致的了解,通过进一步地学习不断深入,后期还要不断更新完善。


参考自Dubbo官网Dubbo百度百科





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
HRPC HRPC是一款基于Netty和Zookeeper设计的轻量级高性能RPC框架。特性采用Protostuff序列化;高性能,负载均衡;支持服务的注册和订阅;支持同步及异步2种调用方式;长连接,自动重连;采用cglib动态代理;代码简答,易上手;支持Spring;HRPC结构图服务注册中心服务端1. 通过Spring配置<?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"        xsi:schemaLocation="http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">     <!--扫描需求发布的服务所在的包-->     <context:component-scan base-package="com.yingjun.rpc.service.impl"/>     <context:property-placeholder location="classpath:system.properties"/>     <!--服务端配置-->     <bean id="rpcServer" class="com.yingjun.rpc.server.RPCServer">         <constructor-arg name="zookeeper" value="${zookeeper.address}"/>         <constructor-arg name="serverAddress" value="${server.address}"/>     </bean> </beans>2. 服务接口public interface UserService {     public User getUser(String phone);     public User updateUser(User user); }3. 注册服务实现@HRPCService(UserService.class) public class UserServiceImpl implements UserService {     @Override     public User getUser(String phone) {         User user =new User(111,"yingjun",phone);         return user;     }     @Override     public User updateUser(User user) {         user.setName("yingjun@update");         return user;     } }客户端1. Spring配置<?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"        xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">     <context:annotation-config/>     <context:property-placeholder location="classpath:system.properties"/>     <!--客户端配置-->     <bean id="rpcClient" class="com.yingjun.rpc.client.RPCClient">         <constructor-arg name="zookeeper" value="${zookeeper.address}"/>         <!--订阅需要用到的接口-->         <constructor-arg name="interfaces">             <list>                 <value>com.yingjun.rpc.service.OrderService</value>                 <value>com.yingjun.rpc.service.UserService</value>                 <value>com.yingjun.rpc.service.GoodsService</value>             </list>         </constructor-arg>     </bean> </beans>2. 同步调用UserService userService = rpcClient.createProxy(UserService.class); User user1 = userService.getUser("188888888"); logger.info("result:"   user1.toString());3. 异步调用AsyncRPCProxy asyncProxy = rpcClient.createAsyncProxy(UserService.class); asyncProxy.call("getUser", new AsyncRPCCallback() {      @Override      public void success(Object result) {          logger.info("result:"   result.toString());      }      @Override      public void fail(Exception e) {          logger.error("result:"   e.getMessage());      }  }, "188888888"); 标签:rpc

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值