Spring Cloud使用Sentinel做为Feign的熔断器之Fallback统一处理

参考博客:https://blog.csdn.net/ttzommed/article/details/90669320


原文中编写FunSentinelFeign类时,由于SentinelInvocationHandler方法的构造函数并不是public修饰
在这里插入图片描述
return new SentinelInvocationHandler(target, dispatch, funFallbackFactory),会报错。修改为使用反射调用

//获取指定的构造方法
                    Constructor<SentinelInvocationHandler> constructor = SentinelInvocationHandler.class.
                            getDeclaredConstructor(Target.class, Map.class, FallbackFactory.class);
                    constructor.setAccessible(true);
 return constructor.newInstance(target, dispatch, new FallbackFactory.Default(fallbackInstance));

demo地址:gitee

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Alibaba Sentinel 是一个流量控制、熔断降级框架,主要用于保护微服务应用程序的稳定性和可靠性。下面是 Sentinel 使用的简单步骤: 1. 在项目的 pom.xml 文件中添加 Sentinel 的依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 2. 在启动类上添加 @EnableDiscoveryClient、@EnableFeignClients、@EnableCircuitBreaker、@EnableHystrixDashboard、@EnableSentinelDashboard 注解。 ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker @EnableHystrixDashboard @EnableSentinelDashboard public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 在 application.yml 中配置 Sentinel: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 ``` 其中 `localhost:8080` 是 Sentinel Dashboard 的地址。 4. 在 Controller 或 Service 的方法上添加 Sentinel 注解,例如: ```java @GetMapping("/hello") @SentinelResource(value = "hello", blockHandler = "blockHandler") public String hello() { return "Hello World"; } public String blockHandler(BlockException ex) { return "Blocked by Sentinel"; } ``` 其中 `@SentinelResource` 注解用于定义资源名称,并且可以指定 blockHandler 方法,当资源被限流或降级时,将会调用该方法。 5. 启动 Sentinel Dashboard,使用浏览器访问 `http://localhost:8080` 即可查看 Sentinel 监控面板。 以上是 Spring Cloud Alibaba Sentinel 的简单使用步骤,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值