本篇內容介紹了“nacos的DistroMapper有什么作用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
目前成都創新互聯公司已為上千的企業提供了網站建設、域名、雅安服務器托管、網站改版維護、企業網站設計、丹東網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
本文主要研究一下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(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方法
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<>(); public List<String> 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<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
它還提供了responsible方法,該方法在switchDomain.isHealthCheckEnabled以及cluster.getHealthCheckTask()不是cancelled的情況下會執行responsible,該方法會調用distroHash來計算hash值
它還提供了mapSrv方法,也是通過distroHash計算hash然后與healthyList的大小取余,最后返回server的key
ServerChangeListener定義了onChangeServerList、onChangeHealthyServerList方法;DistroMapper實現了ServerChangeListener接口,其onChangeHealthyServerList方法會更新healthyList;DistroMapper還提供了responsible方法及mapSrv方法
“nacos的DistroMapper有什么作用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注創新互聯網站,小編將為大家輸出更多高質量的實用文章!
網頁名稱:nacos的DistroMapper有什么作用
地址分享:http://vcdvsql.cn/article0/poohoo.html
成都網站建設公司_創新互聯,為您提供、網站制作、手機網站建設、關鍵詞優化、外貿網站建設、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