搜索
您的当前位置:首页正文

Spring Cloud Hystrix(断路器)

来源:知库网

Hystrix:熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。

微服务存在的问题

在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能会导致服务崩溃。为了解决这一系列的问题,断路器等一系列服务保护机制出现了。

场景展现

image.png

端口为8082服务挂掉了并显示出错误信息
这样的显示对于用户来说是十分不好的,这样就需要引入断路器

实现断路器Hystrix

引入Spring Cloud Hystrix的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

使用 @EnableCircuitBreaker 注解开启断路器功能

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class SpringcloudConsumerRibbonApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

Controller调用方法的method加上@HystrixCommand注解,声明依赖服务调用延迟或失败时调用的方法

@RestController
public class RibbonController {
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/consumer")
    @HystrixCommand(fallbackMethod ="Failback")
    public String getMsg() {

        return restTemplate.getForObject("http://springcloud-eureka-client/client", String.class);
    }
    public String Failback(){
        return "服务中断连接,请联系管理员";
    }
}

配置文件可以不用改(有需要设置参数的可以参考文章下方的推荐文章)

整体项目结构(基本上可Ribbon的一样)

Eureka服务端 端口号是9090
两个Eureka客户端 端口号分别是8081和8082 相同的服务名springcloud-eureka-client
Ribbon消费者客户端 端口号是9999

启动服务进行测试

image.png
设置的服务中断回调方法使用了,说明断路器生效了

使用Hystrix-Dashboard实现hystrix服务监控功能

1.引入hystrix-dashboard依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2.在主类中添加@EnableHystrixDashboard注解开启Hystrix仪表盘

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableHystrixDashboard
public class SpringcloudConsumerRibbonApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

3.配置hystrix-metrics-event-stream
在整个监控统计过程中,最主要的是用到hystrix-metrics-event-stream
hystrix-metrics-event-stream:只要客户端连接还连着,hystrix-metrics-event-stream就会不断的向客户端以text/event-stream的形式推送计数结果(metrics)

使用hystrix-metrics-event-stream需要写个配置类

@Configuration
public class HystrixMetricsStreamConfig {
    @Bean
    public HystrixMetricsStreamServlet hystrixMetricsStreamServlet(){
        return new HystrixMetricsStreamServlet();
    }

    @Bean
    public ServletRegistrationBean registration(HystrixMetricsStreamServlet servlet){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(servlet);
        registrationBean.setEnabled(true);
        registrationBean.addUrlMappings("/hystrix.stream");
        return registrationBean;
    }
}

加上@Configuration注解声明为配置类
配置类中默认的监控路径是/hystrix.stream

如果不写的话是监控不了的


image.png image.png
输入监控的streamtitle,点击Monitor Stream,便会出现图形化的监控页面。
一般只需要填入监控地址就可以了,例如需要监控端口号为9999的服务
就填入

image.png
点击Monitor Stream,进去都看到Loading...,因为端口9999的服务还没调用
image.png
下面来访问下
image.png
便会出现图形化的监控页面,这样就实现了监控的效果了。
Top