您好,登錄后才能下訂單哦!
這篇文章主要介紹“@FeignClient注解中的contextId屬性如何使用”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“@FeignClient注解中的contextId屬性如何使用”文章能幫助大家解決問題。
如果我們使用Feign定義了兩個(gè)接口,但是目標(biāo)服務(wù)是同一個(gè),那么在SpringBoot啟動(dòng)時(shí)就會(huì)遇到一個(gè)問題:
Description:
The bean 'xxxxxxxx.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
修改yml配置:spring.main.allow-bean-definition-overriding=true
spring: main: allow-bean-definition-overriding: true
在每個(gè)Feign的接口中,在注解上加 contextId屬性
contextId在Feign Client的作用是在注冊(cè)Feign Client Configuration的時(shí)候需要一個(gè)名稱,名稱是通過getClientName方法獲取的
@FeignClient(name = "sale-service",contextId= "saleservice1") public interface saleClient{ @RequestMapping(value = "/sale/add", method = RequestMethod.GET) String add(@RequestParam("saleNum") String queryStr); }
備注:contextId= "名稱" 中的名稱,不能用“_”會(huì)報(bào)錯(cuò),可以用“-”
包名:spring-cloud-openfeign-core-2.2.5.RELEASE.jar
類路徑:org.springframework.cloud.openfeign.FeignClientsRegistrar
private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) { String className = annotationMetadata.getClassName(); BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class); this.validate(attributes); definition.addPropertyValue("url", this.getUrl(attributes)); definition.addPropertyValue("path", this.getPath(attributes)); String name = this.getName(attributes); definition.addPropertyValue("name", name); String contextId = this.getContextId(attributes); definition.addPropertyValue("contextId", contextId); definition.addPropertyValue("type", className); definition.addPropertyValue("decode404", attributes.get("decode404")); definition.addPropertyValue("fallback", attributes.get("fallback")); definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory")); definition.setAutowireMode(2); String alias = contextId + "FeignClient"; AbstractBeanDefinition beanDefinition = definition.getBeanDefinition(); beanDefinition.setAttribute("factoryBeanObjectType", className); boolean primary = (Boolean)attributes.get("primary"); beanDefinition.setPrimary(primary); String qualifier = this.getQualifier(attributes); if (StringUtils.hasText(qualifier)) { alias = qualifier; } BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[]{alias}); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); }
代碼截圖:
可以看到, name應(yīng)該是從注解中的屬性取值來的, 再看看getClientName()方法.
private String getClientName(Map<String, Object> client) { if (client == null) { return null; } else { String value = (String)client.get("contextId"); if (!StringUtils.hasText(value)) { value = (String)client.get("value"); } if (!StringUtils.hasText(value)) { value = (String)client.get("name"); } if (!StringUtils.hasText(value)) { value = (String)client.get("serviceId"); } if (StringUtils.hasText(value)) { return value; } else { throw new IllegalStateException("Either 'name' or 'value' must be provided in @" + FeignClient.class.getSimpleName()); } } }
代碼截圖:
一目了然了, 我們聲明@FeignClient注解時(shí), 只使用了value屬性, 所以產(chǎn)生了沖突, 只要加上contextId就好了.
關(guān)于“@FeignClient注解中的contextId屬性如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。