第七章:SpringCloud Feign对hystrix的支持

方法一:设置fallback属性

Feign Hystrix Fallbacks 官网解释
Hystrix supports the notion of a fallback: a default code path that is executed when they circuit is open or there is an error. To enable fallbacks for a given @FeignClient set the fallback attribute to the class name that implements the fallback.

它说你想要给Feign fallback功能,就给@FeignClient注解设置fallback属性,并且回退类要继承@FeignClient所注解的接口

  • 官方实例:
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello iFailSometimes();
}

static class HystrixClientFallback implements HystrixClient {
    @Override
    public Hello iFailSometimes() {
        return new Hello("fallback");
    }
}
  • 但是如果HystrixClientFallback类拿出去单独作为一个类的话,我们就得在该类上添加注解@Component

    UserFeignClient.java.png

    HystrixFallBack.java.png

  • 最后访问feign端口7901调用的服务


    image.png

    访问/health
    image.png

    访问正常。
  • 然后我关闭user服务再进行访问


    image.png

    image.png
  • To disable Hystrix support for Feign, set feign.hystrix.enabled=false.
    想关闭feign对hystrix的支持的话,在application里配置feign.hystrix.enabled=false.

To disable Hystrix support on a per-client basis create a vanilla Feign.Builder with the "prototype" scope, e.g.:
想要以每个Feign客户端为基础来禁用或开启Hystrix
请在需要禁用的类中添加

@Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder();
    }

原理:官网上有
Feign.Builder feignBuilder: HystrixFeign.Builder
说明了 Feign 默认是开启HystrixFeign的,默认Builder对象是HystrixFeign,我们在这里只Builder Feign,所以会达到禁用Hystrix的效果

方法二:设置fallfactory属性

提示:如果fallback默认优先级比fallfactory优先级高。所以二者都存在的话,会访问fallback的回退方法。
这里不做演示。
那么fallback和fallfactory有什么区别呢

不多哔哔,先看代码后加分析。

@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello iFailSometimes();
}

@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
    @Override
    public HystrixClient create(Throwable cause) {
        return new HystrixClientWithFallBackFactory() {
            @Override
            public Hello iFailSometimes() {
                return new Hello("fallback; reason was: " + cause.getMessage());
            }
        };
    }
}

这是官网给的demo。我在这里把类都提了出来。
UserFeignClient 接口:

package com.fantj.moviefeign.feign;

import com.fantj.fantjconsumermovie.entity.User;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Fant.J.
 * 2017/12/1 19:33
 */
@FeignClient(value = "provider-user3",fallbackFactory = HystrixFallbackFactory.class)
public interface UserFeignClient {
    @RequestMapping(value = "/simple/{id}",method = RequestMethod.GET)
    User findById(@PathVariable("id") Long id);
}

HystrixFallbackFactory 实现FallbackFactory接口

package com.fantj.moviefeign.feign;

import com.fantj.fantjconsumermovie.entity.User;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Created by Fant.J.
 * 2017/12/3 14:04
 */
@Component
@Slf4j
public class HystrixFallbackFactory implements FallbackFactory<UserFeignClient>{
    @Override
    public UserFeignClient create(Throwable throwable) {
        log.info("fallback; reason was: " + throwable.getMessage());
        return id -> {
            User user = new User();
            user.setId(0L);
            return user;
        };
    }
}

注意我在重写create方法中把id返回值固定为0。如果它返回的id是0,证明fallbackfactory启用。

image.png

然后create方法中也有获取异常并打印日志。我们看下日志。提示我们访问被拒绝。(之前我手动把3provider-user3服务停止)
image.png

fallback和fallfactory区别:
  • fallback只是重写了回退方法
  • fallfactory层面比较深,因为它可以调出底层异常
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,969评论 19 139
  • Ribbon配置 全局配置 由于Spring cloud Feign的客户端负载均衡是通过spring cloud...
    二月_春风阅读 6,494评论 0 13
  • 介绍 现在我们假设一下,服务提供者响应非常缓慢,那么消费者对提供者的请求就会被强制等待,直到服务返回。在高负载场景...
    RalapHao阅读 936评论 0 0
  • 后来细想,我所希望的,从未真正成功过,或迟来,或减半。 后来细想,我所希望的,终究会实现,或迟些,或变个样子。
    我想到的名字都有阅读 256评论 0 0
  • 一周一天的休息,让我疲惫的身心得到放松 坐在港式茶点屋里,听着老了发黄的粤语歌,感觉时间在慢慢凝滞…… 回想自己工...
    萧瑟花语阅读 215评论 0 1