zookeeper之分布式配置中心

import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ZKUtils2 {
    private static ZooKeeper zkCli;

    private static String address = "192.168.79.3:2181/testConf";

    private static DefaultWatch2 watch = new DefaultWatch2();

    private static CountDownLatch init  =  new CountDownLatch(1);

    public static ZooKeeper getZkCli(){

        try {
            zkCli = new ZooKeeper(address,2000,watch);
            watch.setCountDownLatch(init);
            init.await();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return zkCli ;
    }
}
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import java.util.concurrent.CountDownLatch;

public class DefaultWatch2 implements Watcher {
    CountDownLatch countDownLatch ;

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println(watchedEvent.toString());
        switch (watchedEvent.getState()) {
            case Unknown:
                break;
            case Disconnected:
                break;
            case NoSyncConnected:
                break;
            case SyncConnected:
                countDownLatch.countDown();
                System.out.println("connected!!!!!!!!");
                break;
            case AuthFailed:
                break;
            case ConnectedReadOnly:
                break;
            case SaslAuthenticated:
                break;
            case Expired:
                break;
        }
    }
}

import com.zzh.zookeeper.config.MyConf;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;

/**
 * @Description:watch  回调
 */
public class WatchCallBack2 implements Watcher, AsyncCallback.StatCallback, AsyncCallback.DataCallback {
    ZooKeeper zkCli;
    MyConf conf;
    CountDownLatch countDownLatch = new CountDownLatch(1);

    public ZooKeeper getZkCli() {
        return zkCli;
    }

    public void setZkCli(ZooKeeper zkCli) {
        this.zkCli = zkCli;
    }

    public MyConf getConf() {
        return conf;
    }

    public void setConf(MyConf conf) {
        this.conf = conf;
    }

    public void watchConf() {
        //查看文件是否存在
        zkCli.exists("/AppConf", this, this, "exists???");
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //getData回调
    @Override
    public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
        if(data != null ){
            String config = new String(data);
            conf.setConf(config);
            countDownLatch.countDown();
        }
    }

    //exists回调,存在取数据
    @Override
    public void processResult(int rc, String path, Object ctx, Stat stat) {
        if (stat != null){
            zkCli.getData("/AppConf",this,this,"数据存在吗");
        }else {
            System.out.println(path+"配置文件丢失");
        }
       /* System.out.println("path: "+path);
        System.out.println("ctx: "+ctx.toString());
        System.out.println("rc: "+rc);
        System.out.println("stat: "+stat);*/

    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        switch (watchedEvent.getType()) {
            case None:
                break;
            case NodeCreated:
                zkCli.getData("/AppConf",this,this,"文件创建了");
                break;
            case NodeDeleted:
                conf.setConf("");//配置文件丢失后的操作
                countDownLatch = new CountDownLatch(1);
                break;
            case NodeDataChanged:
                zkCli.getData("/AppConf",this,this,"文件修改了");
                break;
            case NodeChildrenChanged:
                break;
        }
    }
}

import com.zzh.zookeeper.config.MyConf;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 测试类
 */
public class TestConfig2 {
    ZooKeeper zkCli;
    @Before
    public void getZK() {
        zkCli = ZKUtils2.getZk();
    }

    @After
    public void closeZK(){
        try {
            zkCli.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getConf2() throws InterruptedException {

        WatchCallBack2 watchCallBack = new WatchCallBack2();
        MyConf myConf = new MyConf();
        watchCallBack.setZkCli(zkCli);
        watchCallBack.setConf(myConf);

        watchCallBack.watchConf();
        if(myConf.getConf()==null||myConf.getConf().equals("")){
            watchCallBack.watchConf();
        }else{
            System.out.println(myConf.getConf());
        }

//===============================================================
        ExecutorService service = Executors.newFixedThreadPool(3);
        for (int i=0;i<3;i++)
        service.submit(()->{
            watchCallBack.watchConf();
            if(myConf.getConf()==null||myConf.getConf().equals("")){
                watchCallBack.watchConf();
            }else{
                System.out.println(Thread.currentThread().getName()+myConf.getConf());
            }
        });
        service.shutdown();
//-------------------------------------------------------------
        new Thread(()->{
            watchCallBack.watchConf();
            if(myConf.getConf()==null||myConf.getConf().equals("")){
                watchCallBack.watchConf();
            }else{
                System.out.println(Thread.currentThread().getName()+myConf.getConf());
            }
        },"t1").start();
        new Thread(()->{
            watchCallBack.watchConf();
            if(myConf.getConf()==null||myConf.getConf().equals("")){
                watchCallBack.watchConf();
            }else{
                System.out.println(Thread.currentThread().getName()+myConf.getConf());
            }
        },"t2").start();
        new Thread(()->{
            watchCallBack.watchConf();
            if(myConf.getConf()==null||myConf.getConf().equals("")){
                watchCallBack.watchConf();
            }else{
                System.out.println(Thread.currentThread().getName()+myConf.getConf());
            }
        },"t3").start();

        Thread.sleep(10000);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值