Ribbon源码分析

配置

引用eureka依赖的时候内部就已经带人了ribbon,不用单独引用
image.png

image.png

image.png
调用

image.png

流程图

image.png

入口

image.png

image.png
相关代码

image.png

image.png

image.png
感兴趣的可以看看这个:www.codingsky.com/doc/2020/6/…

image.png

debug

image.png

image.png

image.png
一路追踪

image.png
每隔10秒比对一次
image.png

image.png

image.png
定时任务ping服务状态的相关逻辑

com.netflix.loadbalancer.BaseLoadBalancer.Pinger#runPinger
复制代码

从eureka获取服务注册的信息

image.png

image.png

image.png

image.png

image.png

image.png

image.png

那ribbon是怎么更新eureka的服务呢?看下去

image.png
处理逻辑 每隔30秒更新一次

image.png

public synchronized void start(final UpdateAction updateAction) {
    if (isActive.compareAndSet(false, true)) {
        final Runnable wrapperRunnable = new Runnable() {
            @Override
            public void run() {
                if (!isActive.get()) {
                    if (scheduledFuture != null) {
                        scheduledFuture.cancel(true);
                    }
                    return;
                }
                try {
                    updateAction.doUpdate();
                    lastUpdated = System.currentTimeMillis();
                } catch (Exception e) {
                    logger.warn("Failed one update cycle", e);
                }
            }
        };

        scheduledFuture = getRefreshExecutor().scheduleWithFixedDelay(
                wrapperRunnable,
                initialDelayMs,
                refreshIntervalMs,
                TimeUnit.MILLISECONDS
        );
    } else {
        logger.info("Already active, no-op");
    }
}
复制代码

image.png

image.png

后记

到这里就主要流程分析完成了,可能有不足,哈哈哈.下期Feign,敬请期待!