Ribbon|Ribbon 负载均衡

core:Java功能增强 —— 拦截器 通过@LoadBalanced修饰就可以为应用引入Ribbon框架来实现负载均衡。

@Configuration public class ConfigBean {/** * 使用@LoadBalanced开启负载均衡 * RestTemplate 通过它来访问服务 * */ @LoadBalanced @Bean RestTemplate restTemplate() { return new RestTemplate(); } }

使用时这样(jServer1就是注册在eureka的服务名):
@RestController @RequestMapping("/") public class HelloController { @Autowired RestTemplate restTemplate; @RequestMapping("/hello") public String hello() { String result = restTemplate.getForEntity("http://jServer1/hello",String.class).getBody(); System.out.println(result); return result; } }

所以Ribbon的核心在于@LoadBalanced,那它引入了哪些东西呢?进入看一下:

Ribbon|Ribbon 负载均衡
文章图片
image.png
立刻明白了什么,是老朋友拦截器啊,所以是拦截了请求,然后根据负载均衡策略找到合适的服务实例,再发请求出去么。
intercept方法如下:
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException { URI originalUri = request.getURI(); String serviceName = originalUri.getHost(); return (ClientHttpResponse)this.loadBalancer.execute(serviceName, new LoadBalancerRequest() { public ClientHttpResponse apply(ServiceInstance instance) throws Exception { HttpRequest serviceRequest = LoadBalancerInterceptor.this.new ServiceRequestWrapper(request, instance); return execution.execute(serviceRequest, body); } }); }

进到execute方法:
public T execute(String serviceId, LoadBalancerRequest request) throws IOException { ILoadBalancer loadBalancer = this.getLoadBalancer(serviceId); //就是这里了!! Server server = this.getServer(loadBalancer); 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)); RibbonLoadBalancerContext context = this.clientFactory.getLoadBalancerContext(serviceId); RibbonStatsRecorder statsRecorder = new RibbonStatsRecorder(context, server); try { T returnVal = request.apply(ribbonServer); statsRecorder.recordStats(returnVal); return returnVal; } catch (IOException var9) { statsRecorder.recordStats(var9); throw var9; } catch (Exception var10) { statsRecorder.recordStats(var10); ReflectionUtils.rethrowRuntimeException(var10); return null; } } } protected Server getServer(ILoadBalancer loadBalancer) { return loadBalancer == null ? null : loadBalancer.chooseServer("default"); }

以最基本的BaseLoadBalancer为例(还有其他Balancer实现):
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 = DEFAULT_RULE = new RoundRobinRule(); return this.rule.choose(key); } catch (Throwable var3) { return null; } } }

【Ribbon|Ribbon 负载均衡】可以发现是根据IRule.choose(key)来选择服务器实例的,而IRule就是负载均衡策略接口,可以看出已经默认实现了很多种策略:

Ribbon|Ribbon 负载均衡
文章图片
image.png

    推荐阅读