LoadBalancerAutoConfiguration负载均衡自动配置里面用于传入负载均衡拦截器LoadBalancerInterceptor
spring 接口
package org.springframework.cloud.client.loadbalancer;
/**
* Represents a client side load balancer
* @author Spencer Gibb
*/
public interface LoadBalancerClient {
ServiceInstance choose(String serviceId);
<T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException;
URI reconstructURI(ServiceInstance instance, URI original);
}
一共就3个方法,
- 选择 选择具体一个服务实例
- 执行 执行请求
- 重新构造url 把url中原来写的服务名 换掉 换成实际的
只有 一实现类RibbonLoadBalancerClient
看里面的reconstructURI
方法
/
* @param instance 被选中的服务实例
* @param original 原来写的url 里面直接写的服务名
* @return 服务名替换掉的 实际url
*/
@Override
public URI reconstructURI(ServiceInstance instance, URI original) {
Assert.notNull(instance, "instance can not be null");
String serviceId = instance.getServiceId();
//clientFactory是 创造 负载均衡器 的工厂
RibbonLoadBalancerContext context = this.clientFactory
//每个ribbon客户端 都会给生成一个 spring上下文
.getLoadBalancerContext(serviceId);
//被选中的传入的服务实例 转成RibbonLoadBalancerContext需要的Server
Server server = new Server(instance.getHost(), instance.getPort());
boolean secure = isSecure(server, serviceId);
URI uri = original;
if (secure) {
uri = UriComponentsBuilder.fromUri(uri).scheme("https").build().toUri();
}
//最终让全部交给RibbonLoadBalancerContext来干
return context.reconstructURIWithServer(server, uri);
}
RibbonLoadBalancerClient
的execute
方法
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
//按一定策略选中具体的服务实例
Server server = getServer(loadBalancer);
if (server == null) {
throw new IllegalStateException("No instances available for " + serviceId);
}
//服务实例包装一下 加入其他信息
RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server,
serviceId), serverIntrospector(serviceId).getMetadata(server));
RibbonLoadBalancerContext context = this.clientFactory
.getLoadBalancerContext(serviceId);
//用来对服务的请求做跟踪记录的
RibbonStatsRecorder statsRecorder = new RibbonStatsRecorder(context, server);
try {
//向这个具体的服务实例发起请求
//apply方法接受一个ServiceInstance 是对一个服务实例的抽象定义,
//RibbonServer是对他的一个实现
T returnVal = request.apply(ribbonServer);
statsRecorder.recordStats(returnVal);
return returnVal;
}
// catch IOException and rethrow so RestTemplate behaves correctly
catch (IOException ex) {
statsRecorder.recordStats(ex);
throw ex;
}
catch (Exception ex) {
statsRecorder.recordStats(ex);
ReflectionUtils.rethrowRuntimeException(ex);
}
return null;
}
首先就是 用serviceId得具体服务实例,怎么选择的由ILoadBalancer决定,这是Betflix Ribbon的接口 里面有个chooseServer方法
任何包装下Server 变为 RibbonServer 传入LoadBalancerRequest.apply(ServiceInstance instance) 发送请求
apply是定义在LoadBalancerInterceptor里面的匿名类的
@Override
public ClientHttpResponse apply(final ServiceInstance instance)throws Exception {
//ServiceRequestWrapper 是内部类 见下面 getURI()方法被重写了
HttpRequest serviceRequest = new ServiceRequestWrapper(request,instance);
//里面会调用这里被重写的getURI()
return execution.execute(serviceRequest, body);
}
private class ServiceRequestWrapper extends HttpRequestWrapper {
private final ServiceInstance instance;
public ServiceRequestWrapper(HttpRequest request, ServiceInstance instance) {
super(request);
this.instance = instance;
}
//这个方法被重写 用的是loadBalancer 也就是RibbonLoadBalancerClient 里面根据传入的服务实例改写原来的url
@Override
public URI getURI() {
URI uri = LoadBalancerInterceptor.this.
//之前说过的 reconstructURI 组织具体请求的服务器实例地址
loadBalancer.reconstructURI(this.instance, getRequest().getURI());
return uri;
}
}