暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Dubbo的高级配置(二)

Alleria Windrunner 2021-01-13
459
本篇我们来介绍Dubbo高级配置的第二部分:
  • 多协议支持

  • 负载均衡

  • 集群容错


多协议支持

除了 Dubbo 服务暴露协议 Dubbo 协议外,Dubbo 框架还支持另外 8 种服务暴露协议:
  • RMI协议

  • Hessian协议

  • HTTP协议

  • WebService协议

  • Thrift协议

  • Memcached协议

  • Redis协议

  • Rest协议


但在实际生产中,使用最多的就是 Dubbo 服务暴露协议。对于协议的选用总结起来就一句话:大数据小并发用短连接协议,小数据大并发用长连接协议。每个协议有各自的优缺点,接下来我们就一起看一下。
dubbo 协议,Dubbo默认传输协议,连接个数为单连接,连接方式是长连接,传输协议是TCP,传输方式为NIO 异步传输,适用范围为传入传出参数数据包较小(建议小于100K),消费者比提供者个数多,单一消费者无法压满提供者,尽量不要用 dubbo 协议传输大文件或超大字符串。
rmi 协议采用 JDK 标准的java.rmi.*实现,连接个数支持多连接,连接方式是短连接,传输协议是TCP,传输方式为BIO同步传输,适用范围为传入传出参数数据包大小混合,消费者与提供者个数差不多,可传文件。
hession 协议的连接个数支持多连接,连接方式为短连接,传输协议是HTTP,传输方式为BIO同步传输,适用范围为传入传出参数数据包较大,提供者比消费者个数多,提供者抗压能力较大,可传文件。
http 协议连接连接个数为多连接,连接方式是短连接,传输协议为HTTP,传输方式是BIO同步传输,适用范围为传入传出参数数据包大小混合,提供者比消费者个数多,可用浏览器查看,可用表单或 URL 传入参数,暂不支持传文件。
webService 协议连接个数为多连接,连接方式是短连接,传输协议为HTTP,传输方式为BIO同步传输,适用场景为系统集成,跨语言调用。
thrift 协议是Facebook 捐给 Apache 的一个 RPC 框架,其消息传递采用的协议即为 thrift 协议。当前 dubbo 支持的 thrift 协议是对 thrift 原生协议的扩展。Thrift 协议不支持 null 值的传递。
memcached 协议与 redis 协议都是高效的 KV 缓存服务器。它们会对传输的数据使用相应的技术进行缓存。
rest 协议则在开发具有 RESTful 风格的服务场景下,则需要使用该协议。
对于多协议的使用我们又分为两种情况:
  • 同一服务支持多种协议

  • 不同服务使用不同协议


