服务超时

超时可在应用、服务、方法级别设置:

  1. <dubbo:consumer timeout="5000"/>
  2. <!-- 声明需要暴露的服务接口 -->
  3. <dubbo:reference id="goodsService" interface="org.blackist.dist.service.GoodsService">
  4. <dubbo:method name="getGoods" timeout="2000"/>
  5. </dubbo:reference>
  6. <dubbo:reference id="userService" interface="org.blackist.dist.service.UserService" timeout="4000"/>

提供者和消费者在同一级别设置超时,消费者配置优先级。

Ref: https://blog.csdn.net/qq_42374233/article/details/110739460

  1. 同一个级别,消费者 > 提供者
  2. 消费者方法 > 提供者方法 > 消费者接口级别 > 提供者接口级别 > 消费者全局级别 > 提供者全局级别

推荐配置提供者(提供者更了解服务本身)

启动时检查

默认启动时检查,先启动消费者会失败:
Caused by: java.lang.IllegalStateException: Failed to check the status of the service org.blackist.dist.service.GoodsService. No provider available for the service org.blackist.dist.service.GoodsService
通过 check 配置是否启动时检查,应用级别和服务级别:

 <dubbo:consumer timeout="5000" check="false"/>
 <!-- 声明需要暴露的服务接口 -->
 <dubbo:reference id="goodsService" interface="org.blackist.dist.service.GoodsService"
     check="false"/>

重试机制

和超时机制配合,超时后默认重试两次。可在应用级别、服务级别和方法级别配置(就近生效)。

 <dubbo:consumer timeout="5000" retries="3"/>
 <!-- 声明需要暴露的服务接口 -->
 <dubbo:reference id="goodsService" interface="org.blackist.dist.service.GoodsService"
                  check="false" retries="4">
   <dubbo:method name="getGoods" timeout="2000" retries="5"/>
 </dubbo:reference>

灰度发布

先让一半消费者调用新服务,再让另一半调用新服务。通过 version 在提供者配置新版本服务:





在消费者通过 version 配置调用得版本( 表示随机调用):
timeout=”4000” check=”false” version=”
“/>

本地存根

提供者在消费者方执行部分逻辑:做 ThreadLocal 缓存、提前验证参数、调用失败后伪造容错数据等。
此时就需要在 API 中带上 Stub,客户端生成 Proxy 实例,会把 Proxy 通过构造函数传给 Stub [1],然后把 Stub 暴露给用户,Stub 可以决定要不要去调 Proxy。
在消费者方配置:

<dubbo:reference id="userService" interface="org.blackist.dist.service.UserService"
     timeout="4000" check="false" version="*" stub="org.blackist.dist.service.stub.UserServiceStub"/>

Stub 的实现:

public class UserServiceStub implements UserService {
    private UserService userService;
    public UserServiceStub(UserService userService) {
        this.userService = userService;
    }
    @Override public List<UserAddress> getUserAddressList(String userId) {
        try {
            return userService.getUserAddressList(userId);
        } catch (Exception e) {
            return Collections.emptyList();
        }
    }
 }

Zookeeper 宕机与 Dubbo 直连

Dubbo 会缓存服务信息, Zookeeper 宕机后运行着的消费者依然可以调用 Dubbo 服务。
# 宕机时提供者和消费者失去和注册中心的连接
java.net.ConnectException: Connection refused
# 注册中心恢复后消费者和提供者重连
State change: RECONNECTED

负载均衡配置

Dubbo 提供了多种均衡策略,缺省为 random 随机调用。

  • Random LoadBalance
  • RoundRobin LoadBalance
  • LeastActive LoadBalance
  • ConsistentHash LoadBalance

修改 JVM 参数启动3个 Dubbo 服务:
在消费者方配置负载均衡:

 <dubbo:reference id="goodsService" interface="org.blackist.dist.service.GoodsService"
                  check="false" retries="4" loadbalance="roundrobin">
 </dubbo:reference>

服务降级和服务容错

服务容错可以用本地存根代替,服务降级使用 Hystrix 实现。
双方添加依赖:

 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
   <version>2.1.3.RELEASE</version>
 </dependency>

双方添加 Hystrix 注解配置:
@EnableHystrix
消费者端 Service 实现添加 fallback 配置:

@HystrixCommand(fallbackMethod = "getGoodsFallback")
public List<Goods> getGoods() {
    return goodsService.getGoods();
}

public List<Goods> getGoodsFallback() {
    return Collections.singletonList(new Goods("getGoods fallback", 1L));
 }

dubbo 端口

缺省端口: https://dubbo.apache.org/zh/docs/advanced/hostname-binding/
随机端口 / 可用端口 设置: https://blog.csdn.net/qq_22860341/article/details/86629238