sentinel+dubbo如何实现dubbo远程调用异常时全局的降级或熔断
- Dominant language
- Java
- Stars
- 23.1k
- Forks
- 8.1k
- PR merge metrics
- No merged PRs in 30d
Description
> `sentinel+dubbo`如何实现dubbo远程调用异常时全局的降级或熔断,尝试过[sentinel-demo-dubbo](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-dubbo)无法实现全局`fallback`,测试代码如下
**测试环境**
* springcloud:Hoxton.SR12
* springcloudalibaba:2.2.7.RELEASE
* dubbo:2.7.13
* Sentinel:1.8.4
**消费者pom.xml**
```xml
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.csp
sentinel-apache-dubbo-adapter
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
com.alibaba.csp
sentinel-datasource-nacos
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter
com.alibaba.csp
sentinel-spring-cloud-gateway-adapter
```
**消费者ConsumerImpl**
```java
@Service
public class ConsumerServiceImpl implements IConsumerService {
@DubboReference
private IProductService productService;
@Override
@SentinelResource(value = "testHello")
public String hello(Long id) {
Random random = new Random();
productService.hello(id.toString());
String serialNum = String.valueOf(random.nextInt(1000));
return Thread.currentThread().getName() + "\t" + "调用成功,流水号为:" + serialNum;
}
}
```
**消费者DubboAdapterConfig**
全局fallback配置参考[Sentinel+dubbo配置](https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E6%B5%81%E6%A1%86%E6%9E%B6%E7%9A%84%E9%80%82%E9%85%8D#dubbo),文档中说明`用户只需要实现自定义的 DubboFallback 接口,并通过 DubboFallbackRegistry 注册即可`,我按如下代码注册结果不生效,注册方式有误还请大佬指正🙈
```java
@Configuration
public class DubboAdapterConfig {
private static final Logger logger = LoggerFactory.getLogger(DubboAdapterConfig.class);
@Bean
private static void registerFallback() {
DubboFallbackRegistry.setProviderFallback(new DubboFallback() {
@Override
public Result handle(Invoker invoker, Invocation invocation, BlockException ex) {
return new AppResponse("错误: " + ex.getClass().getTypeName());
}
});
}
}
```
---
**服务者pom.xml**
```xml
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.csp
sentinel-apache-dubbo-adapter
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
com.alibaba.csp
sentinel-datasource-nacos
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter
```
**服务者ProductServiceImpl**
```java
@DubboService
public class ProductServiceImpl implements IProductService {
@Override
public String hello(String str) {
return "结果:" + str;
}
}
```
分别启动消费者服务和服务者服务时,可以正常远程调用。当停掉服务者时,再次请求消费者时,控制台抛异常如下:
```bash
org.apache.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:8858 for service com.ran.member.api.service.IProductService on consumer 172.17.192.1 use dubbo version 2.7.13, please check status of providers(disabled, not registered or in blacklist).
at org.apache.dubbo.registry.integration.DynamicDirectory.doList(DynamicDirectory.java:168) ~[dubbo-2.7.13.jar:2.7.13]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP GET "/index?id=-1" [ExceptionHandlingWebHandler]
Stack trace:
```
请问要如何实现当服务者停机时,消费者再次请求`Sentinel`可以做全局降级/限流🤔
Contributor guide
Research direction
Start by reading sentinel-demo-dubbo and the DubboFallbackRegistry registration path, then trace how the consumer handles the shown RpcException when no provider is available. Compare that path with the @SentinelResource call and define a reproducible provider-shutdown test; done means the intended global fallback or throttling behavior is demonstrated or the unsupported path is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring, spring-boot
- Domain
- api, backend, distributed-systems
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100