zookeeper session期限切れClator

2398 ワード

zookeeperにおけるsessionの期限切れの解釈:
 
clientとserverを接続すると、100%ではなく、ずっと接続できると保証します。例えばインターネットの問題です。では、clientは再接続が必要です。このような仕組みは自分で実現するのが複雑です。Curratorクライアントが解決してくれました。接続後にモニターを登録すればいいです。
サービスエンドの路線が通じないと模擬してファイアウォールの方法を開くことができますか?
オープン81ポート:
iptables-I INPUT-i eth 0-p tcp--dport 81-j ACCEPTiptables  -I OUT-o eth 0-p tcp--sport 81-j ACCEPT
81ポートを閉じる:iptables-I INPUT-i eth 0-p tcp--dport 81-j DROP-I OUT-o eth 0-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());
 
以下はモニターです。
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){
					
				}
			}
		}
	}	
}
 
参考記事:
http://www.codelast.com/?p=6049