Ribbon 整合Sentinel

本文介绍如何在SpringCloud项目中集成Sentinel,实现服务的熔断和降级功能。通过添加依赖、注解及配置,可以有效处理流量控制和异常情况,确保系统的稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:加配置
  <!‐‐ 加入 sentinel ‐‐>
< dependency >
3 < groupId > com . alibaba . cloud </ groupId >
4 < artifactId > spring cloud starter alibaba sentinel </ artifactId >
5 </ dependency >
6
7
8 <!‐‐ 加入 actuator ‐‐>
9 < dependency >
10 < groupId > org . springframework . boot </ groupId >
11 < artifactId > spring boot starter actuator </ artifactId >
12 </ dependency >
13
14 <!‐‐ 加入 ribbon ‐‐>
15 < dependency >
16 < groupId > org . springframework . cloud </ groupId >
17 < artifactId > spring cloud starter netflix ribbon </ artifactId >
18 </ dependency >
第二步:加注解(在我们的RestTemplate组件上添加
@SentinelRestTemplate注解)
并且我们可以通过在 @SentinelRestTemplate 同样的可以指定我们的
blockHandlerClass fallbackClass blockHandler fallback 这四个属性
1 @Bean
2 @LoadBalanced
3 @ SentinelRestTemplate (
4 blockHandler = "handleException" , blockHandlerClass = GlobalExceptionHand
ler . class ,
5 fallback = "fallback" , fallbackClass = GlobalExceptionHandler . class
6
7 )
8 public RestTemplate restTemplate () {
9 return new RestTemplate ();
10 }
11
12 13
14
15 ***************** 全局异常处理类
16 public class GlobalExceptionHandler {
17
18
19 /**
20 * 限流后处理方法
21 * @param request
22 * @param body
23 * @param execution
24 * @param ex
25 * @ return
26 */
27 public static SentinelClientHttpResponse handleException ( HttpRequest re
quest ,
28 byte [] body , ClientHttpRequestExecution execution , BlockException ex ) {
29
30 ProductInfo productInfo = new ProductInfo ();
31 productInfo . setProductName ( " 被限制流量拉 " );
32 productInfo . setProductNo ( "‐1" );
33 ObjectMapper objectMapper = new ObjectMapper ();
34
35 try {
36 return new SentinelClientHttpResponse ( objectMapper . writeValueAsString ( p
roductInfo ));
37 } catch ( JsonProcessingException e ) {
38 e . printStackTrace ();
39 return null ;
40 }
41 }
42
43 /**
44 * 熔断后处理的方法
45 * @param request
46 * @param body
47 * @param execution
48 * @param ex
49 * @ return
50 */
51 public static SentinelClientHttpResponse fallback ( HttpRequest request , 52 byte [] body , ClientHttpRequestExecution execution , BlockException ex ) {
53 ProductInfo productInfo = new ProductInfo ();
54 productInfo . setProductName ( " 被降级拉 " );
55 productInfo . setProductNo ( "‐1" );
56 ObjectMapper objectMapper = new ObjectMapper ();
57
58 try {
59 return new SentinelClientHttpResponse ( objectMapper . writeValueAsString ( p
roductInfo ));
60 } catch ( JsonProcessingException e ) {
61 e . printStackTrace ();
62 return null ;
63 }
64 }
65 }
第三步:添加配置
什么时候关闭:一般在我们的自己测试业务功能是否正常的情况,关闭该配置
1 # 是否开启 @SentinelRestTemplate 注解
2 resttemplate :
3 sentinel :
4 enabled : true
​​​​​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

迅捷的软件产品制作专家

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

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

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

打赏作者

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

抵扣说明:

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

余额充值