SpringCloud----服务降级,熔断Hystrix和ribbon重试
三、服务降级
3.1 基于Ribbon的服务降级—Hystrix
3.1.1 添加Hystrix依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
@SpringBootApplication @EnableDiscoveryClient @EnableHystrix public class GoodsUiApplication { public static void main(String[] args) { SpringApplication.run(GoodsUiApplication.class, args); } }
/** * 访问goods-provider服务的“goods/{id}” */ @HystrixCommand(fallbackMethod = "fallback_getGoods",commandProperties = { @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1000") }) public Goods getGoods(String goodsId){ //2.接收对象数据类型的返回 Goods goods = restTemplate.getForObject("http://goods-provider/goods/"+goodsId, Goods.class); return goods; } public Goods fallback_getGoods(String goodsId){ //return new Goods("0","服务降级返回的商品",0.00); return null; }
3.2.1 配置yml
feign:
hystrix:
enabled: true
3.2.2 在服务调用方中配置降级服务
//服务的调用如果失败,则调用fallback指定的类中的同名方法 @FeignClient(value = "order-provider", fallback = OrderUIServiceFallback.class) public interface OrderUIService { @RequestMapping(value = "order/list",method = RequestMethod.GET) public List<Order> listOrders(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize); @RequestMapping(value = "order/add",method = RequestMethod.POST) public ResultVO doAdd(Order order); @RequestMapping(value = "order/add",method = RequestMethod.POST) public ResultVO test(@RequestBody Order order, @RequestParam("pageNum") Integer num, @RequestParam("pageSize") Integer size); }
3.2.3 定义fallback类
@Component public class OrderUIServiceFallback implements OrderUIService { @Override public List<Order> listOrders(Integer pageNum, Integer pageSize) { //降级服务 return null; } @Override public ResultVO doAdd(Order order) { //降级服务 return null; } @Override public ResultVO test(Order order, Integer num, Integer size) { //降级服务 return null; } }
3.2.4 设置超时时间
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000
3.3 在服务提供者配置服务降级—Hystrix
-
添加Hystrix依赖
-
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
-
@SpringBootApplication @EnableEurekaClient @EnableHystrix public class GoodsProviderApplication { public static void main(String[] args) { SpringApplication.run(GoodsProviderApplication.class, args); } }
- 在服务提供者上配置服务降级方案
-
@RequestMapping(value = "/{id}" , method = RequestMethod.GET) @HystrixCommand(fallbackMethod = "fallback_get",commandProperties = { @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1000") }) public Goods get(@PathVariable("id") String gid){ if(!"101".equals(gid)){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } return new Goods(gid,"查询的商品",33.33); } public Goods fallback_get(@PathVariable("id") String gid){ return new Goods("0","这是服务降级方案响应的商品信息",0.0); }
2.3.2 服务熔断实现
-
导入hystrix依赖
-
启动类添加
@EnableHystrix
或者@CircuitBreaker
注解
-
提供服务的方法
@RequestMapping(value = "/test",method = RequestMethod.GET) //服务熔断配置 @HystrixCommand(fallbackMethod = "fallback_test",commandProperties = { @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1000"), @HystrixProperty(name="circuitBreaker.enabled",value="true"), @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="10000"), //时间 @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="10"), //请求次数 @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="50"), //服务错误率 }) public String test(@RequestParam("num") Integer num){ int n = (int)Math.floor(Math.random()*100); int r = n/num; if(num>10){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } return "服务正常返回的结果:"+n+"/"+num+"="+r; } public String fallback_test(@RequestParam("num") Integer num){ return "服务降级响应:num不能为0"; }
2.3.5 服务消费者实现服务熔断
-
Ribbon调用服务的方法
-
@HystrixCommand(fallbackMethod = "fallback_getGoods",commandProperties = { @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="100"), @HystrixProperty(name="circuitBreaker.enabled",value="true"), @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="10000"),//时间 @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="10"), //请求次数 @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="50"), //服务错误率 }) public Goods getGoods(String goodsId){ //2.接收对象数据类型的返回 Goods goods = restTemplate.getForObject("http://goods-provider/goods/"+goodsId, Goods.class); return goods; } public Goods fallback_getGoods(String goodsId){ return null; }
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000
circuitBreaker:
enabled: true
## ...
2.5 Ribbon的负载均衡和重试机制
2.5.1 Ribbon负载均衡
当服务提供者是集群部署时,服务消费者对服务提供者进行基于Ribbon的调用时,Ribbon是实现了负载均衡的
2.5.2 Ribbon的重试机制
当服务消费者通过Ribbon进行服务调用时,如果服务提供者第一次不能正常的响应,Ribbon可以重新发送(一次)请求
ribbon默认开始了重试机制,重试次数1次,其他节点2次,连接超时时间1s,请求响应等待时间1s
ribbon:
ConnectTimeout: 1000
ReadTimeout: 1000
OkToRetryOnAllOperations: true
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 2
2.5.3 Feign的超时设置
-
hystrix超时时间 与 ribbon超时时间的关系
yml设置
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 3000
OkToRetryOnAllOperations: false
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步