本文共 4178 字,大约阅读时间需要 13 分钟。
在实际使用过程中可能会遇到这样的情况,如果某个服务的提供者响应非常慢,那服务的消费者请求服务提供者就会产生超时的情况。如果不做处理,可能导致消费者的资源消耗,甚至拖垮整个系统。以之前的用户和电影为例,电影微服务从用户微服务获取用户信息,假如此时用户微服务网络很微弱,电影微服务在请求用户信息时就会进入一种卡死状态,此时如果消费者发起多个连接都没有响应,可能会导致电影服务的不可用,这种状态如果持续时间很长会导致整个系统的崩溃。
雪崩效应的形成,如下图所示:
这种因“基础服务故障”导致的“级联故障”的现象称为“雪崩效应”。雪崩效应描述的是服务提供者不可用导致消费者不可用,并将不可用逐渐放大的过程。
关于如何容错的相关知识,请参考:
Hystrix是由Netflix开源的一个延迟和容错库,用户隔离访问系统、服务或者第三方库,防止级联失败,从而提升系统的可用性与容错性。Hystrix主要通过以下几点实现延迟和容错。
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-hystrix', version: '1.4.7.RELEASE'
@EnableHystrix
,为项目启动断路器的支持import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClients@EnableHystrixpublic class MicroMovieServiceApplication { public static void main(String[] args) { SpringApplication.run(MicroMovieServiceApplication.class, args); }}
getUserByUserId()
方法,具备容错能力@Controllerpublic class MovieController { private static final Logger logger = LoggerFactory.getLogger(MovieController.class); @Bean @LoadBalanced RestTemplate loadBalancedRestTemplate() { return new RestTemplate(); } @Autowired private RestTemplate restTemplate; @Autowired private LoadBalancerClient loadBalancerClient; @Autowired private UserFeignClient userFeignClient; @ResponseBody @HystrixCommand(fallbackMethod = "getUserFallBackByUserId") @RequestMapping(value = "/movie/{id}", method = { RequestMethod.GET}) public BizResult getUserByUserId(@PathVariable Long id){ ServiceUser user = userFeignClient.findById(id); BizResult bizResult = BizResult.success(); bizResult.setMessage("查询成功"); bizResult.setData(user); return bizResult; } public BizResult getUserFallBackByUserId(Long id){ BizResult bizResult = new BizResult(); bizResult.setMessage(String.format("获取id为%d的用户失败",id)); bizResult.setRetCode(-1); return bizResult; }}
http://127.0.0.1:8880/movie/1
http://127.0.0.1:8880/actuator/hystrix.stream
查看Hystrix的节点信息,第一次访问出现了404的问题,在aaplication.yml中添加以下代码,即可解决:management: endpoints: web: exposure: include: "*"
http://127.0.0.1:8880/actuator/hystrix.stream
,浏览器结果如下: http://127.0.0.1:8880/movie/1
后在访问http://127.0.0.1:8880/actuator/hystrix.stream
,结果如下: 以上内容一直循环,这是因为系统会不断刷新以获得实时的监控数据。Hystrix的监控非常全面,例如HystrixCommand的名称,group的名称,断路器状态,错误率,错误数根据上面的数据结果,发现虽然能够显示监控结果,但是数据显示不直观,所以可使用图形化的Hystrix Dashboard让监控数据图形化,可视化
micro-hystrix-service-dashboard
项目compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix-dashboard', version: '2.1.2.RELEASE'
@SpringBootApplication@EnableHystrixDashboardpublic class HystrixDashboarServicedApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboarServicedApplication.class, args); }}
server: port: 8000
micro-hystrix-service-dashboard
,访问http://127.0.0.1:8000/hystrix
,可看到Hystrix Dashboard的首页 http://127.0.0.1:8880/actuator/hystrix.stream
,可通过图形化界面显示之前浏览器展示的信息。其各项含义如下图所示 更多内容可访问 转载地址:http://ejhnn.baihongyu.com/