参考
springboot版本
2.2.1.RELEASE
使用hystrix
pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${spring.hystrix.version}</version>
</dependency>
启用
@SpringBootApplication(scanBasePackages = {"com.loading"})
@EnableHystrix //加入此行注解
public class WifiPushServerApplication {
public static void main(String[] args) {
SpringApplication.run(WifiPushServerApplication.class, args);
}
}
备注: 文档中给出的示例为加入 @EnableCircuitBreaker 注解,实际此处所使用的 @EnableHystrix 的注解中已经加入了 @EnableCircuitBreaker,具体可查看源码
使用
@HystrixCommand(fallbackMethod = "searchFallBack", commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")})
public void search(String param) {
//do stuff that might fail
}
public void searchFallBack(String param) {
//do other something
}
上段代码中调用search()
方法失败或超过设置的3000毫秒
时将会调用searchFallBack()
方法。具体详细属性查看hystrix wiki
使用hystrix dashboard
pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>${spring.hystrix.version}</version>
</dependency>
启用
@SpringBootApplication(scanBasePackages = {"com.loading"})
@EnableHystrix
@EnableHystrixDashboard //加入此行注解
public class WifiPushServerApplication {
public static void main(String[] args) {
SpringApplication.run(WifiPushServerApplication.class, args);
}
}
配置
加入配置
management.endpoints.web.exposure.include: hystrix.stream
启动
启动时在控制台会看到类似信息
2019-12-25 15:06:45.771 INFO 7743 --- [ main] o.s.b.a.e.web.ServletEndpointRegistrar : Registered '/actuator/hystrix.stream' to hystrix.stream-actuator-endpoint
并且访问网址host:port/hystrix
将会看到以下页面
在页面地址栏输入host:port/actuator/hystrix.stream
将会进入以下页面,即表示配置成功