栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springCloud-40 restTemplate 整合sentinel 实现熔断

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 Alibabasentinel 支持对 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 :协议、主机和端口

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号