ZooKeeper在服务注册与订阅(服务发现)方面,通常会使用有序临时节点来实现。当一个服务实例启动时,它会在ZooKeeper中创建一个临时节点来表示自己的存在。其他服务可以通过监听这些节点的变化来实现实时的服务发现。下面是一个简单的Java代码示例,展示了如何使用ZooKeeper实现服务注册与订阅的基本逻辑。
首先,确保你的项目中引入了ZooKeeper客户端库依赖。常用的ZooKeeper客户端库包括zookeeper
和curator
。
示例代码基于curator
库来实现服务注册与订阅:
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;
public class ServiceRegistry {
private final CuratorFramework client;
private final String basePath;
public ServiceRegistry(String connectString, String basePath) {
this.client = CuratorFrameworkFactory.newClient(connectString, new ExponentialBackoffRetry(1000, 3));
this.basePath = basePath;
client.start();
}
public void registerService(String serviceName, String serviceAddress) throws Exception {
String servicePath = basePath + "/" + serviceName;
PersistentEphemeralNode node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, servicePath, serviceAddress.getBytes());
node.start();
node.waitForInitialCreate();
}
public void close() {
CloseableUtils.closeQuietly(client);
}
}
在上述代码中,ServiceRegistry
类用于服务注册,它会创建一个临时节点来表示服务实例。当服务实例启动时,可以调用registerService
方法来注册自己的信息。
下面是一个简单的服务订阅示例:
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
public class ServiceSubscriber {
private final CuratorFramework client;
private final String basePath;
public ServiceSubscriber(String connectString, String basePath) {
this.client = CuratorFrameworkFactory.newClient(connectString, new ExponentialBackoffRetry(1000, 3));
this.basePath = basePath;
client.start();
}
public void subscribe(String serviceName) throws Exception {
String servicePath = basePath + "/" + serviceName;
PathChildrenCache cache = new PathChildrenCache(client, servicePath, true);
PathChildrenCacheListener listener = new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception {
if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
ChildData data = event.getData();
String serviceAddress = new String(data.getData());
System.out.println("New service added: " + serviceAddress);
}
}
};
cache.getListenable().addListener(listener);
cache.start();
}
public void close() {
CloseableUtils.closeQuietly(client);
}
}
在上述代码中,ServiceSubscriber
类用于订阅服务信息变化。它会监听指定服务的节点变化,并在有新服务实例加入时输出服务地址。
请注意,这只是一个基本的示例代码,实际项目中还需要考虑更多的异常处理、线程安全等问题。同时,还可以使用更高级的ZooKeeper客户端库,如Apache Curator,来简化操作和提供更多的特性。