@EnableCircuitBreaker注解
使用@EnableCircuitBreaker注解开启断路器功能,另外@SpringCloudApplication注解也包含@EnableCircuitBreaker注解,以下为@SpringCloudApplication注解的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}
引入Spring Cloud Hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
方法一:实现HystrixCommand
public class HelloHystrix extends HystrixCommand{
@Autowired
RestTemplate restTemplate;
protected HelloHystrix(String threadKeyName, int timeout, int queueSize, int coreSize) {
super(Setter.withGroupKey(
//设置命令组
HystrixCommandGroupKey.Factory.asKey("springHealthGrooup"))
//设置命令键
.andCommandKey(HystrixCommandKey.Factory.asKey("interventionKey"))
//设置线程池键
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(threadKeyName))
//设置命令属性
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(timeout)
)
.andThreadPoolPropertiesDefaults(
HystrixThreadPoolProperties.Setter()
Spring Cloud Hystrix:断路器与命令处理实战

最低0.47元/天 解锁文章
529

被折叠的 条评论
为什么被折叠?



