Zookeeper? Curator ?
주키퍼를 이용해 master 를 점유하고, 작업을 분배하는걸 만들려고 했는데
API 가 callback 구조로 만들어서 꽤 복잡하다.
그래서, 넷플릭스에서 만들고 아파치재단에 탑프로젝트로 올라온 curator 라는 프로젝트가 존재했다.
고수준의 주키퍼 API 랄까?
샘플코드와 오류!
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.leader.LeaderSelector; import org.apache.curator.framework.recipes.leader.LeaderSelectorListener; import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.*; import java.io.Closeable; import java.io.IOException; public class JmcSample { public static void main(String []argv) throws Exception { final String hostPorts = "11.11.11.11:2181/tost"; String serverId = "===server0001==="; CuratorFramework client; client = CuratorFrameworkFactory.newClient(hostPorts, new ExponentialBackoffRetry(1000, 4)); client.start(); client.getZookeeperClient().blockUntilConnectedOrTimedOut(); System.err.println("PERSISTENT > "); client.create().withMode(CreateMode.PERSISTENT).forPath("/"); // 즉 '/tost' 라는 폴더 생성 client.close(); } }
* log
2017-08-17 12:11:32,209 [myid:] - WARN [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@749] - Received packet at server of unknown type 15
2017-08-17 12:11:32,209 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1044] - Closed socket connection for client /10.10.10.10:58956 which had sessionid 0x15dbc19c3140034
2017-08-17 12:11:34,000 [myid:] - INFO [SessionTracker:ZooKeeperServer@358] - Expiring session 0x15dbc19c3140032, timeout of 40000ms exceeded
2017-08-17 12:11:34,001 [myid:] - INFO [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@486] - Processed session termination for sessionid: 0x15dbc19c3140032
2017-08-17 12:11:48,000 [myid:] - INFO [SessionTracker:ZooKeeperServer@358] - Expiring session 0x15dbc19c3140033, timeout of 40000ms exceeded
2017-08-17 12:11:48,001 [myid:] - INFO [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@486] - Processed session termination for sessionid: 0x15dbc19c3140033
해결법
이유는 단순했다. CDH 버전의 주키퍼와 버전을 맞추기위해 3.4.x 버전을 사용했는데, 버전충돌이 문제였다.
저렇게 하고 실행하니 성공한다. 허허허
* pom.xml
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.0</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version>
</dependency>