欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud @FeignClient參數(shù)的用法解析

 更新時間:2021年10月20日 15:52:53   作者:苦海菩提路  
這篇文章主要介紹了SpringCloud @FeignClient參數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringCloud @FeignClient 參數(shù)詳解

今天因為工作中遇到FeignClient一個奇葩的bug,后面仔細研究了,找出了原因,那么剛好對FeignClient 這個注解總結(jié)一下:

先看@FeignClient 源碼:源碼如下,本文最后面。

11個方法,常用方法說明如下

@FeignClient(name = "service-name", url = "${feign.urls.service-name:}", fallback =ApiFallBack.class,configuration = Interceptor.class)
  • 1.value,name 這兩個就同一個意思:對應(yīng)的是調(diào)用的微服務(wù)的服務(wù)名,對用服務(wù)發(fā)現(xiàn)、走網(wǎng)關(guān)調(diào)用,這個很關(guān)鍵。
  • 2.url 這是訪問地址,可以直接提供給外部調(diào)用,也可以直接寫如192.168.1.11:8800/applicationName
  • 3.fallback fallbackFactory

就給@FeignClient注解設(shè)置fallback屬性,并且回退類要繼承@FeignClient所注解的接口

ApiFallBack類拿出去單獨作為一個類的話,我們就得在該類上添加注解@Component

如果fallback默認優(yōu)先級比fallfactory優(yōu)先級高。所以二者都存在的話,會訪問fallback的回退方法。

這里不做演示。

那么fallback和fallfactory有什么區(qū)別呢

@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區(qū)別

  • 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 注解常用參數(shù)

怕以后又忘記,總結(jié)下目前項目中實際用到的 @FeignClient 注解中的參數(shù),如下:

@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

  • 一般用于調(diào)試,可以手動指定 @FeignClient 調(diào)用的地址

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot壓縮json并寫入Redis的示例代碼

    SpringBoot壓縮json并寫入Redis的示例代碼

    由于業(yè)務(wù)需要,存入redis中的緩存數(shù)據(jù)過大,占用了10+G的內(nèi)存,內(nèi)存作為重要資源,需要優(yōu)化一下大對象緩存,所以我們需要對json進行壓縮,本文給大家介紹了SpringBoot如何壓縮Json并寫入redis,需要的朋友可以參考下
    2024-08-08
  • 詳細全面解析Java泛型

    詳細全面解析Java泛型

    這篇文章主要介紹了詳細全面解析Java泛型,java泛型主要提高了Java 程序的類型安全,通過知道使用泛型定義的變量的類型限制,編譯器可以驗證類型假設(shè),消除源代碼中的許多強制類型轉(zhuǎn)換等多個有點,下面我們進入文章了解更多的詳細內(nèi)容吧
    2022-02-02
  • MyBatis-Flex+ShardingSphere-JDBC多數(shù)據(jù)源分庫分表實現(xiàn)

    MyBatis-Flex+ShardingSphere-JDBC多數(shù)據(jù)源分庫分表實現(xiàn)

    本文介紹了使用MyBatis-Flex和ShardingSphere-JDBC實現(xiàn)多數(shù)據(jù)源分庫分表的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10
  • mybatisplus的坑?insert標簽insert?into?select無參數(shù)問題的解決

    mybatisplus的坑?insert標簽insert?into?select無參數(shù)問題的解決

    這篇文章主要介紹了mybatisplus的坑?insert標簽insert?into?select無參數(shù)問題的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java編程實現(xiàn)計算兩個日期的月份差實例代碼

    Java編程實現(xiàn)計算兩個日期的月份差實例代碼

    這篇文章主要介紹了Java編程實現(xiàn)計算兩個日期的月份差實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • java編程之基于SpringBoot框架實現(xiàn)掃碼登錄

    java編程之基于SpringBoot框架實現(xiàn)掃碼登錄

    本文將介紹基于SpringBoot + Vue + Android實現(xiàn)的掃碼登錄demo的總體思路,文中附含詳細示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • SpringBoot中配置log4j2日志詳解

    SpringBoot中配置log4j2日志詳解

    這篇文章主要介紹了SpringBoot中配置log4j2日志詳解,Apache Log4j2 是對原先的 Log4j 項目的升級版本,參考了 logback 的一些優(yōu)秀的設(shè)計,并且修復(fù)了一些問題,因此帶來了一些重大的提升,需要的朋友可以參考下
    2023-11-11
  • SpringBoot MyBatis保姆級整合教程

    SpringBoot MyBatis保姆級整合教程

    因為Spring Boot框架開發(fā)的便利性,所以實現(xiàn)Spring Boot與數(shù)據(jù)訪問層框架(例如MyBatis)的整合非常簡單,主要是引入對應(yīng)的依賴啟動器,并進行數(shù)據(jù)庫相關(guān)參數(shù)設(shè)置即可
    2022-06-06
  • java?抽象類示例詳解

    java?抽象類示例詳解

    我們將“只有方法聲明,沒有方法體”的一類方法統(tǒng)稱為抽象方法,抽象方法用關(guān)鍵字abstract修飾,本文介紹java?抽象類示例詳解,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • 詳細了解MyBatis的異常處理機制

    詳細了解MyBatis的異常處理機制

    本文將對MyBatis的異常體系以及異常使用進行學習,MyBatis版本是3.5.6,作為一款成熟的ORM框架,MyBatis有自己一套成熟的異常處理體系,,需要的朋友可以參考下
    2023-06-06

最新評論