zookeeper実現master-slave選挙(ZkClient実現)

15763 ワード

すべてのクライアントは1つのノードにmasterノードを登録し、登録に成功した場合のみmasterになります.
package master_slave;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkNodeExistsException;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MasterSelector {
     

    private ZkClient zkClient;

    private final static String MASTER_PATH = "/master";

    private IZkDataListener dataListener;//         
    private UserCenter server;//    
    private UserCenter master;//master   

    private static boolean isRunning = false;

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);


    public MasterSelector(UserCenter server,ZkClient zkClient) {
     
        System.out.println(server.toString());
        this.server = server;
        this.zkClient = zkClient;
        this.dataListener = new IZkDataListener() {
     
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
     

            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
     
                //       ,      
                chooseMaster();

            }
        };
    }

    public void start(){
     
        //    
        if(!isRunning){
     
            isRunning=true;
            zkClient.subscribeDataChanges(MASTER_PATH,dataListener);
            chooseMaster();
        }
    }

    public void stop(){
     
        //    
        if(isRunning){
     
            scheduledExecutorService.shutdown();
            zkClient.unsubscribeDataChanges(MASTER_PATH,dataListener);
            releaseMaster();
        }
    }

    //        
    private void chooseMaster(){
     
        if (!isRunning) {
     
            System.out.println("        。");
            return;
        }
        try {
     
            zkClient.createEphemeral(MASTER_PATH,server);
            master = server;
            System.out.println(master.getMc_name());

            //  master    
            scheduledExecutorService.schedule(()->{
     
                releaseMaster();
            }, 5,TimeUnit.SECONDS);


        }catch (ZkNodeExistsException e){
     
            //  master    
            UserCenter userCenter = zkClient.readData(MASTER_PATH);
            if(userCenter==null) {
     
                chooseMaster();//    master
            }else{
     
                master=userCenter;
            }
        }
    }

    private void releaseMaster(){
     
        //   
        //       master
        if(checkIsMaster()){
     
            zkClient.delete(MASTER_PATH);
        }
    }

    private boolean checkIsMaster(){
     
        //     master
        UserCenter userCenter = zkClient.readData(MASTER_PATH);
        if(userCenter.getMc_name().equals(server.getMc_name())){
     
            master=server;
            return true;
        }
        return false;
    }


}