SpringCloud @FeignClient参数的用法解析
作者:苦海菩提路 发布时间:2022-11-25 06:31:37
标签:SpringCloud,@FeignClient,参数
SpringCloud @FeignClient 参数详解
今天因为工作中遇到FeignClient一个奇葩的bug,后面仔细研究了,找出了原因,那么刚好对FeignClient 这个注解总结一下:
先看@FeignClient 源码:源码如下,本文最后面。
11个方法,常用方法说明如下
@FeignClient(name = "service-name", url = "${feign.urls.service-name:}", fallback =ApiFallBack.class,configuration = Interceptor.class)
1.
value
,name
这两个就同一个意思:对应的是调用的微服务的服务名,对用服务发现、走网关调用,这个很关键。2.
url
这是访问地址,可以直接提供给外部调用,也可以直接写如192.168.1.11:8800/applicationName3.
fallback
与fallbackFactory
就给@FeignClient注解设置fallback属性,并且回退类要继承@FeignClient所注解的接口
ApiFallBack类拿出去单独作为一个类的话,我们就得在该类上添加注解@Component
如果fallback默认优先级比fallfactory优先级高。所以二者都存在的话,会访问fallback的回退方法。
这里不做演示。
那么fallback和fallfactory有什么区别呢
@FeignClient(name = "service-name", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/test")
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());
}
};
}
}
fallback和fallfactory区别
fallback
只是重写了回退方法。fallfactory
层面比较深,因为它用线程抛出了异常,可以看到底层具体问题。
/**
* Annotation for interfaces declaring that a REST client with that interface should be
* created (e.g. for autowiring into another component). If ribbon is available it will be
* used to load balance the backend requests, and the load balancer can be configured
* using a <code>@RibbonClient</code> with the same name (i.e. value) as the feign client.
*
* @author Spencer Gibb
* @author Venil Noronha
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
/**
* The name of the service with optional protocol prefix. Synonym for {@link #name()
* name}. A name must be specified for all clients, whether or not a url is provided.
* Can be specified as property key, eg: ${propertyKey}.
*/
@AliasFor("name")
String value() default "";
/**
* The service id with optional protocol prefix. Synonym for {@link #value() value}.
*
* @deprecated use {@link #name() name} instead
*/
@Deprecated
String serviceId() default "";
/**
* The service id with optional protocol prefix. Synonym for {@link #value() value}.
*/
@AliasFor("value")
String name() default "";
/**
* Sets the <code>@Qualifier</code> value for the feign client.
*/
String qualifier() default "";
/**
* An absolute URL or resolvable hostname (the protocol is optional).
*/
String url() default "";
/**
* Whether 404s should be decoded instead of throwing FeignExceptions
*/
boolean decode404() default false;
/**
* A custom <code>@Configuration</code> for the feign client. Can contain override
* <code>@Bean</code> definition for the pieces that make up the client, for instance
* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
*
* @see FeignClientsConfiguration for the defaults
*/
Class<?>[] configuration() default {};
/**
* Fallback class for the specified Feign client interface. The fallback class must
* implement the interface annotated by this annotation and be a valid spring bean.
*/
Class<?> fallback() default void.class;
/**
* Define a fallback factory for the specified Feign client interface. The fallback
* factory must produce instances of fallback classes that implement the interface
* annotated by {@link FeignClient}. The fallback factory must be a valid spring
* bean.
*
* @see feign.hystrix.FallbackFactory for details.
*/
Class<?> fallbackFactory() default void.class;
/**
* Path prefix to be used by all method-level mappings. Can be used with or without
* <code>@RibbonClient</code>.
*/
String path() default "";
/**
* Whether to mark the feign proxy as a primary bean. Defaults to true.
*/
boolean primary() default true;
}
@FeignClient 注解常用参数
怕以后又忘记,总结下目前项目中实际用到的 @FeignClient 注解中的参数,如下:
@FeignClient(value = "annoroad-alpha", url = "${annoroad.ms.annoroad-alpha.url}")
public interface UserFacade {
@PostMapping(value = "/user/detail")
UserDto detail(@RequestParam("id") long id);
}
value
value 等同于 name
url
一般用于调试,可以手动指定 @FeignClient 调用的地址
来源:https://blog.csdn.net/Susan8888/article/details/95992172


猜你喜欢
- 很久之前也写过一篇使用Jitpack发布Android开源库的文章,详见Android发布项目到jitpack的完整步骤近来因为工作原因,又
- 场景:当我们正在master分支开发新功能的时候,突然接到一个任务发现线上出现了一个紧急的BUG需要修复,由于没有打新分支做这部分新需求,这
- 传值就是将实参的值传到所调用的函数里面,实参的值并没有发生变化,默认传值的有int型,浮点型,bo
- 什么是MybatisMyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software fou
- 我们在编写完Spring的代码后,往往需要测试代码的正确性,这个时候就需要用到单元测试了。我们这里使用的版本是junit4.一个程序的入口是
- 本文实例讲述了Android编程实现画板功能的方法。分享给大家供大家参考,具体如下:Android实现画板主要有2种方式,一种是用自定义Vi
- 快速排序快速排序是一种比较高效的排序算法,采用“分而治之”的思想,通过多次比较和交换来实现排序,在一
- 今天遇到一个特别的需求,需要从下面的字符串中转换成一个DateTime对象:[07-13 15:50:42]主要问题是这个时间不是标准的时间
- 提到数组大家肯定不会陌生,但我们也知道数组有个缺点就是在创建时就确定了长度,之后就不能更改长度。所以Java官方向我们提供了ArrayLis
- 一个popwindow,在弹出的时候背景是原界面的截图加高斯模糊效果:先给出popwindow的布局文件<?xml version=&
- 背景最近公司的客户要求,分配给员工的任务除了有微信通知外,还希望PC端的网页也能实时收到通知。管理员分配任务是在我们的系统A,而员工接受任务
- 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式。数组虽然也可以存
- /** * 日期工具类 * 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期&nbs
- 一、抽象类是什么?在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的
- 一、所需要的包:1、commons-fileupload-1.2.1.jar:下载地址http://commons.apache.org/d
- 本文实例为大家分享了Android实现仿网易音乐唱片播放效果的具体代码,供大家参考,具体内容如下效果图: 在values中创建attrs.x
- 本文记录一下,我从AndroidStudio 2.3.3升级到3.0,再升级到3.0.1一路上遇到的输入法之坑以及解决方案。前些天把Andr
- swagger简介Swagger是一款RESTful接口的文档在线自动生成、功能测试功能框架。一个规范和完整的框架,用于生成、描述、调用和可
- 最近 * 丝的公司想要为以前的那个网页产品加上一个过滤的功能,废话不多说,直接看筛选的界面是啥样的吧:可以看出,我们的Message分为Crit
- 前言:现在的手机品牌和型号越来越多,导致我们平时写布局的时候会在个不同的移动设备上显示的效果不同,比如我们的设计稿一个View的大小是300