欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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 调用的地址

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。