Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save daemin-hwang/27715a3f9ba1f410a2a8b54908b05712 to your computer and use it in GitHub Desktop.

Select an option

Save daemin-hwang/27715a3f9ba1f410a2a8b54908b05712 to your computer and use it in GitHub Desktop.
주키퍼 클라이언트 구현
- 주키퍼내의 각 ZNODE는 하위 노드를 가질 수 있다.
- //자기자신에게 와처를 등록하고, 하위 노드들을 위한 콜백을 등록한다.
//node, watcher, callback, context 순으로 넘긴다.
zk.getChildren(znode, this, this, null);
//znode기준으로 하위 노드를 현재 클래스의 hostname으로 만든다. ACL, 노드생성방식을 지정해야 한다. /myteam + /{hostname}
- zk.create(znode + "/" + hostname, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
- 와쳐는 안번쓰이고 버려진다. 따라서 와처의 process를 처리하고 나면 다시 와처를 붙여줘야한다.
- 이때 대상 노드 뿐 아니라 자식노드의 변화에 따른 콜백을 등록하고자 한다면 ChildrenCallback구현한 구체클래스를 인자로 넘긴다.
///////////////////////////////////////////////////
// 클라이언트 설정 참고용 코드
package com.naver.editor.core.config;
/**
* Created by Naver on 2016-04-01.
*/
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@Slf4j
@Service
public class SpellerHealthChecker implements Watcher, AsyncCallback.ChildrenCallback {
@Value("#{spellerZookeeper.connectUrl}")
private String connectUrl;
@Value("#{spellerZookeeper.znode}")
private String znode;
private ZooKeeper zk;
private AtomicBoolean dead = new AtomicBoolean();;
@PostConstruct
public void postConstruct() throws IOException, KeeperException, InterruptedException {
Assert.hasLength(connectUrl, "connectUrl must be not empty.");
Assert.hasLength(znode, "znode must be not empty.");
zk = new ZooKeeper(connectUrl, 30000, this); //세션타임아웃 30초
String hostname = InetAddress.getLocalHost().getHostName();
zk.create(znode + "/" + hostname, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
//node, watcher, callback, context
//자기자신에게 와처를 등록하고, 하위 노드들을 위한 콜백을 등록한다.
//zk.getChildren(znode, this, this, null);
zk.getChildren(znode, this);
}
//와처 인터페이스 구현
@Override
public void process(WatchedEvent event) {
log.debug("path 1 {} :" , event.getPath());
if (event.getType() == Event.EventType.NodeChildrenChanged) {
log.info("{}", "Event.EventType.NodeChildrenChanged" );
zk.getChildren(znode, this, this, null);
}
}
//하위 노드에게 와처를 할당할때 호출되는 콜백
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
log.debug("path 2 {} :" , path);
for(String child : children) {
log.info("node name : {}", child);
}
if (CollectionUtils.isEmpty(children)) {
dead.set(true);
log.error("{}", "speller server all die !!");
} else {
dead.set(false);
}
}
public boolean isDead() {
return dead.get();
}
}
참조링크 :
http://idkbj.tistory.com/93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment