一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringCloud @FeignClient參數的用法解析

SpringCloud @FeignClient參數的用法解析

2022-02-24 00:45苦海菩提路 Java教程

這篇文章主要介紹了SpringCloud @FeignClient參數的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringCloud @FeignClient 參數詳解

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

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

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

?
1
@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有什么區別呢

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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 層面比較深,因為它用線程拋出了異常,可以看到底層具體問題。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
 * 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 注解中的參數,如下:

?
1
2
3
4
5
@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 調用的地址

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/Susan8888/article/details/95992172

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本人交换乱理伦片 | 国内久久| 好湿好紧好大野战 | 亚洲免费在线视频 | 激情三级hd中文字幕 | 国产精品全国探花在线观看 | 久久亚洲精品AV无码四区 | 美女翘臀内疯狂进出 | 午夜欧美福利视频 | 青青草成人在线 | 色婷婷综合和线在线 | 高清不卡一区二区 | 法国贵妇一级伦理hd | 欧美综合另类 | yin娃sao货调教情趣用品店 | 国产精品一二区 | 美女被的在线网站91 | 国产精品亚洲综合第一区 | 波多野结衣中文字幕乱七八糟 | 国产成人99精品免费观看 | 女上男下gifxxoo动态视频 | 草草在线视频 | 女人和男人搞鸡 | 果冻传媒九一制片厂网站 | 亚洲高清一区二区三区久久 | 午夜国产在线 | 久久久大香菇 | 久久国产乱子伦免费精品 | 99爱在线观看精品视频 | 日韩欧美一区二区三区中文精品 | 成人国产在线播放 | 调教车文 | 成人伊在线影院 | 好大好粗好舒服 | 隔壁的漂亮邻居hd中文 | 久久久久久久伊人电影 | 免费一级日本c片完整版 | 国产精品久久国产精品99 gif | 女子监狱第二季在线观看免费完整版 | 欧美xxxxx69| 天天操天天爽天天射 |