@FeignClient註解 中屬性 contextId使用

@FeignClient註解 中屬性 contextId
比如我們有個user服務,但user服務中有很多個接口,我們不想將所有的調用接口都定義在一個類中,比如:

Client 1
@FeignClient(name = "optimization-user")
public interface UserRemoteClient {
	@GetMapping("/user/get")
	public User getUser(@RequestParam("id") int id);
}

Client 2
@FeignClient(name = "optimization-user")
public interface UserRemoteClient2 {
	@GetMapping("/user2/get")
	public User getUser(@RequestParam("id") int id);
}

這種情況下啓動就會報錯了,因爲Bean的名稱衝突了,具體錯誤如下:

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

解決方案可以增加下面的配置,作用是允許出現beanName一樣的BeanDefinition。

spring.main.allow-bean-definition-overriding=true

另一種解決方案就是爲每個Client手動指定不同的contextId,這樣就不會衝突了。

上面給出了Bean名稱衝突後的解決方案,下面來分析下contextId在Feign Client的作用,在註冊Feign Client Configuration的時候需要一個名稱,名稱是通過getClientName方法獲取的:

鏈接:http://www.imooc.com/article/details/id/299213

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章