专栏首页码匠的流水账聊聊nacos的ServerChangeListener

聊聊nacos的ServerChangeListener

本文主要研究一下nacos的ServerChangeListener

ServerChangeListener

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/cluster/servers/ServerChangeListener.java

public interface ServerChangeListener {

    /**
     * If member list changed, this method is invoked.
     *
     * @param servers servers after change
     */
    void onChangeServerList(List<Server> servers);

    /**
     * If reachable member list changed, this method is invoked.
     *
     * @param healthyServer reachable servers after change
     */
    void onChangeHealthyServerList(List<Server> healthyServer);
}
  • ServerChangeListener定义了onChangeServerList、onChangeHealthyServerList方法

DistroMapper

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/core/DistroMapper.java

@Component("distroMapper")
public class DistroMapper implements ServerChangeListener {

    private List<String> healthyList = new ArrayList<>();

    //......

    @Override
    public void onChangeServerList(List<Server> latestMembers) {

    }

    @Override
    public void onChangeHealthyServerList(List<Server> latestReachableMembers) {

        List<String> newHealthyList = new ArrayList<>();
        for (Server server : latestReachableMembers) {
            newHealthyList.add(server.getKey());
        }
        healthyList = newHealthyList;
    }

    //......
}
  • DistroMapper实现了ServerChangeListener接口,其onChangeHealthyServerList会更新自己的healthyList

RaftPeerSet

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java

@Component
@DependsOn("serverListManager")
public class RaftPeerSet implements ServerChangeListener, ApplicationContextAware {

    @Autowired
    private ServerListManager serverListManager;

    private ApplicationContext applicationContext;

    private AtomicLong localTerm = new AtomicLong(0L);

    private RaftPeer leader = null;

    private Map<String, RaftPeer> peers = new HashMap<>();

    private Set<String> sites = new HashSet<>();

    private boolean ready = false;

    //......

    @Override
    public void onChangeServerList(List<Server> latestMembers) {

        Map<String, RaftPeer> tmpPeers = new HashMap<>(8);
        for (Server member : latestMembers) {

            if (peers.containsKey(member.getKey())) {
                tmpPeers.put(member.getKey(), peers.get(member.getKey()));
                continue;
            }

            RaftPeer raftPeer = new RaftPeer();
            raftPeer.ip = member.getKey();

            // first time meet the local server:
            if (NetUtils.localServer().equals(member.getKey())) {
                raftPeer.term.set(localTerm.get());
            }

            tmpPeers.put(member.getKey(), raftPeer);
        }

        // replace raft peer set:
        peers = tmpPeers;

        if (RunningConfig.getServerPort() > 0) {
            ready = true;
        }

        Loggers.RAFT.info("raft peers changed: " + latestMembers);
    }

    @Override
    public void onChangeHealthyServerList(List<Server> latestReachableMembers) {

    }

    //......
}
  • RaftPeerSet实现了ServerChangeListener接口,其onChangeServerList方法会更新peers及ready属性

小结

ServerChangeListener定义了onChangeServerList、onChangeHealthyServerList方法;DistroMapper实现了ServerChangeListener接口,其onChangeHealthyServerList会更新自己的healthyList;RaftPeerSet实现了ServerChangeListener接口,其onChangeServerList方法会更新peers及ready属性

doc

  • ServerChangeListener

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-10-01

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊nacos的ServerChangeListener

    nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/cluster/servers/Server...

    codecraft
  • 聊聊Elasticsearch的ExponentiallyWeightedMovingAverage

    本文主要研究一下Elasticsearch的ExponentiallyWeightedMovingAverage

    codecraft
  • 聊聊openjdk的BufferPoolMXBean

    java.management/java/lang/management/PlatformManagedObject.java

    codecraft
  • 聊聊nacos的ServerChangeListener

    nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/cluster/servers/Server...

    codecraft
  • MISC-多彩

    生平第一次这么认真看口红色号谁能想到是因为一道MISC题呢,吐了这个弱智题目,如今的网站根本没有当时题目上的那些色号,亏我还去淘宝京东都问了一遍,最后才想起来去...

    Elapse
  • Android 中IntentFilter的匹配规则实例详解

    假如我们要隐式启动一个Activity我们需要成功匹配activity中的任意一个intent-filter。如果要成功匹配该intent-filter我们需要...

    砸漏
  • NAB SHOW 2018视频影像专题丨HHI带来全新VR视频编解码器,夏普将展示其8K生态系统

    VRPinea
  • 【程序猿水贴】风是如何形成的 | 大气运动的原因

    最近看新闻偶然和老爸聊到最新的疫情,老爸随口说这病毒会被风刮到对面的M国去,我反驳说这是病毒又不是细菌。后来老爸又说形成风的原因是地球自转,我笑到说怎么可能,风...

    浩Coding
  • Go语言调度器之创建main goroutine(13)

    上一节我们分析了调度器的初始化,这一节我们来看程序中的第一个goroutine是如何创建的。

    阿波张
  • Netty 在 Dubbo 中是如何应用的?

    众所周知,国内知名框架 Dubbo 底层使用的是 Netty 作为网络通信,那么内部到底是如何使用的呢?今天我们就来一探究竟。

    IT大咖说

扫码关注云+社区

领取腾讯云代金券