spring-cloud微服务项目实战(7)- 集成Hystrix容错机制

目标

集成Hystrix容错机制,集成hystrix-dashboard进行监控管理

简介

Hystrix是为了防止分布式架构中雪崩效应问题,主要实现功能有以下两点:

  1. 网络请求设置超时时间,减少资源浪费
  2. 断路器模式,类似家庭中的保险丝,流程图如下:


    断路器实现模式

开工集成

  1. 添加hystrix依赖,同时我们需要开启hystrix-dashboard监控功能,其原理依据spring-boot-starter-actuator健康检查,所以同时在client中添加spring-boot-starter-actuator依赖
          
            org.springframework.cloud
            spring-cloud-starter-hystrix
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        
  1. 启动类添加hystrix注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "open.template.work.udm.client.feign")
@EnableHystrix
public class UdmClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(UdmClientApplication.class, args);
    }
    @Bean
    public IRule ribbonRoundRobinRule() {
        return new RandomRule();
    }
}
  1. application.yml开启Hystrix功能
feign:
  hystrix:
    enabled: true
  1. 在feign接口中添加容错处理类
package open.template.work.udm.client.feign;

import feign.hystrix.FallbackFactory;
import open.template.work.commons.constants.CloudServerDirectory;
import open.template.work.commons.vo.udm.request.TaskRequestVo;
import open.template.work.commons.vo.udm.resposne.TaskReponseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = CloudServerDirectory.UDM_SERVER,fallbackFactory = FeignCLientFallbackFactory.class)
public interface UdmServerTaskFegin {

    @PostMapping("/task/get")
    TaskReponseVo getTaskInfo(@RequestBody TaskRequestVo vo);
}

@Component
class FeignCLientFallbackFactory implements FallbackFactory{

    private static final Logger  LOGGER=LoggerFactory.getLogger(FeignCLientFallbackFactory.class);

    @Override
    public UdmServerTaskFegin create(Throwable throwable) {

        return new  UdmServerTaskFegin(){
            @Override
            public TaskReponseVo getTaskInfo(TaskRequestVo vo) {
                LOGGER.info("udm server fallback,reason was:{}",throwable==null?"空异常":throwable.getMessage());
                TaskReponseVo responseVo=new TaskReponseVo();
                responseVo.setStatus("0");
                responseVo.setTaskId("0");
                return responseVo;
            }
        };
    }
}
  1. 这样启动客户端后,关闭服务端测试


    image.png

在看接口返回结果


正是我们默认返回的结果
  1. 但上面只是单次错误的降级,并没有触发断路器,只有当单位时间内达到错误阈值时,断路器才会真正打开,我们可以通过/health查看hystrix状态,注意actuator一些敏感信息不会默认展示,此时health查看的数据可能不完整,需要在application.yml中添加配置才可看到
management:
  security:
    enabled: false

此时查看/health


image.png

可见hystrix是up状态,显示运行正常,表示断路器并没有启用,我们此时关闭对应的所有提供者,多次调用测试


断路器此时

这是断路器属于开启状态,此时重启服务提供者,过段时间后,断路器会自动关闭。
  1. 下面集成hystrix-dashboard,hystrix-dashboard是基于spring-boot-starter-actuator的数据进行可视化的监控,需要我们重新建立个module,


    image.png

依赖中添加

        
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
        

入口添加支持

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }

}

application.yml指定端口

server:
  port: 9000

启动服务,访问服务http://localhost:9000/hystrix

image.png

第一行url输入我们要查看的服务地址,uri为hystrix,stream,设置延时和标题,启动
image.png

这样就可以监控到此服务的错误熔断情况。

  1. 上面的dashboard是对单应用的监控,如果需要同时监控多个微服务就需要turbine技术,其原理就是将指定的若干个hystrix.stream进行整合展示,本章不作为重点讨论内容,有需要可以参考https://www.cnblogs.com/leeSmall/p/8847652.html

参考资料: https://www.cnblogs.com/leeSmall/p/8847652.html

你可能感兴趣的:(spring-cloud微服务项目实战(7)- 集成Hystrix容错机制)