阿里巴巴sentinel熔断降级(请求方是浏览器、请求方是应用两种方式考虑)

一:当请求方式浏览器的时候,你不能直接返回sentinel默认的报错信息吧?如下是sentinel默认的报错信息:

Blocked by Sentinel (flow limiting)

上面的报错肯定不友好。需要对流控的报错信息进行处理。

可以在Controller的方式加上@SentinelResource(blockHandler = "blockHandlerMethod")

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

@GetMapping("/sayHello")
@SentinelResource(value = "fallback",blockHandler = "blockHandlerMethod")
public String sayHello(){
int i=1/0;
return "hello world";
}


public String blockHandlerMethod(){
return "服务暂时不可用";
}
}

二:如果是服务A调用服务B,那么其实可以在服务A判断异常信息,

如果是sentinel返回的熔断异常信息,可以用下面的方法判断。

com.alibaba.csp.sentinel.slots.block.BlockException#isBlockException

可以实现如下接口,进行降级处理:org.springframework.cloud.openfeign.FallbackFactory

原文地址:https://www.cnblogs.com/mkl34367803/p/14646304.html