• springcloud feign增加熔断器Hystrix


    1.依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </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<MqSendFeign> {
    
        @Override
        public MqSendFeign create(Throwable throwable) {
            return new MqSendFeign() {
                @Override
                public Result sendMq(MqMessage mqMessage) {
                    log.error("MqsendFeignFallbackFactory error",throwable);
                    return new Result();
                }
            };
        }
    }
  • 相关阅读:
    Windows照片查看器全屏浏览查看
    Windows调出软键盘
    IE叉事件
    对gridview的小改动
    gridview小把戏
    安全认证(转)
    用数组的方式实现DataTable中的distinct(转)
    TreeView的简单应用
    禁止按钮重复提交
    配置Microsoft Visual SourceSafe 2005的Internet访问(转)
  • 原文地址:https://www.cnblogs.com/liuboyuan/p/12010127.html
Copyright © 2020-2023  润新知