springCloud-40 restTemplate 整合sentinel 实现熔断

在基于sentinel 的服务保护上一文中,我们通过restTemplate来请求服务提供者,然后通过@SentinelResource 来使用sentinel的降级,如果我们要调用多个http 请求,就需要配置多个降级方法,代码臃肿。所以可以配置一个公用的降级,熔断方法。


@GetMapping("/buy/{id}")
@SentinelResource(value="order",blockHandler = "orderblockHandler",fallback
= "orderfallback")
public Product order(@PathVariable Long id) {
    return restTemplate.getForObject("http://shop-service-
    product/product/1", Product.class);
}

SpringCloud-38 基于Sentinel的服务保护_阿涩的博客-CSDN博客因为sentinel 的功能就是保护服务的可用,有流量控制,线程数隔离,限流,熔断,降级,种种都是为了系统的可用性。下面我们来简单的使用一下sentinel一, 首先还是加入依赖和配置yml文件详情可以通过上篇文章查看。二,设置熔断方法2.1 编写代码在服务消费者添加熔断方法,主要是使用sentinel的@SentinelResource 注解,参数有@GetMapping("/buy/{id}")@SentinelResource(value="ord..https://blog.csdn.net/qq_41169544/article/details/122625813

 一,在restTemplate 的bean 上加上注解

Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造
RestTemplate bean 的时候需要加上 @SentinelRestTemplate 注解。
  • @SentinelRestTemplate 注解的属性支持限流( blockHandler , blockHandlerClass )和降级 ( fallback , fallbackClass )的处理。
  • 其中 blockHandler fallback 属性对应的方法必须是对应 blockHandlerClass fallbackClass 属性中的静态方法。
  • 该方法的参数跟返回值跟org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 方法一致,其中参数多出了一个 BlockException 参数用于获取 Sentinel 捕获的异常。

@SpringBootApplication
@EntityScan("com.zjk.feignHystrix.entity")
@EnableEurekaClient
//通过@EnableFeignClients 激活feign
@EnableFeignClients
//激活hystrix
@EnableHystrix
@EnableCircuitBreaker
@EnableHystrixDashboard
public class OrdFeignSentinelApplication {

    @Bean
    @LoadBalanced
    @SentinelRestTemplate(fallback = "handleFallback",fallbackClass = ExceptionUtil.class, blockHandler="handleBlock",blockHandlerClass=ExceptionUtil.class)
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(OrdFeignSentinelApplication.class,args);
    }
}
比如上述 @SentinelRestTemplate 注解中 ExceptionUtil handleException 属性对应的方法
声明如下:
public class ExceptionUtil {

    //限流熔断业务逻辑
    public static SentinelClientHttpResponse handleBlock(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        System.err.println("Oops: " + ex.getClass().getCanonicalName());
        return new SentinelClientHttpResponse("限流熔断降级");
    }
    //异常熔断业务逻辑
    public static SentinelClientHttpResponse handleFallback(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        System.err.println("fallback: " + ex.getClass().getCanonicalName());
        return new SentinelClientHttpResponse("异常熔断降级");
    }
}
Sentinel RestTemplate 限流的资源规则提供两种粒度:
  • httpmethod:schema://host:port/path :协议、主机、端口和路径
  • httpmethod:schema://host:port :协议、主机和端口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vegetari

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值