负载均衡-ribbon源码解析
1 @LoadBalanced注解
/*** 基于ribbon调用服务及负载均衡* @return*/
@LoadBalanced
@Bean
public RestTemplate restTemplate(){return new RestTemplate();
}
@Bean
@ConditionalOnMissingBean
public RestTemplateCustomizer restTemplateCustomizer(final LoadBalancerInterceptor loadBalancerInterceptor) {return (restTemplate) -> {// 获取拦截器List<ClientHttpRequestInterceptor> list = new ArrayList(restTemplate.getInterceptors());// 添加一个新的拦截器(负载均衡的拦截器,该拦截器就是在restTemplate发送请求之前执行)list.add(loadBalancerInterceptor);// 将拦截器设置到restTemplate中 该restTemplate就是在OrderApplication中添加了@LoadBalanced的注解restTemplate.setInterceptors(list);};
}
总上所属:@LoadBalanced就是对template起到标识作用,加了@LoadBalanced注解的template开启负载均衡
2 源码解析
1)通过restTemplate发起请求
Result result = restTemplate.getForObject("http://product-server/product/" + id, Result.class);
2)执行拦截器的intercept方法
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException {URI originalUri = request.getURI();String serviceName = originalUri.getHost();Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);// 调用loadBalancer的execute方法 loadBalancer:负载均衡器// serviceName:就是product-serverreturn (ClientHttpResponse)this.loadBalancer.execute(serviceName, this.requestFactory.createRequest(request, body, execution));
}
3)查看RibbonLoadBalancerClient中的execute方法
public <T> T execute(String serviceId, LoadBalancerRequest<T> request, Object hint) throws IOException {// 通过负载均衡器 ILoadBalancer loadBalancer = this.getLoadBalancer(serviceId);// 通过负载均衡器 接 负载均衡算法 获取服务实例Server server = this.getServer(loadBalancer, hint);if (server == null) {throw new IllegalStateException("No instances available for " + serviceId);} else {RibbonLoadBalancerClient.RibbonServer ribbonServer = new RibbonLoadBalancerClient.RibbonServer(serviceId, server, this.isSecure(server, serviceId), this.serverIntrospector(serviceId).getMetadata(server));return this.execute(serviceId, (ServiceInstance)ribbonServer, (LoadBalancerRequest)request);}
}
4)根据负载均衡算法获取服务实例
protected Server getServer(ILoadBalancer loadBalancer, Object hint) {return loadBalancer == null ? null : loadBalancer.chooseServer(hint != null ? hint : "default");
}
public Server chooseServer(Object key) {if (this.counter == null) {this.counter = this.createCounter();}this.counter.increment();if (this.rule == null) {return null;} else {try {// rule就是配置的负载均衡算法// choose就是根据指定的负载均衡算法获取服务实例return this.rule.choose(key);} catch (Exception var3) {logger.warn("LoadBalancer [{}]: Error choosing server for key {}", new Object[]{this.name, key, var3});return null;}}
}
// 默认的负载均衡器
private static final IRule DEFAULT_RULE = new RoundRobinRule();
}
}
}
```java
// 默认的负载均衡器
private static final IRule DEFAULT_RULE = new RoundRobinRule();