美文网首页springcloud
013--Eureka中Feign对Hystrix支持

013--Eureka中Feign对Hystrix支持

作者: 糖纸疯了 | 来源:发表于2018-05-20 17:42 被阅读33次

    话题一:Feign使用Hystrix实现断路FallBack

    • 1.Feign使用Hystrix的时候,不是在Controller中进行,而是在Feign接口中进行操作
    • 2.创建Feign的接口,然后用Hystrix实现这个接口,在里面返回默认值
        @FeignClient(name="server-provider",fallback=ConsumeFeignHystrix.class)
        public interface ConsumeFeign {
        
             @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
             public User findById(@PathVariable("id") Long id); // 两个坑:1. @GetMapping不支持   2. @PathVariable得设置value
        }
        // 进行Hystrix的实现
        @Component//必须添加,要不然程序启动不了
        class ConsumeFeignHystrix implements ConsumeFeign{
            @Override
            public User findById(Long id) {
                User user = new User(11L, "xaioming");
                return user;
            }
        }
    
    • 3.不同的接口实现不同的FallBack
      • 3.1 ##添加feign对Hystrix的支持
        feign.hystrix.enable: true
      • 3.2 ##设置Hystrix的请求超时时间
        hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds: 5000
      • 3.3 分别对不用的FeignClient进行不同的FallBack配置
      • 3.4 因为feign.hystrix.enable: true全局开启,所以即使Eureka挂了,也还可以调用对应的CallBack方法(前提Eureka必须先启动)

    话题二:禁用Feign对Hystrix的支持

    • 1 在Fegin使用的Config中添加Feign.Builder feignBuilder配置
            @Bean
            @Scope("prototype")
            //禁用Feign对Hystrix的支持
            public Feign.Builder feignBuilder(){
                return Feign.builder();
            }
    
    • 2 重启Fegin,如果此时Eureka挂了
      • 在访问使用feignBuilder的Feign请求中,不能进行CallBack回调了
      • 在访问没有feignBuilder的Feign请求中,仍然可以进行CallBack回调

    相关文章

      网友评论

        本文标题:013--Eureka中Feign对Hystrix支持

        本文链接:https://www.haomeiwen.com/subject/zbrtjftx.html