好的,我会自己回答这个问题,以防其他人将来需要。
其实我终于实现了。
TLDR - 解决方案在这里:https://github.com/TheFonz2017/Spring-Cloud-Netflix-Ribbon-CF-Routing
解决办法:
- 允许在 Cloud Foundry 上使用 Ribbon,覆盖 Go-Router 的负载平衡。
- 向 Ribbon 负载均衡请求(包括重试)添加自定义路由标头,以指示 CF 的 Go-Router 将请求路由到 Ribbon(而不是其自己的负载均衡器)选择的服务实例。
- 展示如何拦截负载平衡请求
理解这一点的关键是,Spring Cloud 有自己的LoadBalancer 框架,Ribbon 只是其中一种可能的实现。同样重要的是要理解,Ribbon 仅用作负载平衡器而不是用作 HTTP 客户端。换句话说,Ribbon 的ILoadBalancer 实例仅用于从服务器列表中选择 服务实例。对选定服务器实例的请求由 Spring Cloud 的AbstractLoadBalancingClient 的实现完成。使用 Ribbon 时,这些是 RibbonLoadBalancingHttpClient 和 RetryableRibbonLoadBalancingHttpClient 的子类。
所以,我最初为 Ribbon 的 HTTP 客户端发送的请求添加 HTTP 标头的方法没有成功,因为 Spring Cloud 实际上根本不使用 Ribbon 的 HTTP / Rest 客户端。
解决方案是实现一个 Spring Cloud LoadBalancerRequestTransformer,它(与其名称相反)是一个请求拦截器。
我的解决方案使用以下实现:
public class CFLoadBalancerRequestTransformer implements LoadBalancerRequestTransformer {
public static final String CF_APP_GUID = "cfAppGuid";
public static final String CF_INSTANCE_INDEX = "cfInstanceIndex";
public static final String ROUTING_HEADER = "X-CF-APP-INSTANCE";
@Override
public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) {
System.out.println("Transforming Request from LoadBalancer Ribbon).");
// First: Get the service instance information from the lower Ribbon layer.
// This will include the actual service instance information as returned by Eureka.
RibbonLoadBalancerClient.RibbonServer serviceInstanceFromRibbonLoadBalancer = (RibbonLoadBalancerClient.RibbonServer) instance;
// Second: Get the the service instance from Eureka, which is encapsulated inside the Ribbon service instance wrapper.
DiscoveryEnabledServer serviceInstanceFromEurekaClient = (DiscoveryEnabledServer) serviceInstanceFromRibbonLoadBalancer.getServer();
// Finally: Get access to all the cool information that Eureka provides about the service instance (including metadata and much more).
// All of this is available for transforming the request now, if necessary.
InstanceInfo instanceInfo = serviceInstanceFromEurekaClient.getInstanceInfo();
// If it's only the instance metadata you are interested in, you can also get it without explicitly down-casting as shown above.
Map<String, String> metadata = instance.getMetadata();
System.out.println("Instance: " + instance);
dumpServiceInstanceInformation(metadata, instanceInfo);
if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) {
final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX));
System.out.println("Returning Request with Special Routing Header");
System.out.println("Header Value: " + headerValue);
// request.getHeaders might be immutable, so we return a wrapper that pretends to be the original request.
// and that injects an extra header.
return new CFLoadBalancerHttpRequestWrapper(request, headerValue);
}
return request;
}
/**
* Dumps metadata and InstanceInfo as JSON objects on the console.
* @param metadata the metadata (directly) retrieved from 'ServiceInstance'
* @param instanceInfo the instance info received from the (downcast) 'DiscoveryEnabledServer'
*/
private void dumpServiceInstanceInformation(Map<String, String> metadata, InstanceInfo instanceInfo) {
ObjectMapper mapper = new ObjectMapper();
String json;
try {
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(metadata);
System.err.println("-- Metadata: " );
System.err.println(json);
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(instanceInfo);
System.err.println("-- InstanceInfo: " );
System.err.println(json);
} catch (JsonProcessingException e) {
System.err.println(e);
}
}
/**
* Wrapper class for an HttpRequest which may only return an
* immutable list of headers. The wrapper immitates the original
* request and will return the original headers including a custom one
* added when getHeaders() is called.
*/
private class CFLoadBalancerHttpRequestWrapper implements HttpRequest {
private HttpRequest request;
private String headerValue;
CFLoadBalancerHttpRequestWrapper(HttpRequest request, String headerValue) {
this.request = request;
this.headerValue = headerValue;
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.putAll(request.getHeaders());
headers.add(ROUTING_HEADER, headerValue);
return headers;
}
@Override
public String getMethodValue() {
return request.getMethodValue();
}
@Override
public URI getURI() {
return request.getURI();
}
}
}
该类正在查找 Eureka 返回的服务实例元数据中设置 CF App Instance Routing 标头所需的信息。
那个信息是
- 实现服务的 CF 应用程序的 GUID,其中存在多个用于负载平衡的实例。
- 应将请求路由到的服务/应用程序实例的索引。
您需要在 服务 的 application.yml 中提供该信息,如下所示:
eureka:
instance:
hostname: ${vcap.application.uris[0]:localhost}
metadata-map:
# Adding information about the application GUID and app instance index to
# each instance metadata. This will be used for setting the X-CF-APP-INSTANCE header
# to instruct Go-Router where to route.
cfAppGuid: ${vcap.application.application_id}
cfInstanceIndex: ${INSTANCE_INDEX}
client:
serviceUrl:
defaultZone: https://eureka-server.<your cf domain>/eureka
最后,您需要在 服务使用者(在后台使用 Ribbon)的 Spring 配置中注册 LoadBalancerRequestTransformer 实现:
@Bean
public LoadBalancerRequestTransformer customRequestTransformer() {
return new CFLoadBalancerRequestTransformer();
}
因此,如果您在服务使用者中使用@LoadBalanced RestTemplate,模板将调用 Ribbon 来选择发送请求的服务实例,发送请求,拦截器将注入路由标头. Go-Router 会将请求路由到路由标头中指定的确切实例,并且不会执行任何会干扰 Ribbon 选择的额外负载平衡。
如果需要重试(针对相同或一个或多个下一个实例),拦截器将再次注入相应的路由标头 - 这次是针对 Ribbon 选择的可能不同的服务实例。
这使您可以有效地将 Ribbon 用作负载均衡器,并事实上禁用 Go-Router 的负载均衡,将其降级为单纯的代理。好处是 Ribbon 是您可以(以编程方式)影响的东西,而您对 Go-Router 几乎没有影响。
注意:这是针对@LoadBalanced RestTemplate 的测试并且有效。
但是,对于@FeignClients,它不能以这种方式工作。
在this post 中描述了我为 Feign 解决此问题的最接近的方法,但是,那里描述的解决方案使用了一个无法访问(功能区)选择的服务实例的拦截器,因此不允许访问所需的元数据。
到目前为止还没有为FeignClient找到解决方案。