Dubbo启动检查、负载均衡、多协议支持

本文详细介绍Dubbo服务启动检查配置、负载均衡策略及其配置方式,包括随机、轮询、最小活跃数和一致哈希等负载均衡算法。此外,还探讨了多协议支持,如dubbo、RMI和Hessian的不同特性和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、dubbo启动检查

在服务的消费者项目里,项目在启动过程中,默认如果检测到没有服务可供调用的话,就会报错。

  • 服务启动过程中验证服务提供者的可用性。
  • 验证过程出现问题,则阻止整个spring容器的初始化。
  • 服务启动检查可以尽可能早的发现服务问题。

在项目启动过程中,如果在服务消费者中不想去做服务检查,可以做如下配置;

通过 spring 配置文件

关闭某个服务的启动时检查 (没有提供者时报错):

<dubbo:reference interface="com.foo.BarService" check="false" />

关闭所有服务的启动时检查 (没有提供者时报错):

<dubbo:consumer check="false" />

关闭注册中心启动时检查 (注册订阅失败时报错):

<dubbo:registry check="false" />
通过 dubbo.properties
dubbo.reference.com.foo.BarService.check=false
dubbo.reference.check=false
dubbo.consumer.check=false
dubbo.registry.check=false
通过 -D 参数
java -Ddubbo.reference.com.foo.BarService.check=false
java -Ddubbo.reference.check=false
java -Ddubbo.consumer.check=false 
java -Ddubbo.registry.check=false
配置的含义

dubbo.reference.check=false,强制改变所有 reference 的 check 值,就算配置中有声明,也会被覆盖。

dubbo.consumer.check=false,是设置 check 的缺省值,如果配置中有显式的声明,如:<dubbo:reference check=“true”/>,不会受影响。

dubbo.registry.check=false,前面两个都是指订阅成功,但提供者列表是否为空是否报错,如果注册订阅失败时,也允许启动,需使用此选项,将在后台定时重试。

springboot中对单个服务配置
@Reference(interfaceClass = UserAPI.class,check = false)

二、 负载均衡

什么是负载均衡?

在集群服务环境中,为了是多个服务的调用,达到一种均衡状态的目的。

负载均衡策略
1、Random LoadBalance(随机负载均衡)
  • 随机,按权重设置随机概率
  • 在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。
2、RoundRobin LoadBalance(轮询负载均衡)
  • 轮询,按公约后的权重设置轮询比率。
  • 存在慢的提供者累计请求的问题: 比如:第二台机器很慢,但没挂,当请求调到第二台时,就卡在那,久而久之,所有请求都卡在调到的第二台上。
3、LeastActive LoadBalance (最小活跃数负载均衡)
  • 最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
  • 使慢的提供者收到更少请求,因为越慢的提供者的的调用前后计数差会越大。
4、ConsistentHash LoadBalance (一致哈希负载均衡)
  • 一致性 Hash,相同参数的请求总是发到同一提供者。
  • 当某一台提供挂时,原本发往该提供者的请求,基于虚拟节点,平摊其他提供者,不会引起剧烈变动。
Spring配置
服务端服务级别
<dubbo:service interface="..." loadbalance="roundrobin" />
客户端服务级别
<dubbo:reference interface="..." loadbalance="roundrobin" />
服务端方法级别
<dubbo:service interface="...">
    <dubbo:method name="..." loadbalance="
    "/>
</dubbo:service>
客户端方法级别
<dubbo:reference interface="...">
    <dubbo:method name="..." loadbalance="roundrobin"/>
</dubbo:reference>
springboot-dubbo 服务配置

客户端配置:

 @Reference(interfaceClass = UserAPI.class, loadbalance = "roundrobin")

服务端配置:

@Service(interfaceClass = UserAPI.class, loadbalance = "roundrobin")

三、多协议支持

Dubbo 允许配置多协议,在不同服务上支持不同协议或者同一服务上同时支持多种协议。

特性对比dubboRMIHessian
连接数单连接多连接多连接
连接方式长连接短连接短连接
传输协议TCP协议TCP协议HTTP协议
传输方式NIO异步传输同步传输同步传输
适用场景1、数据包较小 2、消费者个数多 3、常规方式1、数据包大小不一 2、消费者和提供者数量相差不大1、数据包大小不一 2、消费者和提供者数量相差不大
不同服务不同协议

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

<?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-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 
    <dubbo:application name="world"  />
    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
    <!-- 多协议配置 -->
    <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" /> 
</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-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
    <!-- 多协议配置 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:protocol name="hessian" port="8080" />
    <!-- 使用多个协议暴露服务 -->
    <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,hessian" />
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值