Feign 的动态 URL
2024-09-30 08:00:49 # Technical # Notes

继承 Feign.Builder,重写 target 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Component
public class FeignBuilderHelper extends Feign.Builder {

private final DynamicUrlHelper dynamicUrlHelper;

@Autowired
public FeignBuilderHelper(DynamicUrlHelper dynamicUrlHelper) {
this.dynamicUrlHelper = dynamicUrlHelper;
}

@Override
public <T> T target(Target<T> target) {
return super.target(new Target.HardCodedTarget<T>(target.type(), target.name(), target.url()) {
@Override
public String url() {
String url = dynamicUrlHelper.dynamicUrl(this.name());
return StringUtils.isBlank(url) ? super.url() : url;
}
});
}
}

从配置中心,获取动态的URL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
public class DynamicUrlHelper {

private static final String BIG_DATA_SERVER_NAME = "BigDataServer";

@Value("${big.data.virtual.host:}")
private String bigDataServerUrl;

public String dynamicUrl(String targetName) {
if (Objects.equals(targetName, BIG_DATA_SERVER_NAME)) {
return bigDataServerUrl;
}
return Strings.EMPTY;
}
}

注意

可能会出现 BeanCreationException 异常:

1
Unsatisfied dependency expressed through constructor parammeter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creeating bean with name '......': FactoryBean threw exception object creation; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'feign.Feign$Builder' available: expected single matching bean but found 2: feignBuilderHelper,feignHystrixBuilder 

解决:

FeignBuilderHelper 添加 @Primary 注解

Thanks

Feign:实现动态URL