SpringCloud学习-part61 sentinel服务熔断与fallback

消费者文件结构

SpringCloud学习-part61 sentinel服务熔断与fallback_第1张图片

消费者controller

@RestController
@Slf4j
public class CircleBreakerController {

    @Value("${service-url.nacos-user-service}")
    private String SERVICE_URL ;

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/fallback/{id}")
    //@SentinelResource(value = "fallback") //没有配置
    //@SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback只负责业务异常
    //@SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler只负责sentinel控制台配置违规
    @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler",
            exceptionsToIgnore = {IllegalArgumentException.class})
    public CommonResult fallback(@PathVariable Long id)
    {
        CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);

        if (id == 4) {
            throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return result;
    }
    //本例是fallback
    public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(444,"兜底异常handlerFallback,exception内容  "+e.getMessage(),payment);
    }
    //本例是blockHandler
    public CommonResult blockHandler(@PathVariable  Long id, BlockException blockException) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException  "+blockException.getMessage(),payment);
    }
}

消费者Pom

nacos服务发现依赖

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

sentinel的启动依赖

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        

消费者yml

erver:
  port: 84
#消费者将要去访问的微服务名称(成功注册入nacos的微服务提供者),用于resttemplate调用前的属性注入
service-url:
  nacos-user-service: http://nacos-payment-provider
management:
  endpoints:
    web:
      exposure:
        include: '*'

服务者controller

@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    public static HashMap hashMap = new HashMap<>();
    static
    {
        hashMap.put(1L,new Payment(1L,"28a8c1e3bc2742d8848569891fb42181"));
        hashMap.put(2L,new Payment(2L,"bba8c1e3bc2742d8848569891ac32182"));
        hashMap.put(3L,new Payment(3L,"6ua8c1e3bc2742d8848569891xt92183"));
    }

    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult paymentSQL(@PathVariable("id") Long id)
    {
        Payment payment = hashMap.get(id);
        CommonResult result = new CommonResult(200,"from mysql,serverPort:  "+serverPort,payment);
        return result;
    }
}

限流

SpringCloud学习-part61 sentinel服务熔断与fallback_第2张图片

测试

当id等于4时

由于被以下设定导致非法参数异常被忽略:
exceptionsToIgnore = {IllegalArgumentException.class}
所以会直接抛出error页面:
SpringCloud学习-part61 sentinel服务熔断与fallback_第3张图片
当id等于5时,若QPS小于等于1则,正常显示CommonResult
在这里插入图片描述
当id等于5时,若QPS大于1,则优先被限流
在这里插入图片描述

你可能感兴趣的:(SpringCloud)