屬性:
value:指定FeignClient的名稱,如果項目使用了Ribbon,name屬性會作爲微服務的名稱,用於服務發現
configuration: Feign配置類,可以自定義Feign的Encoder、Decoder、LogLevel、Contract
contextId:用來區分FeignClient實例名稱
比如我們有個HBS_SERVICE服務,但HBS_SERVICE服務中有很多個對外接口,我們不想將所有的調用接口都定義在一個類中,比如:
CustomerClient1
@FeignClient(value = ServiceNameConstants.HBS_SERVICE, fallbackFactory = CustomerClientFallbackFactory.class) public interface CustomerClient1 { ...... }
CustomerClient2
@FeignClient(value = ServiceNameConstants.HBS_SERVICE, fallbackFactory = CustomerClientFallbackFactory.class) public interface CustomerClient2 { ...... }
在定義多個的時候,因爲定了相同的value,啓動服務會報錯
Description: The bean 'optimization-user.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled. Action: Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
爲了解決這個錯誤,有兩種辦法:
1.避免name重複,給contextId賦值。
2.在name重複的情況下允許BeanDefinition的覆蓋,則配置spring.main.allow-bean-definition-overriding=true
如果我們使用的是第二種辦法,而且要自定義一個Client的configuration時,設置對應Client的configuration屬性,可能配置會不生效
@FeignClient(value = ServiceNameConstants.HBS_SERVICE, configuration = FeignSupportConfig.class, fallbackFactory = CustomerClientFallbackFactory.class) public interface CustomerClient2 { ...... }
因爲允許BeanDefinition的覆蓋時,對應的FeignContext的配置也會覆蓋,這種就需要單獨設置contextId,配置纔會生效
@FeignClient(contextId = "CustomerClient2", value = ServiceNameConstants.HBS_SERVICE, configuration = FeignSupportConfig1.class, fallbackFactory = CustomerClientFallbackFactory.class) public interface CustomerClient2 { ...... }