久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

elasticsearch怎么實現客戶端負載均衡

152次閱讀
沒有評論

共計 3404 個字符,預計需要花費 9 分鐘才能閱讀完成。

本篇內容介紹了“elasticsearch 怎么實現客戶端負載均衡”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

  客戶端負載均衡技術是,客戶端維護一組服務器引用,每次客戶端請求的時候,會根據負載均衡算法選中一個節點,發送請求。常用的負載算法有 Random,Round robin,Hash,StaticWeighted 等。ES 的客戶端負載使用了 Round robin 算法。(另外 Hash 一致性算法還會在另一地方遇見的) 一個 Count 請求的整個客戶端模塊的調用流程是

簡化的調用流程

client 提供了客戶端的操作接口,比如 count()

nodesService 的 execute() 隨機一個節點出來

Proxy 代理通過 transportService 發送請求

一些初始化的事情

我們先看下創建客戶端的代碼,這里配置了幾個配置項

Settings settings = ImmutableSettings.settingsBuilder()
 .put(cluster.name ,  myClusterName)
 .put(client.transport.sniff , true).build();
client=new TransportClient(settings)
 .addTransportAddress(new InetSocketTransportAddress( localhost ,9300));

誰會在乎這些配置項呢,就是 TransportClientNodesService 類。它負責著嗅探,維護集群節點列表。選舉節點。它的構造方法里做了一些初始化工作

 this.nodesSamplerInterval = componentSettings.getAsTime(nodes_sampler_interval , timeValueSeconds(5));
 this.pingTimeout = componentSettings.getAsTime(ping_timeout , timeValueSeconds(5)).millis();
 this.ignoreClusterName = componentSettings.getAsBoolean(ignore_cluster_name , false);
 //.....
 if (componentSettings.getAsBoolean( sniff , false)) { this.nodesSampler = new SniffNodesSampler();
 } else { this.nodesSampler = new SimpleNodeSampler();
 }
 this.nodesSamplerFuture = threadPool.schedule(nodesSamplerInterval, ThreadPool.Names.GENERIC, new ScheduledNodeSampler());

nodes_sampler_interval  嗅探集群節點的間隔,默認 5 秒

pingTimeout Ping 節點的超時時間,默認是 5 秒

ignoreClusterName  忽略集群名稱,集群驗證的時候

sniff  是否開啟集群嗅探

另外 TransportClientNodesService 維護著 2 個列表,集群節點列表 nodes 和監聽列表 listedNodes。其中監聽列表就是通過 TransportClient.addTransportAddress() 增加的節點列表。

嗅探集群節點

 interface NodeSampler { void sample();
 }

NodeSampler 接口很簡單,只有一個 sample() 方法,它的實現類有 2 個 SniffNodesSampler 和 SimpleNodeSampler,我們在初始化里已經看到了,如果 sniff 配置項是 true 的話使用 SniffNodesSampler 類。它們 2 個的實現邏輯是

SimpleNodeSampler 

循環 listedNodes 列表里的每一個節點

沒有連接就連接到這個節點

發送 cluster/nodes/info 請求獲取節點的集群名稱,需要的話,做集群名驗證。

增加到 nodes 節點列表

SniffNodesSampler

創建一個 listedNodes 和 nodes 去重后的列表 nodesToPing

循環 nodesToPing 里的每一個節點

沒有連接就連接到這個節點,如果是 nodes 列表的就正常連接,listedNode 列表的建立個輕連接就好了

發送 cluster/state 請求獲取節點的狀態,這里會有集群所有的數據節點 dataNodes

再次確認下已經和所有節點建立連接

增加到 nodes 節點列表

       

  我們可以發現 SimpleNodeSampler 最終的節點列表還是 listedNodes,如果我們建立客戶端的時候,只添加了一個 localhost,那它所有的請求都會發送到 localhost。只有 SniffNodesSampler 才去探測集群的所有節點。也就是 SimpleNodeSampler 的意圖是讓集群中的某些個節點,專門用于接受用戶請求。SniffNodesSampler 的話,所有節點都會參與負載。

class ScheduledNodeSampler implements Runnable {
 @Override
 public void run() {
 try { nodesSampler.sample();
 if (!closed) { nodesSamplerFuture = threadPool.schedule(nodesSamplerInterval, ThreadPool.Names.GENERIC, this);
 }
 } catch (Exception e) { logger.warn( failed to sample , e);
 }
 }
}

ScheduledNodeSampler 線程啟動后,Sampler 就開始忙碌起來了。

選舉節點

有了集群節點列表后 execute() 方法就可以通過輪詢調度算法 Round robin,選舉節點了。算法的特點是實現起來簡單優雅,請求會被均衡的發送到各個節點上。

public  T  T execute(NodeCallback T  callback) throws ElasticSearchException {
 ImmutableList DiscoveryNode  nodes = this.nodes;
 if (nodes.isEmpty()) { throw new NoNodeAvailableException();
 }
 int index = randomNodeGenerator.incrementAndGet();
 if (index   0) {
 index = 0;
 randomNodeGenerator.set(0);
 }
 for (int i = 0; i   nodes.size(); i++) { DiscoveryNode node = nodes.get((index + i) % nodes.size());
 try { return callback.doWithNode(node);
 } catch (ElasticSearchException e) { if (!(e.unwrapCause() instanceof ConnectTransportException)) {
 throw e;
 }
 }
 }
 throw new NoNodeAvailableException();}

關鍵是這一行代碼,說那么多話,其實,我只是了為了這么,一行代碼啊?

DiscoveryNode node = nodes.get((index + i) % nodes.size());

“elasticsearch 怎么實現客戶端負載均衡”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-16發表,共計3404字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 郑州市| 正宁县| 三明市| 高要市| 万安县| 屏东县| 白城市| 淮北市| 无为县| 富源县| 镇巴县| 屏南县| 西青区| 洛宁县| 农安县| 会泽县| 松滋市| 旬阳县| 休宁县| 马尔康县| 分宜县| 灵武市| 舞钢市| 盐亭县| 宿迁市| 门头沟区| 江山市| 巩义市| 叙永县| 堆龙德庆县| 封开县| 芦山县| 理塘县| 重庆市| 香港 | 微山县| 台湾省| 湾仔区| 景洪市| 西峡县| 河津市|