我们先来看同一服务支持多种协议,修改服务提供者工程配置文件,声明新添加的协议,然后在服务<dubbo:service/>标签中再新增该新的协议。若不指定,默认为dubbo协议。
    <?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


    <dubbo:application name="provider-version"/>


    <dubbo:registry address="zookeeper://zkOS:2181"/>


    <!--注册Service实现类-->
    <bean id="oldService" class="com.eleven.provider.OldServiceImpl"/>
    <bean id="newService" class="com.eleven.provider.NewServiceImpl"/>


    <!--暴露服务-->
    <dubbo:service interface="com.eleven.service.SomeService"
    ref="oldService" version="0.0.1"/>
    <dubbo:service interface="com.eleven.service.SomeService"
    ref="newService" version="0.0.2" protocol="dubbo,rmi"/>


    </beans>

    这里需要理解这个服务暴露协议的意义。其是指消费者若要连接当前的服务,就需要通过这里指定的协议及端口号进行访问。这里的端口号可以是任意的,不一定非要使用默认的端口号(Dubbo 默认为 20880,rmi 默认为 1099)。这里指定的协议名称及端口号,在当前服务注册到注册中心时会一并写入到服务映射表中。当消费者根据服务名称查找到相应主机时,其同时会查询出消费此服务的协议、端口号等信息。其底层就是一个 Socket 编程,通过主机名与端口号进行连接。
    然后修改消费者配置文件,在消费者引用服务时要指出所要使用的协议。
      <?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://dubbo.apache.org/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

      <dubbo:application name="consumer-version"/>


      <dubbo:registry address="zookeeper://zkOS:2181" >


      <!--指定消费0.0.1版本,即oldService提供者-->
      <!--<dubbo:reference id="someService" version="0.0.1"-->
      <!--interface="com.eleven.service.SomeService"/>-->


      <!--指定消费0.0.2版本,即newService提供者-->
      <dubbo:reference id="someService" version="0.0.2" protocol="rmi"
      interface="com.eleven.service.SomeService"/>


      </beans>
      同一服务支持多种协议”的应用场景是:系统在使用过程中其使用场景逐渐发生了变化,例如由原来的消费者数量多于提供者数量,变为了消费者数量与提供者数量差不多了,并且原来系统不用传输文件,现在的系统需要传输文件了。此时就将将原来默认的 dubbo 协议更换为 rmi 协议。目的是为了兼容老工程,扩展新功能。
      接下来是不同服务使用不同协议,此种需求出现在同一个系统中不同的业务具有不同的特点,所以它们的传输协议就应该根据它们的特点选择不同的协议。例如对于前面使用服务分组实现的“微信支付”与“支付宝支付”,就可以针对不同的 支付方式,使用不同的协议。
        <?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://dubbo.apache.org/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


        <dubbo:application name="provider-group"/>


        <dubbo:registry address="zookeeper://zkOS:2181"/>


        <!--注册Service实现类-->
        <bean id="weixinService" class="com.eleven.provider.WeixinServiceImpl"/>
        <bean id="zhifubaoService" class="com.eleven.provider.ZhifubaoServiceImpl"/>


        <!--暴露服务-->
        <dubbo:service interface="com.eleven.service.SomeService"
        ref="weixinService" group="pay.weixin" protocol="dubbo"/>
        <dubbo:service interface="com.eleven.service.SomeService"
        ref="zhifubaoService" group="pay.zhifubao" protocol="rmi"/>
        </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:dubbo="http://dubbo.apache.org/schema/dubbo"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

          <dubbo:application name="consumer-group"/>


          <dubbo:registry address="zookeeper://zkOS:2181" >


          <!--指定调用微信服务-->
          <dubbo:reference id="weixin" group="pay.weixin"
          interface="com.eleven.service.SomeService" protocol="dubbo"/>
          <!--指定调用支付宝服务-->
          <dubbo:reference id="zhifubao" group="pay.zhifubao"
          interface="com.eleven.service.SomeService" protocol="rmi"/>


          </beans>

          负载均衡

          Dubbo内置的负载均衡算法分为以下几类:
          • random

          • roundrobin

          • leastactive

          • consistenthash


          random随机算法,是 Dubbo 默认的负载均衡算法。存在服务堆积问题。roundrobin轮询算法。按照设定好的权重依次进行调度。leastactive最少活跃度调度算法。即被调度的次数越少,其优选级就越高,被调度到的机率就越高。consistenthash一致性 hash 算法。对于相同参数的请求,其会被路由到相同的提供者。
          负载均衡算法可以在消费者端指定,也可以在提供者端指定。控制的粒度可以在服务级别也可以在方法级别。这里我以消费端配置为例,服务端配置同理。
            <?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://dubbo.apache.org/schema/dubbo"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

            <dubbo:application name="consumer-version"/>


            <dubbo:registry address="zookeeper://zkOS:2181" >


            <!--指定消费0.0.1版本,即oldService提供者-->
            <dubbo:reference id="someService" version="0.0.1"
            interface="com.eleven.service.SomeService"
            loadbalance="roundrobin"/>


            <!--指定消费0.0.2版本,即newService提供者-->
            <dubbo:reference id="someService" version="0.0.2" protocol="rmi"
            interface="com.eleven.service.SomeService"/>
            <dubbo:method name="hello" loadbalance="roundrobin"/>
            <dubbo:method name="doFirst" loadbalance="random"/>
            <dubbo:method name="doFirst" loadbalance="leastactive"/>


            </beans>


            集群容错

            集群容错指的是,当消费者调用提供者集群时发生异常的处理方案。Dubbo 内置了 6 种集群容错策略。
            • Failover

            • Failfast

            • Failsafe

            • Failback

            • Forking

            • Broadcast


            故障转移策略,当消费者调用提供者集群中的某个服务器失败时,其会自动尝试着调用其它服务器。该策略通常用于读操作,例如消费者要通过提供者从 DB 中读取某数据。但重试会带来服务延迟。
            快速失败策略。消费者端只发起一次调用,若失败则立即报错。通常用于非幂等性的写操作,比如新增记录。
            失败安全策略。当消费者调用提供者出现异常时,直接忽略本次消费操作。该策略通常用于执行相对不太重要的服务,例如写入审计日志等操作。

                    失败自动恢复策略。消费者调用提供者失败后,Dubbo 会记录下该失败请求,然后定时自动重新发送该请求。该策略通常用于实时性要求不太高的服务,例如消息通知操作。

            并行策略。消费者对于同一服务并行调用多个提供者服务器,只要一个成功即调用结束并返回结果。通常用于实时性要求较高的读操作,但其会浪费较多服务器资源。
            广播策略。广播调用所有提供者,逐个调用,任意一台报错则报错。通常用于通知所有提供者更新缓存或日志等本地资源信息。
            容错策略可以设置在消费者端,也可以设置在提供者端。若消费者与提供者均做了设置,则消费者端的优先级更高。另外我们还可以设置重试次数,Dubbo 默认的容错策略是故障转移策略 Failover,即允许失败后重试。可以通过如下方式来设置重试次数,注意设置的是重试次数,不含第一次正常调用。
              <?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://dubbo.apache.org/schema/dubbo"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


              <dubbo:application name="provider-version"/>


              <dubbo:registry address="zookeeper://zkOS:2181"/>


              <!--注册Service实现类-->
              <bean id="oldService" class="com.eleven.provider.OldServiceImpl"/>
              <bean id="newService" class="com.eleven.provider.NewServiceImpl"/>


              <!--暴露服务-->
              <dubbo:service interface="com.eleven.service.SomeService"
              ref="oldService" version="0.0.1" retries="2" cluster="failfast">
              <dubbo:method name="hello" retries="2"/>
              <dubbo:service>
              </beans>
              文章转载自Alleria Windrunner,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

              评论