Feign 中 @FeignClient 如何使用 fallbackFactory 属性 ,并打印fallback异常

一.介绍

fallbackFactory(类似于断容器)与fallback方法。

feign的注解@FeignClient:
fallbackFactory 与 fallback 方法不能同时使用,这个两个方法其实都类似于 Hystrix 的功能,当网络不通时返回默认的配置数据。

fallback 方法的使用:详情见文章 ,这里不做叙述。

二.现在讲下 fallbackFactory 的使用。

项目用 eureka-feign-hystrix-client,[参考文章链接](https://blog.csdn.net/weixin_40991408/article/details/103892328)
2.1 在@feignClient 中 fallbackFactory方法,代码如下:

package com.example.eurekafeignhystrixclient.inter;

import com.example.eurekafeignhystrixclient.config.FeignConfig;
import com.example.eurekafeignhystrixclient.fallback.FallBack;
import com.example.eurekafeignhystrixclient.fallbackfactory.HystrixClientFactory;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


//在接口上加 @FeignClient 注解来声明 一个Feign Client,其中 value 为 远程调用其他服务的服务名
//FeignConfig.class 为 Feign Client 的配置类,注入Retryer类的实例,这样在远程调用失败后,feign会进行重试
//使用 Spring MVC 的注解来绑定具体该服务提供的 REST 接口

//fallback 配置回调处理类,该处理类是作为 Feign 熔断器的逻辑处理类,实现FeignHystrixInter 接口
//@FeignClient(value = "eureka-client",configuration = FeignConfig.class,fallback = FallBack.class)

//fallbackFactory(类似于断容器)与fallback方法。
@FeignClient(value = "eureka-client",configuration = FeignConfig.class,fallbackFactory = HystrixClientFactory.class)
public interface FeignHystrixInter {

    //使用 Spring MVC 的注解来绑定具体该服务提供的 REST 接口
    @GetMapping(value = "/HiController/hi/{name}")
    String hi(@PathVariable(value = "name") String name);
}

2.2 写一个UserFeignClientWithFactory 接口了,继承FeignHystrixInter ,代码如下:

package com.example.eurekafeignhystrixclient.fallbackfactory.inter;

import com.example.eurekafeignhystrixclient.inter.FeignHystrixInter;

//继承 @FeignClient 注解的 接口类
public interface UserFeignClientWithFactory extends FeignHystrixInter {
}

2.3 写 HystrixClientFactory 类,实现 FallbackFactory 接口,重写 create方法,代码如下:

package com.example.eurekafeignhystrixclient.fallbackfactory;

import com.example.eurekafeignhystrixclient.fallbackfactory.inter.UserFeignClientWithFactory;
import com.example.eurekafeignhystrixclient.inter.FeignHystrixInter;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component //注入Spring 容器中
public class HystrixClientFactory implements FallbackFactory<FeignHystrixInter> {

    private static final Logger logger = LoggerFactory.getLogger(HystrixClientFactory.class);

    @Override
    public FeignHystrixInter create(Throwable cause) {
        //一进入异常就能知道什么异常
        HystrixClientFactory.logger.info("fallback; exception was: {}", cause.toString());
        HystrixClientFactory.logger.info("fallback; reason was: {}", cause.getMessage());
        return new UserFeignClientWithFactory() {
            @Override
            public String hi(String name) {
                return "我有两把枪,一把叫射,一把叫啊";
            }
        };
    }
}

2.4 启动 eureka-serve,eureka-client (8762,8763 两个端口),eureka-feign-hystrix-client , 浏览器访问 http://localhost:8761/
在这里插入图片描述
在浏览器上 输入 :http://localhost:8767/FeignHystrixController/hi/java,得到结果如下:
在这里插入图片描述
关掉eureka-client (8762,8763 两个端口 )服务,结果如下:
在这里插入图片描述
在这里插入图片描述

发布了33 篇原创文章 · 获赞 42 · 访问量 3158

猜你喜欢

转载自blog.csdn.net/weixin_40991408/article/details/103902499