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

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: