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服务状态的相关逻辑

1
bash复制代码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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
java复制代码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,敬请期待!

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%