有Feign客户端呈现:
@FeignClient(name = "storeClient", url = "${feign.url}")
public interface StoreClient {
//..
}
是否有可能利用环境变化的Spring Cloud功能在运行时更改Feign URL? (更改feign.url
属性并调用/refresh
端点)
作为一种可能的解决方案 - 可以引入RequestInterceptor
,以便在RequestTemplate
中定义的属性中设置RefreshScope
中的URL。
要实现此方法,您应该执行以下操作:
ConfigurationProperties
中定义Component
RefreshScope
@Component
@RefreshScope
@ConfigurationProperties("storeclient")
public class StoreClientProperties {
private String url;
...
}
application.yml
中指定客户端的默认URL
storeclient
url: https://someurl
RequestInterceptor
@Configuration
public class StoreClientConfiguration {
@Autowired
private StoreClientProperties storeClientProperties;
@Bean
public RequestInterceptor urlInterceptor() {
return template -> template.insert(0, storeClientProperties.getUrl());
}
}
FeignClient
定义中使用一些占位符作为URL,因为它不会被使用
@FeignClient(name = "storeClient", url = "NOT_USED")
public interface StoreClient {
//..
}
现在storeclient.url
可以刷新,定义的URL将在RequestTemplate
中用于发送http请求。