1.注册
package live.yanxiaohui.server;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* @Description 服务注册
* @Author: yanxh<br>
* @Date 2020-05-06 16:15<br>
* @Version 1.0<br>
*/
@Component
public class OrderServer implements ApplicationRunner {
@Value("${name}")
private String name;
@Value("${zk.url}")
private String url;
@Value("${server.port}")
private String port;
// 超时时间
private int TIME_OUT = 50000;
/**
* 启动springboot时做的事情
*
* @param args
* @throws Exception
*/
@Override
public void run(ApplicationArguments args) throws Exception {
String path = "/my_servers";
ZooKeeper zooKeeper = new ZooKeeper(url, TIME_OUT, a -> System.out.println(a.getState().name()));
Stat exists = zooKeeper.exists(path, a -> System.out.println(a.getState().name()));
String data = "http://127.0.0.1:" + port + "/" + name;
if (exists == null) {
// 不存在父节点
zooKeeper.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.create(path + "/" + port, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
} else {
exists = zooKeeper.exists(path + "/" + port, a -> System.out.println(a.getState().name()));
if (exists == null) {
zooKeeper.create(path + "/" + port, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}else{
System.out.println("节点已存在");
}
}
}
}
2.发现
package live.yanxiaohui.server;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Value;
import java.util.concurrent.CountDownLatch;
/**
* @Description 服务发现
* @Author: yanxh<br>
* @Date 2020-05-06 17:26<br>
* @Version 1.0<br>
*/
public class FindServer {
private static final String url = "127.0.0.1:2181";
private static final CountDownLatch countDownLatch = new CountDownLatch(1);
// 超时时间
private static final int TIME_OUT = 50000;
public static void main(String[] args) throws Exception {
String path = "/my_servers/";
ZooKeeper zooKeeper = new ZooKeeper(url, TIME_OUT, a -> {
Watcher.Event.KeeperState state = a.getState();
// 如果当前连接成功,则开始放心
if (state == Watcher.Event.KeeperState.SyncConnected) {
System.out.println("zk连接成功~~");
countDownLatch.countDown();
}
});
countDownLatch.await();
for (String children : zooKeeper.getChildren(path, null, new Stat())) {
String childPath = path + children;
byte[] datas = zooKeeper.getData(childPath, null, new Stat());
System.out.println("服务名称:" + new String(datas));
}
}
}
3.启动
配置文件application.yml
server:
port: 8080
name: order_service
zk:
url: 127.0.0.1:2181
启动后,变更配置再启动
server:
port: 8081
name: prod_service
zk:
url: 127.0.0.1:2181
启动后,变更配置再启动
server:
port: 8082
name: user_service
zk:
url: 127.0.0.1:2181
通过客户端插件可以看出有三个服务已注册至zookeeper中
此客户端连接工具可在我上传的资源中仅进行下载
启动服务发现的程序,可以遍历ZK中所有的服务