Hystrix熔断-Rest、Feign两种方式

Hystrix使用(RestTemplate)

1、引入hystrix依赖 
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	</dependency>
2、启动类:@EnableCircuitBreaker(@EnableHystrix均可)
3、@HystrixCommand(fallbackMethod = "sendFail")
4、fallback的请求参数和 返回参数 要和原方法一致。

commandProperties

@HystrixProperty(name = "fallback.enabled",value = "false")  //默认true

execution.isolation.strategy:隔离策略,默认为Thread

HystrixCommandProperties【统计相关、熔断器相关、信号量相关、其他】

Hystrix使用(Feign)

feign自带Hystrix,但是默认没有打开,首先打开Hystrix。(从Spring Cloud Dalston开始,feign的Hystrix 默认关闭,如果要用feign,必须开启)

feign:
  hystrix:
    enabled: true 
@FeignClient(name = "service-name",fallback = SmsClientFallback.class)
public class ClientFallback implements ServerClient 
@EnableFeignClients
@EnableCircuitBreaker

捕获熔断异常信息

restTemplate:
在fallback方法参数加上Throwable,获取throwable
Feign:
1、@FeignClient(name = “service-sms”,fallbackFactory = SmsClientFallbackFactory.class)
2、实现FallbackFactory

忽略异常

不走备用逻辑
1、继承HystrixBadRequestException
2、属性配置 @HystrixCommand(fallbackMethod = “sendFail”,
ignoreExceptions = {HystrixIgnoreException.class})

发布了25 篇原创文章 · 获赞 0 · 访问量 569

猜你喜欢

转载自blog.csdn.net/RaymondCoder/article/details/105257433