java学习Spring-BootSpring Boot

第七章:SpringCloud Feign对hystrix的支持

2017-12-03  本文已影响55人  FantJ

方法一:设置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");
    }
}

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区别:
上一篇下一篇

猜你喜欢

热点阅读