SpringCloud sentinel服务熔断 服务降级
在https://github.com/alibaba/Sentinel/releases下载最新版本sential jar包
打开cmd,输入java -jar xxx.jar 启动sentinel 服务端
通过观察cmd 日志输出sentinel基于springboot 2.5 开发
INFO: Sentinel log output type is: fileINFO: Sentinel log charset is: utf-8INFO: Sentinel log base directory is: C:\\Users\\logs\\csp\\INFO: Sentinel log name use pid is: falseINFO: Sentinel log level is: INFO . ____ _ __ _ _ /\\\\ / ___\'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\( ( )\\___ | \'_ | \'_| | \'_ \\/ _` | \\ \\ \\ \\ \\\\/ ___)| |_)| | | | | || (_| | ) ) ) ) \' |____| .__|_| |_|_| |_\\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.12)
打开
http://localhost:8080/ 用户名密码都是sentinel 打开控制台页面http://localhost:8080/#/dashboard/flow/demo1springboot项目引入依赖
spring-cloud-starter-alibaba-sentinel
坐标参考
com.alibaba.cloud spring-cloud-starter-alibaba-sentinel 2023.0.3.3
properties增加配置:
spring.cloud.sentinel.transport.dashboard= localhost:8080spring.cloud.sentinel.transport.port= 8719feign.sentinel.enabled=true
在service 中@FeignClient增加项,fallbackFactory 指向TestfallbackFactory对象
@FeignClient(name = \"demo1\",fallbackFactory = TestFallbackFactory.class,configuration = FeignConfig.class)public interface FeignService { @GetMapping(value = \"test\") User test();}@Componentpublic class TestFallbackFactory implements FallbackFactory { @Override public FeignService create(Throwable cause) { return new FeignService() { @Override public User test() { System.out.println(\"fallback\"); return new User(); } }; }}@Configurationpublic class FeignConfig { @Bean Logger.Level feignLoggerLevel() { // 设置日志级别为FULL以获取最详细的日志信息 return Logger.Level.FULL; }}
至此,客户端就配置好了
服务端打开http://localhost:8080/
点击簇点链路菜单
对调用的微服务设置并发1 使之进入fallback
打开浏览器调用t1方法 一顿狂刷新
可以看到有时候返回正常
有时候返回fallback设置的对象
测试正常,超过并发1进入fallback,否则正常返回