负载均衡-ribbon源码解析

负载均衡-ribbon源码解析

1 @LoadBalanced注解

java 复制代码
/**
 * 基于ribbon调用服务及负载均衡
 * @return
 */
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
    return new RestTemplate();
}
java 复制代码
@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发起请求

java 复制代码
Result result = restTemplate.getForObject("http://product-server/product/" + id, Result.class);

2)执行拦截器的intercept方法

java 复制代码
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-server
    return (ClientHttpResponse)this.loadBalancer.execute(serviceName, this.requestFactory.createRequest(request, body, execution));
}

3)查看RibbonLoadBalancerClient中的execute方法

java 复制代码
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)根据负载均衡算法获取服务实例

java 复制代码
protected Server getServer(ILoadBalancer loadBalancer, Object hint) {
    return loadBalancer == null ? null : loadBalancer.chooseServer(hint != null ? hint : "default");
}
java 复制代码
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;
        }
    }
}
java 复制代码
// 默认的负载均衡器
private static final IRule DEFAULT_RULE = new RoundRobinRule();

}

}

}

复制代码
```java
// 默认的负载均衡器
private static final IRule DEFAULT_RULE = new RoundRobinRule();
相关推荐
老坛程序员1 天前
Coze 与 n8n 深度对比:AI智能体平台与工作流自动化的核心博弈
运维·人工智能·自动化
Zz_waiting.1 天前
多机部署,负载均衡
运维·负载均衡
Mr.45671 天前
Linux CentOS 7 安装配置HAProxy完整指南:实现高可用负载均衡
linux·centos·负载均衡
玉树临风江流儿1 天前
Linux驱动开发总结速记
linux·运维·驱动开发
七宝大爷1 天前
GPU服务器深度解析:H100/H200的“机头”与“模组”架构详解
运维·服务器·架构·h100·h200
A-花开堪折1 天前
Qemu 嵌入式Linux驱动开发
linux·运维·驱动开发
磊灬泽1 天前
【Linux驱动开发】PWM子系统-servo
linux·运维·算法
郝学胜-神的一滴1 天前
Linux系统函数stat和lstat详解
linux·运维·服务器·开发语言·c++·程序人生·软件工程
Mr.亮先生1 天前
常用、高效、实用的 Linux 服务器监控与运维工具清单
linux·运维·服务器
极客先躯1 天前
高可用巡检脚本实战:一键掌握服务、网络、VIP、资源状态
运维·网络·金融