Sentinel使用

介绍

Sentinel是阿里开源的一套用于服务容错的综合性解决方案。主要以流量为切入点,从流量控制,熔断降级,系统负载保护等多个维度来维护系统的稳定性。

 

Sentinel使用

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }

  

    @SentinelResource(value = KEY, blockHandler = "blockHandlerMethod", fallback = "queryGoodsInfoFallback")
    public String queryGoodsInfo(String spuId) {

        // 模拟调用服务出现异常
        if ("0".equals(spuId)) {
            throw new RuntimeException();
        }

        return "query goodsinfo success, " + spuId;
    }

    public String blockHandlerMethod(String spuId, BlockException e) {
        log.warn("queryGoodsInfo222 blockHandler", e.toString());
        return "queryGoodsInfo error, blockHandlerMethod res: " + spuId;

    }

    public String queryGoodsInfoFallback(String spuId, Throwable e) {
        log.warn("queryGoodsInfo222 fallback", e.toString());
        return "queryGoodsInfo error, return fallback res: " + spuId;
    }

  

发起请求

http://127.0.0.1:8071/goods/queryGoodsInfo?spuId=0

    @GetMapping("queryGoodsInfo")
    public ResponseEntity<String> hello(@RequestParam("spuId") String spuId) {
        log.info("-------, 调用queryGoodsInfo接口");
        String res = goodsQueryService.queryGoodsInfo(spuId);
        return ResponseEntity.ok(res);
    }

  

 

 

posted on 2021-12-03 17:04  rabbit-xf  阅读(135)  评论(0)    收藏  举报