Spring Cloud(三):断路器Hystrix和Hystrix Dashboard
Netflix创造了一个可调用的库来实现断路器模式,即Hystrix组件,在微服务架构中,一个API网关控制请求发送到不同的服务中,如下图所示,各个客户端通过API网关发送请求到各个服务
当一个服务挂掉的时候,会导致通信出现问题。当请求这个服务的次数达到一定阈值时(Hystrix中的默认值为5秒内出现20次故障),电路打开,不进行通话。在错误和开路的情况下,开发人员可以进行服务的修复。
同时Hystrix提供Fallback(回滚)的作用,防止级联故障。接下来展示如何在SpringCloud中集成Hystrix组件
-
配置Hystrix
在新建模块的时候加入以下依赖,其中actuator包是Hystrix监控必须的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类上加上@EnableHystrix注解,表示开启Hystrix监控
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@EnableHystrix
public class EurekaConsumerHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerHystrixApplication.class, args);
}
}
配置文件如下,其中feign.hystrix.enabled表示开启监控,这个属性配置时又没提示,很容易会以为是错的OAO
spring:
application:
name: eureka-consumer-hystrix
server:
port: 8763
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
feign:
hystrix:
enabled: true
在上一个项目中以及写了Feign调用服务的接口,这次新建一个实现类,重写message方法,该方法是用在断路开路情况下调用的方法
@Component
public class HystrixRemote implements FeignRemote {
@Override
public String clientMessage(@RequestParam("name") String name) {
return name + " this message sending failed";
}
}
依次启动server、client、HystrixApplication
访问http://localhost:8763/message/hong,返回信息如下
然后我们停止client服务,断路器Hystrix就会起作用,并且返回当服务断路开路时要调用的方法
-
Hystrix Dashboard
Hystrix Dashboard是Hystrix提供的监控面板,是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。
在刚才的项目上修改,新增dashboard依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
然后是启动类上增加@EnableHystrixDashboard注解
@SpringBootApplication
@EnableHystrixDashboard
@EnableEurekaClient
@EnableFeignClients
public class HystrixDashboardTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardTurbineApplication.class, args);
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
这里多加了一个servlet,是因为SpringBoot1.x版本和2.x版本的不同,如果按照1.x的版本的各个教程去启动,就会导致dashboard页面无法连接到服务的错误,即红色字体的hystrix dashboard Unable to connect to Command Metric Stream,在源码中体现为HystrixStreamEndpoint ——>HystrixMetricsStreamServlet 。其中的description就告诉我们需要增加这个HystrixMetricsStreamServlet的映射才能保证hystrix的正常运行
/**
* Streams Hystrix metrics in text/event-stream format.
* <p>
* Install by:
* <p>
* 1) Including hystrix-metrics-event-stream-*.jar in your classpath.
* <p>
* 2) Adding the following to web.xml:
* <pre>{@code
* <servlet>
* <description></description>
* <display-name>HystrixMetricsStreamServlet</display-name>
* <servlet-name>HystrixMetricsStreamServlet</servlet-name>
* <servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class>
* </servlet>
* <servlet-mapping>
* <servlet-name>HystrixMetricsStreamServlet</servlet-name>
* <url-pattern>/hystrix.stream</url-pattern>
* </servlet-mapping>
* } </pre>
*/
public class HystrixMetricsStreamServlet extends HystrixSampleSseServlet {
.......
}
配置文件上新增endpoint指向属性
spring:
application:
name: eureka-consumer-hystrix
server:
port: 8764
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
exclude: hystrix.stream
然后就可以打开http://localhost:8764/hystrix,看到如下画面
如果查看默认集群使用第一个url,查看指定集群使用第二个url,单个应用的监控使用第三个,这里我们选第三个,输入url:http://localhost:8764/hystrix.stream
发现都处于loading状态,因为没有请求发送,这时我们访问http://localhost:8764/message/hong,发送请求,就可以看见监控画面了
下图是对这个图形界面的解释
demo地址:https://github.com/hong52ni/SpringCloud-Demo-Aggregate