SpringCloud @FeignClient参数的用法解析
作者:苦海菩提路 时间:2022-11-25 06:31:37
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
标签:SpringCloud,@FeignClient,参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android中TabHost的图标(48×48)和文字叠加解决方法
2023-05-19 14:13:28
C#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍历方法对比
2021-07-25 17:00:04
C#操作串口通信协议Modbus的常用方法介绍
2023-01-20 04:06:58
C#索引属性用法实例分析
2023-02-02 14:15:19
JAVA抽象类,接口,内部类详解
2023-11-09 16:37:25
基于WPF实现面包屑控件的示例代码
2021-12-19 12:34:33
![](https://img.aspxhome.com/file/2023/4/82184_0s.gif)
SpringBoot整合Mybatis与thymleft实现增删改查功能详解
2023-01-06 00:22:59
![](https://img.aspxhome.com/file/2023/2/118822_0s.png)
Java模拟有序链表数据结构的示例
2023-09-26 22:25:30
Mybatis防止sql注入原理分析
2023-08-09 22:54:44
c语言中十六进制转二进制显示的实现方法
2023-12-17 23:21:44
Java中ByteArrayInputStream和ByteArrayOutputStream用法详解
2023-03-01 11:22:48
![](https://img.aspxhome.com/file/2023/8/101978_0s.png)
SpringBoot2零基础到精通之数据与页面响应
2022-08-25 22:26:41
![](https://img.aspxhome.com/file/2023/5/110925_0s.jpg)
实战SpringBoot集成JWT实现token验证
2022-10-07 15:57:49
![](https://img.aspxhome.com/file/2023/8/62138_0s.jpg)
Android Vitamio和ExoPlayer两种播放器优劣分析
2023-02-25 13:21:51
Android自定义View实现五子棋游戏
2021-12-25 19:32:55
![](https://img.aspxhome.com/file/2023/5/126685_0s.jpg)
详解Kotlin中的面向对象(一)
2023-08-17 12:57:38
Android自定义View新年烟花、祝福语横幅动画
2022-01-24 21:31:27
![](https://img.aspxhome.com/file/2023/2/139432_0s.gif)
IDEA利用自带Axis工具和wsdl文件反向生成服务端客户端代码图文详解
2021-12-06 20:02:12
![](https://img.aspxhome.com/file/2023/2/73032_0s.jpg)
c# DataDirectory的用法
2021-07-30 14:36:22
VS2019编写C程序或者CUDA程序出现“无法启动程序,系统找不到指定的文件”问题的详细解决方法
2022-04-25 08:09:31
![](https://img.aspxhome.com/file/2023/4/131954_0s.png)