Dubbo

作者: BeYearn | 来源:发表于2019-01-03 15:57 被阅读0次

入门 https://blog.csdn.net/noaman_wgs/article/details/70214612
注意点:
https://blog.csdn.net/liyanlei5858/article/details/79236685
https://www.jianshu.com/p/9d062eceb765

服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外,注册中心通过长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者
注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表

  1. Dubbo的容错方案
    当我们的系统中用到Dubbo的集群环境,因为各种原因在集群调用失败时,Dubbo提供了多种容错模式,缺省为failover重试。

先说明一下各节点关系:

  • 这里的Invoker是Provider的一个可调用Service的抽象,Invoker封装了Provider地址及Service接口信息。
  • Directory代表多个Invoker,可以把它看成List<Invoker>,但与List不同的是,它的值可能是动态变化的,比如注册中心推送变更。
  • Cluster将Directory中的多个Invoker伪装成一个Invoker,对上层透明,伪装过程包含了容错逻辑,调用失败后,重试另一个。
  • Router负责从多个Invoker中按路由规则选出子集,比如读写分离,应用隔离等。
  • LoadBalance负责从多个Invoker中选出具体的一个用于本次调用,选的过程包含了负载均衡算法,调用失败后,需要重选
  • Failover Cluster (读推荐多用这个)
    失败自动切换,当出现失败,重试其它服务器。(缺省)
    通常用于读操作,但重试会带来更长延迟。
    可通过retries="2"来设置重试次数(不含第一次)。
  • Failfast Cluster (写操作推荐这个)
    快速失败,只发起一次调用,失败立即报错。
    通常用于非幂等性的写操作,比如新增记录。
  • Failsafe Cluster
    失败安全,出现异常时,直接忽略。
    通常用于写入审计日志等操作。
  • Failback Cluster
    失败自动恢复,后台记录失败请求,定时重发。
    通常用于消息通知操作
  • Forking Cluster
    并行调用多个服务器,只要一个成功即返回。
    通常用于实时性要求较高的读操作,但需要浪费更多服务资源。
    可通过forks="2"来设置最大并行数。
  • Broadcast Cluster
    广播调用所有提供者,逐个调用,任意一台报错则报错。(2.1.0开始支持)
    通常用于通知所有提供者更新缓存或日志等本地资源信息。

重试次数配置如:(failover集群模式生效)
<dubbo:service retries="2"/>
或:<dubbo:reference retries="2"/>
或:<dubbo:reference>
<dubbo:methodname="findFoo" retries="2"/>
</dubbo:reference>

集群模式配置如:
<dubbo:service cluster="failsafe"/>
或:<dubbo:reference cluster="failsafe"/>

cluster="failfast" 和 cluster="failover"、retries="0"是一样的效果,retries="0"就是不重试

  1. dubbo负载均衡策略:
    在集群负载均衡时,Dubbo提供了多种均衡策略,缺省为random随机调用。
  • RandomLoadBalance
    随机,按权重设置随机概率。
    在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。
  • RoundRobin LoadBalance
    轮循,按公约后的权重设置轮循比率。
    存在慢的提供者累积请求问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。
  • LeastActive LoadBalance
    最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
    使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。
  • ConsistentHashLoadBalance
    一致性Hash,相同参数的请求总是发到同一提供者。
    当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。
  1. 服务上线怎么不影响旧版本(多版本支持)。
当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。
 在低压力时间段,先升级一半提供者为新版本,再将所有消费者升级为新版本,然后将剩下的一半提供者升级为新版本
<dubbo:service interface="com.foo.BarService" version="1.0.0" />
<dubbo:service interface="com.foo.BarService" version="2.0.0" />
<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />
<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />
不区分版本:(2.2.0以上版本支持)
<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />
  1. 测试环境调试 直连某一服务
    只需在服务消费端增加url="dubbo://localhost:20880"即可。
//消费端配置
<?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" default-autowire="byName">

    <dubbo:application name="${dubbo.application.name}"/>
    <dubbo:registry address="${dubbo.registry.address}" timeout="${dubbo.consumer.timeout}" />
    <dubbo:consumer retries="${dubbo.consumer.retries}" group="oa" />

    <!-- basic start -->
    <dubbo:reference  interface="com.xxx.basic.manage.service.SysRoleService" id="sysRoleService" check="false" url="dubbo://localhost:20880"/>
    <dubbo:reference  interface="com.xxx.basic.manage.service.ResourceService" id="resourceService" check="false" url="dubbo://localhost:20880" />
    <dubbo:reference  interface="com.xxx.basic.manage.service.OrganizationService" id="organizationService" check="false" url="dubbo://localhost:20880" />
    <dubbo:reference  interface="com.xxx.basic.manage.service.SysUserService" id="sysUserService" check="false" url="dubbo://localhost:20880"/>
   

</beans>
//服务端配置
<?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://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">

    <dubbo:provider group="xxx"/>
<!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="sports-basic-provider"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}"/>
    <!-- 使用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- basic start -->
    <dubbo:service interface="com.xxx.basic.manage.service.OrganizationService" ref="organizationService" timeout="10000"/>
    <dubbo:service interface="com.xxx.basic.manage.service.ResourceService" ref="resourceService" timeout="10000"/>
    <dubbo:service interface="com.xxx.basic.manage.service.SysRoleService" ref="sysRoleService" timeout="10000"/>
    <dubbo:service interface="com.xxx.basic.manage.service.SysUserService" ref="sysUserService" timeout="10000"/>

</beans>

相关文章

网友评论

      本文标题:Dubbo

      本文链接:https://www.haomeiwen.com/subject/uhkfrqtx.html