一、雪崩效应
在微服务架构中,通常有多个服务层调用,如果某个服务不可用,造成调用的服务也不可用,造成整个系统不可用的情况,叫做雪崩效应
二、Hystrix
放雪崩利器Hystrix,基于Netflix对应的Hystrix。
Hystrix功能: 服务降级,服务熔断,依赖隔离, 监控(Hystrix Dashboard)
1、服务降级
优先核心服务,非核心服务不可用或弱可用
通过HystrixCommand注解指定
fallbackMethod(回退函数)中具体实现降级逻辑
三、Order工程中使用RestTemplate调用Product中的方法
@GetMapping("/getProductInfoList") public String getProductInfoList(){ RestTemplate restTemplate = new RestTemplate(); return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class); }
返回结果
当Product服务关闭后,再次访问,将返回连接拒绝
然后使用Hystrix进行服务降级
在Order服务中增加引用
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>RELEASE</version> </dependency>
增加注解EnableCircuitBreaker
或者用@SpringCloudApplication替换另外三个注解
增加HystrixController 类
@RestController public class HystrixController { @HystrixCommand(fallbackMethod = "fallback") @GetMapping("/getProductInfoList") public String getProductInfoList(){ RestTemplate restTemplate = new RestTemplate(); return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class); } private String fallback(){ return "太拥挤了,请稍后再试~~"; } }
再次调用,Product服务此时是关闭的。
说明服务降级了。
原文:https://www.cnblogs.com/linlf03/p/10392738.html