本篇內(nèi)容介紹了“nacos的DistroMapper有什么作用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
目前成都創(chuàng)新互聯(lián)公司已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、丹東網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
本文主要研究一下nacos的DistroMapper
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(Listservers); /** * If reachable member list changed, this method is invoked. * * @param healthyServer reachable servers after change */ void onChangeHealthyServerList(List healthyServer); }
ServerChangeListener定義了onChangeServerList、onChangeHealthyServerList方法
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/core/DistroMapper.java
@Component("distroMapper") public class DistroMapper implements ServerChangeListener { private ListhealthyList = new ArrayList<>(); public List getHealthyList() { return healthyList; } @Autowired private SwitchDomain switchDomain; @Autowired private ServerListManager serverListManager; /** * init server list */ @PostConstruct public void init() { serverListManager.listen(this); } public boolean responsible(Cluster cluster, Instance instance) { return switchDomain.isHealthCheckEnabled(cluster.getServiceName()) && !cluster.getHealthCheckTask().isCancelled() && responsible(cluster.getServiceName()) && cluster.contains(instance); } public boolean responsible(String serviceName) { if (!switchDomain.isDistroEnabled() || SystemUtils.STANDALONE_MODE) { return true; } if (CollectionUtils.isEmpty(healthyList)) { // means distro config is not ready yet return false; } int index = healthyList.indexOf(NetUtils.localServer()); int lastIndex = healthyList.lastIndexOf(NetUtils.localServer()); if (lastIndex < 0 || index < 0) { return true; } int target = distroHash(serviceName) % healthyList.size(); return target >= index && target <= lastIndex; } public String mapSrv(String serviceName) { if (CollectionUtils.isEmpty(healthyList) || !switchDomain.isDistroEnabled()) { return NetUtils.localServer(); } try { return healthyList.get(distroHash(serviceName) % healthyList.size()); } catch (Exception e) { Loggers.SRV_LOG.warn("distro mapper failed, return localhost: " + NetUtils.localServer(), e); return NetUtils.localServer(); } } public int distroHash(String serviceName) { return Math.abs(serviceName.hashCode() % Integer.MAX_VALUE); } @Override public void onChangeServerList(List latestMembers) { } @Override public void onChangeHealthyServerList(List latestReachableMembers) { List newHealthyList = new ArrayList<>(); for (Server server : latestReachableMembers) { newHealthyList.add(server.getKey()); } healthyList = newHealthyList; } }
DistroMapper實(shí)現(xiàn)了ServerChangeListener接口,其onChangeHealthyServerList方法會(huì)更新healthyList
它還提供了responsible方法,該方法在switchDomain.isHealthCheckEnabled以及cluster.getHealthCheckTask()不是cancelled的情況下會(huì)執(zhí)行responsible,該方法會(huì)調(diào)用distroHash來(lái)計(jì)算hash值
它還提供了mapSrv方法,也是通過(guò)distroHash計(jì)算hash然后與healthyList的大小取余,最后返回server的key
ServerChangeListener定義了onChangeServerList、onChangeHealthyServerList方法;DistroMapper實(shí)現(xiàn)了ServerChangeListener接口,其onChangeHealthyServerList方法會(huì)更新healthyList;DistroMapper還提供了responsible方法及mapSrv方法
“nacos的DistroMapper有什么作用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!