W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Spring Cloud的Feign支持中的中心概念是指定客戶的概念。每個(gè)虛擬客戶端都是組件的一部分,這些組件可以一起工作以按需聯(lián)系遠(yuǎn)程服務(wù)器,并且該組件的名稱是您使用@FeignClient
批注將其指定為應(yīng)用程序開(kāi)發(fā)人員的。Spring Cloud根據(jù)需要使用FeignClientsConfiguration
為每個(gè)命名客戶端創(chuàng)建一個(gè)新的合奏作為ApplicationContext
。
其中包含feign.Decoder
,feign.Encoder
和feign.Contract
。通過(guò)使用@FeignClient
批注的contextId
屬性,可以覆蓋該集合的名稱。
Spring Cloud使您可以通過(guò)使用@FeignClient
聲明其他配置(在FeignClientsConfiguration
之上)來(lái)完全控制假客戶端。例:
@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient { //.. }
在這種情況下,客戶端由FeignClientsConfiguration
中已有的組件以及FooConfiguration
中的任何組件組成(其中后者將覆蓋前者)。
FooConfiguration
不需要用@Configuration
進(jìn)行注釋。但是,如果是的話,請(qǐng)注意將其從任何可能包含此配置的@ComponentScan
中排除,因?yàn)樗鼘⒊蔀?/span>feign.Decoder
,feign.Encoder
,feign.Contract
等的默認(rèn)來(lái)源,指定時(shí)。可以通過(guò)將其與任何@ComponentScan
或@SpringBootApplication
放在單獨(dú)的,不重疊的包中來(lái)避免這種情況,也可以在@ComponentScan
中將其明確排除在外。
現(xiàn)在不推薦使用
serviceId
屬性,而推薦使用name
屬性。
除了更改
ApplicationContext
集合的名稱之外,還使用@FeignClient
批注的contextId
屬性,它會(huì)覆蓋客戶端名稱的別名,并將其用作配置名稱的一部分bean為該客戶端創(chuàng)建的。
以前,使用
url
屬性不需要name
屬性。現(xiàn)在需要使用name
。
name
和url
屬性中支持占位符。
@FeignClient(name = "${feign.name}", url = "${feign.url}") public interface StoreClient { //.. }
Spring Cloud Netflix默認(rèn)提供以下beans偽裝(BeanType
beanName:ClassName
):
Decoder
feignDecoder:ResponseEntityDecoder
(包裝SpringDecoder
)
Encoder
feignEncoder:SpringEncoder
Logger
feignLogger:Slf4jLogger
Contract
feignContract:SpringMvcContract
Feign.Builder
feignBuilder:HystrixFeign.Builder
Client
feignClient:如果啟用了Ribbon,則它是LoadBalancerFeignClient
,否則使用默認(rèn)的偽裝客戶端。
可以通過(guò)分別將feign.okhttp.enabled
或feign.httpclient.enabled
設(shè)置為true
并將其放在類路徑中來(lái)使用OkHttpClient和ApacheHttpClient虛擬客戶端。您可以自定義HTTP客戶端,方法是在使用Apache時(shí)提供ClosableHttpClient
的bean,在使用OK HTTP時(shí)提供OkHttpClient
。
Spring Cloud Netflix 默認(rèn)情況下不會(huì)為偽裝提供以下beans,但仍會(huì)從應(yīng)用程序上下文中查找以下類型的beans以創(chuàng)建偽裝客戶端:
Logger.Level
Retryer
ErrorDecoder
Request.Options
Collection<RequestInterceptor>
SetterFactory
創(chuàng)建其中一種類型的bean并將其放置在@FeignClient
配置中(例如上述FooConfiguration
),您可以覆蓋上述的每個(gè)beans。例:
@Configuration public class FooConfiguration { @Bean public Contract feignContract() { return new feign.Contract.Default(); } @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("user", "password"); } }
這將SpringMvcContract
替換為feign.Contract.Default
,并將RequestInterceptor
添加到RequestInterceptor
的集合中。
@FeignClient
也可以使用配置屬性進(jìn)行配置。
application.yml
feign: client: config: feignName: connectTimeout: 5000 readTimeout: 5000 loggerLevel: full errorDecoder: com.example.SimpleErrorDecoder retryer: com.example.SimpleRetryer requestInterceptors: - com.example.FooRequestInterceptor - com.example.BarRequestInterceptor decode404: false encoder: com.example.SimpleEncoder decoder: com.example.SimpleDecoder contract: com.example.SimpleContract
可以按照與上述類似的方式在@EnableFeignClients
屬性defaultConfiguration
中指定默認(rèn)配置。不同之處在于此配置將適用于所有
偽客戶端。
如果您希望使用配置屬性來(lái)配置所有@FeignClient
,則可以使用default
虛擬名稱創(chuàng)建配置屬性。
application.yml
feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 loggerLevel: basic
如果我們同時(shí)創(chuàng)建@Configuration
bean和配置屬性,則配置屬性將獲勝。它將覆蓋@Configuration
值。但是,如果要將優(yōu)先級(jí)更改為@Configuration
,可以將feign.client.default-to-properties
更改為false
。
如果您需要在
RequestInterceptor`s you will need to either set the thread isolation strategy for Hystrix to `SEMAPHORE
中使用ThreadLocal
綁定變量,或者在Feign中禁用Hystrix。
application.yml
# To disable Hystrix in Feign feign: hystrix: enabled: false # To set thread isolation to SEMAPHORE hystrix: command: default: execution: isolation: strategy: SEMAPHORE
如果我們要?jiǎng)?chuàng)建多個(gè)具有相同名稱或URL的偽裝客戶端,以便它們指向同一臺(tái)服務(wù)器,但每個(gè)客戶端使用不同的自定義配置,則必須使用@FeignClient
的contextId
屬性,以避免這些配置beans的名稱沖突。
@FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class) public interface FooClient { //.. }
@FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class) public interface BarClient { //.. }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: