SpringCloud @FeignClient参数的用法解析
程序员文章站
2022-06-17 22:51:05
目录springcloud @feignclient 参数详解@feignclient 注解常用参数springcloud @feignclient 参数详解今天因为工作中遇到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/applicationname - 3.
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 调用的地址
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
上一篇: 躺睡月入百万,宠物直播能诞生下一个“薇娅李佳琦”吗?
下一篇: 整数反转
推荐阅读
-
js解析与序列化json数据(一)json.stringify()的基本用法_json
-
PHP中redis的用法深入解析_PHP
-
解析Ubuntu下crontab命令的用法
-
请问关于PDO的参数(有长度限制,或者Bug)用法
-
html中
标签的用法以及作用解析 -
Object.wait()与Object.notify()的用法详细解析
-
spring MVC中接口参数解析的过程详解
-
c#方法中调用参数的值传递方式和引用传递方式以及ref与out的区别深入解析
-
java Swing JFrame框架类中setDefaultCloseOperation的参数含义与用法示例
-
解析web.xml中在Servlet中获取context-param和init-param内的参数