Grz*_*ski 5 spring-cloud spring-cloud-feign
有假装客户介绍:
@FeignClient(name = "storeClient", url = "${feign.url}")
public interface StoreClient {
//..
}
Run Code Online (Sandbox Code Playgroud)
是否可以利用环境更改的Spring Cloud功能在运行时更改Feign url?(更改feign.url属性并调用/refresh端点)
作为一种可能的解决方案 -RequestInterceptor可以引入以便RequestTemplate从RefreshScope.
要实施此方法,您应该执行以下操作:
定义ConfigurationProperties Component在RefreshScope
@Component
@RefreshScope
@ConfigurationProperties("storeclient")
public class StoreClientProperties {
private String url;
...
}
Run Code Online (Sandbox Code Playgroud)在中指定客户端的默认 URLapplication.yml
storeclient
url: https://someurl
Run Code Online (Sandbox Code Playgroud)定义RequestInterceptor将切换 URL
@Configuration
public class StoreClientConfiguration {
@Autowired
private StoreClientProperties storeClientProperties;
@Bean
public RequestInterceptor urlInterceptor() {
return template -> template.insert(0, storeClientProperties.getUrl());
}
}
Run Code Online (Sandbox Code Playgroud)在 URL 的定义中使用一些占位符FeignClient,因为它不会被使用
@FeignClient(name = "storeClient", url = "NOT_USED")
public interface StoreClient {
//..
}
Run Code Online (Sandbox Code Playgroud)现在storeclient.url可以刷新并且定义的 URL 将用于RequestTemplate发送 http 请求。
| 归档时间: |
|
| 查看次数: |
1410 次 |
| 最近记录: |