送设?/span> 保质?/span> 高品?/span>
当前位置:主页 > 建站 >

那天晚上和@FeignClient注解的深度交流

文章出处:未知 发表时间:2020-11-11

比方咱们有个 user 服务,但 user 服务中有很多个接口,咱们不想将一切的调用接口都界说在一个类中,比方:

Client 1

@FeignClient

public interface UserRemoteClient {

@GetMapping

public User getUser int id);

}

@FeignClient

public interface UserRemoteClient {

@GetMapping

public User getUser int id);

}

Client 2

@FeignClient

public interface UserRemoteClient2 {

@GetMapping

public User getUser int id);

}

@FeignClient

public interface UserRemoteClient2 {

@GetMapping

public User getUser 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

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 办法获取的:

String name = getClientName;

registerClientConfiguration);

String name = getClientName;

registerClientConfiguration);

private String getClientName {

if {

return null;

}

String value = client.get;

if ) {

value = client.get;

}

if ) {

value = client.get;

}

if ) {

value = client.get;

}

if ) {

return value;

}

throw new IllegalStateException);

}

private String getClientName {

if {

return null;

}

String value = client.get;

if ) {

value = client.get;

}

if ) {

value = client.get;

}

if ) {

value = client.get;

}

if ) {

return value;

}

throw new IllegalStateException);

}

能够看到假如装备了 contextId 就会用 contextId,假如没有装备就会去 value 然后是 name 最终是 serviceId。默许都没有装备,当呈现一个服务有多个 Feign Client 的时分就会报错了。

其次的效果是在注册 FeignClient 中,contextId 会作为 Client 别号的一部分,假如装备了 qualifier 优先用 qualifier 作为别号。

private void registerFeignClient {

String className = annotationMetadata.getClassName;

BeanDefinitionBuilder definition = BeanDefinitionBuilder

.genericBeanDefinition;

validate;

definition.addPropertyValue);

definition.addPropertyValue);

String name = getName;

definition.addPropertyValue;

String contextId = getContextId;

definition.addPropertyValue;

definition.addPropertyValue;

definition.addPropertyValue);

definition.addPropertyValue);

definition.addPropertyValue);

definition.setAutowireMode;


// 拼接别号

String alias = contextId FeignClient ;

AbstractBeanDefinition beanDefinition = definition.getBeanDefinition;



boolean primary = attributes.get; // has a default, won't be

// null



beanDefinition.setPrimary;


// 装备了qualifier优先用qualifier

String qualifier = getQualifier;

if ) {

alias = qualifier;

}



BeanDefinitionHolder holder = new BeanDefinitionHolder;

BeanDefinitionReaderUtils.registerBeanDefinition;

}

private void registerFeignClient {

String className = annotationMetadata.getClassName;

BeanDefinitionBuilder definition = BeanDefinitionBuilder

.genericBeanDefinition;

validate;

definition.addPropertyValue);

definition.addPropertyValue);

String name = getName;

definition.addPropertyValue;

String contextId = getContextId;

definition.addPropertyValue;

definition.addPropertyValue;

definition.addPropertyValue);

definition.addPropertyValue);

definition.addPropertyValue);

definition.setAutowireMode;


// 拼接别号

String alias = contextId FeignClient ;

AbstractBeanDefinition beanDefinition = definition.getBeanDefinition;



boolean primary = attributes.get; // has a default, won't be

// null



beanDefinition.setPrimary;


// 装备了qualifier优先用qualifier

String qualifier = getQualifier;

if ) {

alias = qualifier;

}



BeanDefinitionHolder holder = new BeanDefinitionHolder;

BeanDefinitionReaderUtils.registerBeanDefinition;

}

上一篇:微服务开发的 10 个最佳实践 下一篇:没有了

推荐产品

同类文章排行

最新资讯文章

友情链接