springcloud feign增加熔断器Hystrix

1.依赖

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>

2.springboot启动类上不需要额外加注解,feignclient注解默认支持hystrix

@EnableFeignClients  //1.支持Hystrix和他的fallback  2.支持ribbon 的负载均衡  3.启动时会进行包扫描,扫描所有带@FeignClient的类并进行处理

3.配置增加

#开启熔断
feign.hystrix.enabled=true
#默认超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=120000 
#特定feign的超时时间
hystrix.command.MqSendFeign#sendMq(MqMessage).execution.isolation.thread.timeoutInMilliseconds=1000

 

4.feign客户端注解中fallback指定熔断处理类,需要知道具体调用失败原因可以使用fallbackFactory

feign

@FeignClient(name = "test",fallback = MqSendFeignFallback.class)
//@FeignClient(name = "test",fallbackFactory = MqsendFeignFallbackFactory.class) //fallbackFactory可以获取调用失败原因
public interface MqSendFeign {

    @PostMapping("/ss/common/sendMq")
    Result sendMq(@RequestBody @Valid MqMessage mqMessage);

}

fallback

@Slf4j
@Component
public class MqSendFeignFallback implements MqSendFeign {

    @Override
    public Result sendMq(MqMessage mqMessage) {
        log.info("MqSendFeignFallback error");
        return new Result();
    }
}

 

fallbackFactory

@Slf4j
@Component
public class MqsendFeignFallbackFactory implements FallbackFactory {

    @Override
    public MqSendFeign create(Throwable throwable) {
        return new MqSendFeign() {
            @Override
            public Result sendMq(MqMessage mqMessage) {
                log.error("MqsendFeignFallbackFactory error",throwable);
                return new Result();
            }
        };
    }
}

 

你可能感兴趣的:(springcloud feign增加熔断器Hystrix)