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

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

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

SpringCloud @FeignClient 參數(shù)詳解

今天因?yàn)楣ぷ髦杏龅紽eignClient一個(gè)奇葩的bug,后面仔細(xì)研究了,找出了原因,那么剛好對(duì)FeignClient 這個(gè)注解總結(jié)一下:

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

11個(gè)方法,常用方法說明如下

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

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

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

如果fallback默認(rèn)優(yōu)先級(jí)比fallfactory優(yōu)先級(jí)高。所以二者都存在的話,會(huì)訪問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 層面比較深,因?yàn)樗镁€程拋出了異常,可以看到底層具體問題。
/**
 * 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é)下目前項(xiàng)目中實(shí)際用到的 @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)試,可以手動(dòng)指定 @FeignClient 調(diào)用的地址

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

相關(guān)文章

  • springboot controller參數(shù)注入方式

    springboot controller參數(shù)注入方式

    這篇文章主要介紹了springboot controller參數(shù)注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot中RestTemplate的使用詳解

    SpringBoot中RestTemplate的使用詳解

    這篇文章主要介紹了SpringBoot中RestTemplate的使用詳解,RestTemplate是由Spring框架提供的一個(gè)可用于應(yīng)用中調(diào)用rest服務(wù)的類它簡(jiǎn)化了與http服務(wù)的通信方式,統(tǒng)一了RESTFul的標(biāo)準(zhǔn),封裝了http連接,我們只需要傳入url及其返回值類型即可,需要的朋友可以參考下
    2023-10-10
  • SpringBoot JMX的基本使用方式

    SpringBoot JMX的基本使用方式

    這篇文章主要介紹了SpringBoot JMX的基本使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解如何在Java中實(shí)現(xiàn)屏幕共享

    詳解如何在Java中實(shí)現(xiàn)屏幕共享

    在本文中,將為大家展示如何利用 JxBrowser 的功能,實(shí)現(xiàn)運(yùn)行在不同電腦上的兩個(gè) Java 應(yīng)用程序之間的屏幕共享,感興趣的小伙伴可以參考一下
    2024-10-10
  • java實(shí)現(xiàn)潛艇大戰(zhàn)游戲源碼

    java實(shí)現(xiàn)潛艇大戰(zhàn)游戲源碼

    潛艇大戰(zhàn)游戲相信大家都玩過,是一款非常有趣的小游戲,那么基于代碼是如何實(shí)現(xiàn)的呢?今天小編給大家?guī)硪黄坛處椭蠹覍W(xué)習(xí)java實(shí)現(xiàn)潛艇大戰(zhàn)游戲,感謝的朋友一起看看吧
    2021-06-06
  • SpringBoot文件上傳功能的實(shí)現(xiàn)方法

    SpringBoot文件上傳功能的實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot文件上傳功能的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法

    SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法

    本篇文章主要介紹了SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法,詳細(xì)的介紹了三種方法,有興趣的可以了解一下。
    2017-01-01
  • hibernate通過session實(shí)現(xiàn)增刪改查操作實(shí)例解析

    hibernate通過session實(shí)現(xiàn)增刪改查操作實(shí)例解析

    這篇文章主要介紹了hibernate通過session實(shí)現(xiàn)增刪改查操作實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Java獲取用戶訪問IP及地理位置的方法詳解

    Java獲取用戶訪問IP及地理位置的方法詳解

    這篇文章主要介紹了Java獲取用戶訪問IP及地理位置的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java基于百度地圖開放平臺(tái)獲取用戶訪問IP及地理位置相關(guān)操作技巧,需要的朋友可以參考下
    2020-04-04
  • Java?離線中文語音文字識(shí)別功能的實(shí)現(xiàn)代碼

    Java?離線中文語音文字識(shí)別功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java?離線中文語音文字識(shí)別,本次使用springboot?+maven實(shí)現(xiàn),官方demo為springboot+gradle,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評(píng)論