zookeepersession過期該如何理解

zookeeper session過期該如何理解,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)10多年成都定制網(wǎng)站服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及高端網(wǎng)站定制服務(wù),成都定制網(wǎng)站及推廣,對(duì)成都石雕等多個(gè)方面擁有豐富的網(wǎng)站推廣經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。

zookeeper 中 session 過期解釋:

當(dāng)client 和 server 連接后,不是100%保證一直可以連上的。比如網(wǎng)絡(luò)問題。那么client需要重連,這種機(jī)制自己實(shí)現(xiàn)比較復(fù)雜,還在有Curator客戶端幫我們解決了,只需要在連接后注冊(cè)一個(gè)監(jiān)聽器就可以了。

模擬服務(wù)端線路不通可以開啟防火墻方法,或者,

開啟81端口:
iptables -I INPUT -i eth0 -p tcp --dport 81 -j ACCEPT
iptables  -I OUTPUT -o eth0 -p tcp --sport 81 -j ACCEPT
關(guān)閉81端口:
iptables -I INPUT -i eth0 -p tcp --dport 81 -j DROP
iptables -I OUTPUT -o eth0 -p tcp --sport 81 -j DROP

然后保存

具體代碼如下: 

代碼如下:

String path = "/session/service-";
		SessionConnectionStateListener listener = new SessionConnectionStateListener(path,zookeeperConnectionString);
		client.getConnectionStateListenable().addListener(listener);
		client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
		.forPath(path,"haha".getBytes());

下面是監(jiān)聽器:

package com.mmblue.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.zookeeper.CreateMode;

public class SessionConnectionStateListener implements ConnectionStateListener {
	private String zkRegPathPrefix;
	private String regContent;

	public SessionConnectionStateListener(String zkRegPathPrefix, String regContent) {
		this.zkRegPathPrefix = zkRegPathPrefix;
		this.regContent = regContent;
	}

	@Override
	public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState){
		if(connectionState == ConnectionState.LOST){
			while(true){
				try {
					System.err.println("我來了,嘿嘿");
					if(curatorFramework.getZookeeperClient().blockUntilConnectedOrTimedOut()){
						curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(zkRegPathPrefix, regContent.getBytes("UTF-8"));
						break;
					}
				} catch (InterruptedException e) {
					break;
				} catch (Exception e){
					
				}
			}
		}
	}	
}

關(guān)于zookeeper session過期該如何理解問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)站欄目:zookeepersession過期該如何理解
URL鏈接:http://bm7419.com/article46/iicoeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、ChatGPT外貿(mào)建站、網(wǎng)站收錄、定制網(wǎng)站、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)