我有一個春天的雲應用程序,我自定義功能區客戶端作爲解釋here in section Customizing the Ribbon Client我的iRule是這樣的:通行證請求頭到功能區的iRule關鍵參數
public class HeadersRule extends AbstractLoadBalancerRule {
public HeadersRule() {
}
public HeadersRule(ILoadBalancer lb) {
this();
this.setLoadBalancer(lb);
}
public Server choose(ILoadBalancer lb, Object key) {
//I want the key to contain the headers from the request so I can decide choose the server by one of the headers
}
我休息控制器:
@RequestMapping("/")
public String hello(HttpServletRequest request, HttpServletResponse response) {
//here I want to pass the key parameter to ribbon
return result;
}
我想在我的IRule中通過其中一個標頭的值選擇下一個服務器。 我如何傳遞的標頭,以我的自定義的iRule關鍵參數?(通過RestTemplate或假裝或者如果你有一個使用色帶另一個選項...)類AbstractLoadBalancerAwareClient
編輯可能的方向
public T executeWithLoadBalancer(final S request, final IClientConfig requestConfig) throws ClientException {
RequestSpecificRetryHandler handler = getRequestSpecificRetryHandler(request, requestConfig);
LoadBalancerCommand<T> command = LoadBalancerCommand.<T>builder()
.withLoadBalancerContext(this)
.withRetryHandler(handler)
.withLoadBalancerURI(request.getUri())
.build();
構建負載平衡器命令和省略:
.withServerLocator(request)
會完成這項工作! 我可以從配置覆蓋此方法,在Spring RibbonClientConfiguration類我可以配置:
@Bean
@Lazy
@ConditionalOnMissingBean
public RestClient ribbonRestClient(IClientConfig config, ILoadBalancer loadBalancer) {
RestClient client = new OverrideRestClient(config);
client.setLoadBalancer(loadBalancer);
Monitors.registerObject("Client_" + this.name, client);
return client;
}
的問題是,一些與名稱不工作:
@Value("${ribbon.client.name}")
private String name = "client";
似乎有是一些配置應該用這個名字來完成,因爲我看到我的負載均衡服務器列表總是空的,出於某種原因,如果有人知道我應該如何配置這個屬性我相信它可以解決問題...
功能區不知道當前的請求。它也運行在另一個線程中。 – spencergibb
如果我可以將IRule選擇函數的關鍵參數傳遞給其餘模板,並將其傳遞給底層功能區,它將解決問題,我只是不知道如何...(可能會將自定義攔截器添加到其餘模板中,或者重寫另一個類,如負載平衡器......) –
這可能是以下模式的一種:https://github.com/jmnarloch/ribbon-discovery-filter-spring-cloud-首發 – spencergibb