Zookeeper作为注册中心的实现

  • 引入依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
  • 注册监听器,程序执行时,完成ip+port的数据保存
@SpringBootApplication
public class ProductserviceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductserviceApplication.class, args);
    }

    //注册监听对象
    @Bean
    public ServletListenerRegistrationBean servletRegistrationBean(){
        ServletListenerRegistrationBean registrationBean=new ServletListenerRegistrationBean();
        registrationBean.setListener(new InitListener());
        return registrationBean;
    }

}
  • 监听实现
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Properties;

/*
任涛 -----专用
*/
public class InitListener implements ServletContextListener {

    //容器初始化时调用
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            Properties properties = new Properties();
            properties.load(InitListener.class.getClassLoader().getResourceAsStream("application.properties"));
            //获取服务所在ip
            String ip= InetAddress.getLocalHost().getHostAddress();
            System.out.println("获取服务所在ip--->"+ip);
            //获取服务所在port
            String port=properties.getProperty("server.port");
            System.out.println("获取服务所在port--->"+port);
            ZkUtil.saveInfo(ip,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
  • 数据存储
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/*
任涛 -----专用
*/
public class ZkUtil {

    //定义数据保存路径
    private static final String SAVEPATH="/product";

    //发令枪:阻塞作用
    private static CountDownLatch countDownLatch=new CountDownLatch(1);

    public static void saveInfo(String ip, String port) {
        try {
            //创建连接
            ZooKeeper zooKeeper = new ZooKeeper("192.168.58.128:2181", 5000,new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    //监控连接状态:连接完成时,停止阻塞
                    if(watchedEvent.getState()==Event.KeeperState.SyncConnected){
                        countDownLatch.countDown();
                    }
                }
            });
            //判断目录是否存在
            Stat exists = zooKeeper.exists(SAVEPATH, false);
            if (null==exists){
                //目录为空,创建持久节点
                System.out.println("创建目录");
                zooKeeper.create(SAVEPATH,"1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            //加入数据
            String data=ip+":"+port;
            //创建临时顺序节点  1:客户端断开,数据自动删除 2:顺序节点,区分不同客户端
            String s = zooKeeper.create(SAVEPATH + "/data", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println("s---->"+s);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

备注:程序停止时,客户端消失,临时节点消失

猜你喜欢

转载自blog.csdn.net/weixin_44971379/article/details/110355113

相关文章