在springboot中全部的整合都是以bean的形式注入对象,从数据库coon、redis conn、再到整合的zookeeper,依然是依照bean注入链接对象,经过zookeeper api对zookeeper中node 数据进行增删改查等操做,从而实现配置同步。这篇文章只是初步使用web服务,在服务启动时注册服务,并将配置文件内容写入zookeeper,经过api接口获取配置内容。至于多节点配置文件同步一致性,则是之后须要深刻研究的主题。java
在zookeeper_caesar建立两个模块,core和client,core中存放zookeeper链接和相关操做方法,client是正常的web服务。在springboot分层思想中core至关于DAO层,进行zookeeper操做,在client的service层和controller层进行调用和处理。node
其中client依赖core模块,在其pom.xml中添加core模块信息,其后在client中添加spring模块spring-boot-starter-web(spring对servlet封装的模块和嵌入式tomcat)python
<dependency> <groupId>com.soft.caesar</groupId> <artifactId>core</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
在RegistryConfig.java中建立须要的bean serviceRegistry 即zookeeper链接对象 zk = new ZooKeeper(zkServers,SESSION_TIMEOUT,this)git
在TestController.java中调用serviceRestry即core中定义操做getValue,获取zookeeper中数据github
在WebListener.java中监听web服务启动,启动时将服务存入zookeeperweb
ClientApplication.py 启动服务主函数redis
@Component public class ServiceRegistryImpl implements ServiceRegistry,Watcher { private static CountDownLatch latch = new CountDownLatch(1); # 多线程时,等待,直到一个线程时,在latch被唤醒 private ZooKeeper zk; private static final int SESSION_TIMEOUT=5000; private static final String REGISTRY_PATH = "/registry"; public ServiceRegistryImpl() { } public ServiceRegistryImpl(String zkServers) { try { zk = new ZooKeeper(zkServers,SESSION_TIMEOUT,this); latch.await();# latch等待唤醒 } catch (Exception e) { e.printStackTrace(); } } @Override public void register(String serviceName, String serviceAddress) { try { String registryPath = REGISTRY_PATH; if (zk.exists(registryPath, false) == null) { zk.create(registryPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); #持久化建立/registry node } //建立服务节点(持久节点) String servicePath = registryPath + "/" + serviceName; if (zk.exists(servicePath, false) == null) { zk.create(servicePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } //建立地址节点 String addressPath = servicePath + "/address-"; # 此处节点是瞬态的节点,当服务断开zookeeper链接时,节点消失,从新链接时,address- 以序列添加末尾序列值。
这种序列方法能够判断注册服务的主被,先注册的数字小,后注册的数字大,每次从主上同步数据到被。在服务异常时,节点自动消失,能够探测服务状态 String addressNode = zk.create(addressPath, serviceAddress.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); } catch (Exception e){ e.printStackTrace(); } } @Override public void process(WatchedEvent watchedEvent) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) latch.countDown(); }
启动zookeeper,启动以上web服务spring
java客户端登陆zookeeper,查询注册服务的注册信息数据库
调用接口查询zookeeper数据api
以上是关于zookeeper的初步探索,能够参考https://github.com/CaesarLinsa/zookeeper_caesar,在version1.0分支中去掉core模块,添加到service层中,添加对zookeeper操做接口,实如今接口修改zookeeper同时,配置文件发生变动。固然如此须要每一个服务的守护进程中存在相似socket通讯,在server端发生变化时,在watch中向守护进程中发送相关命令,促使配置变动,服务启动或者不启动加载配置。