Dubbo使用

2018-11-21  本文已影响0人  目睹了整个事件的索先生

Dubbo中文文档地址

https://dubbo.gitbooks.io/dubbo-user-book/content/

Dubbo背景

image.png

Dubbo

Dubbo快速搭建(结合SpringBoot):http://start.dubbo.io/,快速生成消费者和提供者项目

Dubbo的Pom文件配置

        <!-- dubbo 需要的jar start -->
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <!-- dubbo 需要的jar end -->

Dubbo基本使用

服务提供者和消费者都需要引入公用接口相关的jar包

服务提供者

     <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="gw-service-user"/>

    <!-- 使用zookeeper注册中心暴露服务地址
    <dubbo:registry protocol="zookeeper" address="192.168.3.71:2181" />
    -->

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 和本地bean一样实现服务 -->
    <bean id="pmsUserFacade" class="wusc.edu.facade.user.service.impl.PmsUserFacadeImpl"/>

    <!-- 用户服务接口 -->
    <dubbo:service interface="wusc.edu.facade.user.service.PmsUserFacade" ref="pmsUserFacade"/>

服务消费者

     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="edu-web-boss"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- 注册中心地址-->
    <!--<dubbo:registry protocol="zookeeper" address="192.168.3.71:2181" />-->

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>

    <!-- 用户服务接口 -->
    <dubbo:reference interface="wusc.edu.facade.user.service.PmsUserFacade" id="pmsUserFacade" check="false"/>

配置重试次数,最好只用于读的重试,写操作可能会引起多次写入 下面三个任意一个配置就行 默认retries="0"

注册中心

image.png

流程:
1.服务提供者启动时向/dubbo/com.foo.BarService/providers目录下写入URL
2.服务消费者启动时订阅/dubbo/com.foo.BarService/providers目录下的URL向/dubbo/com.foo.BarService/consumers目录下写入自己的URL
3.监控中心启动时订阅/dubbo/com.foo.BarService目录下的所有提供者和消费者URL

Dubbo负载均衡

  1. Random LoadBalance
    随机,按权重设置随机概率。
    在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。
  2. RoundRobin LoadBalance
    轮循,按公约后的权重设置轮循比率。
    存在慢的提供者累积请求问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。
  3. LeastActive LoadBalance
    最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
    使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。
  4. ConsistentHash LoadBalance
    一致性Hash,相同参数的请求总是发到同一提供者。
    当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。算法参见:http://en.wikipedia.org/wiki/Consistent_hashing
    缺省只对第一个参数Hash,如果要修改,请配置<dubbo:parameter key="hash.arguments" value="0,1" />
    缺省用160份虚拟节点,如果要修改,请配置<dubbo:parameter key="hash.nodes" value="320" />
<dubbo:service interface="..." loadbalance="roundrobin" />

<dubbo:reference interface="..." loadbalance="roundrobin" />

<dubbo:service interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:service>

<dubbo:reference interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>

Dubbo多协议配置

不同服务在性能上适用不同协议进行传输,比如大数据用短连接协议,小数据大并发用长连接协议。

    <!-- 多协议配置 -->
   <dubbo:protocol name="dubbo" port="20880" />
   <dubbo:protocol name="rmi" port="1099" />

   <!-- 使用dubbo协议暴露服务 -->
   <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
   <!-- 使用rmi协议暴露服务 -->
   <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" />
   <!-- 使用多个协议暴露服务 -->
   <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,hessian" />

Dubbo多注册中心

     <!-- 多注册中心配置 -->
    <dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />

    <!-- 向多个注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />

一个接口多种实现时


  <dubbo:service group="feedback" interface="com.xxx.IndexService" />
  <dubbo:service group="member" interface="com.xxx.IndexService" />
  
  <dubbo:reference id="feedbackIndexService" group="feedback" interface="com.xxx.IndexService" />
  <dubbo:reference id="memberIndexService" group="member" interface="com.xxx.IndexService" />
  <!--任意组:(2.2.0以上版本支持,总是只调一个可用组的实现)-->
  <dubbo:reference id="barService" interface="com.foo.BarService" group="*" />

多版本

当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。
在低压力时间段,先升级一半提供者为新版本
再将所有消费者升级为新版本
然后将剩下的一半提供者升级为新版本

  <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" />
上一篇 下一篇

猜你喜欢

热点阅读