第七章:SpringCloud Feign对hystrix的支持

本文详细介绍了 Feign 中两种容错机制的使用方法:通过 fallback 和 fallbackFactory 属性。并通过实例展示了如何配置和使用这两种机制。

方法一:设置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

    img_f674ef6cbdaea807916acc14d2e93478.png
    UserFeignClient.java.png

    img_254ce0b5ef82fbfb34bd0a9fa925c52d.png
    HystrixFallBack.java.png

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


    img_7e0a7e2e2145941248b8878e30a9206c.png
    image.png

    访问/health
    img_8a3910620af69543d2629b0e411f66ea.png
    image.png

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


    img_c90d9de428434c5564106fefbf42a441.png
    image.png

    img_ff3673d46d1eb0c9c835992dee712f15.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启用。

img_bc58f7f9358aa2ca2748447088ec34b8.png
image.png

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

fallback和fallfactory区别:
  • fallback只是重写了回退方法
  • fallfactory层面比较深,因为它可以调出底层异常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值