zoukankan      html  css  js  c++  java
  • zk保证定时任务集群部署时单个节点执行

    @Component
    public class ZKLeaderLatch {
    
        private static CuratorFramework zkClient;
        private static LeaderLatch leaderLatch;
    
    
        public ZKLeaderLatch(@Value("${zkservers}")String servers, @Value("${masterkey}")String masterkey) {
            String connectString = servers;
            String masterKey = masterkey;
            try {
                final String zkid = String.format("zkLatchClient#%s", InetAddress.getLocalHost().getHostAddress());
                CommonLogger.printInfo(this,"zk "+zkid+" client init... server:"+connectString+", masterKey:"+masterKey+"");
                RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                zkClient = CuratorFrameworkFactory.builder().connectString(connectString)
                        .sessionTimeoutMs(6000).retryPolicy(retryPolicy).build();
                CommonLogger.printInfo(this,"zk client start....");
                zkClient.start();
                leaderLatch = new LeaderLatch(zkClient, masterKey,zkid);
                LeaderLatchListener leaderLatchListener = new LeaderLatchListener() {
                    @Override
                    public void notLeader() {
                        CommonLogger.printInfo(this,"client "+zkid+" is not main. ");
                    }
                    @Override
                    public void isLeader() {
                        CommonLogger.printInfo(this,"client "+zkid+" is main. YEAH!");
                    }
                };
                leaderLatch.addListener(leaderLatchListener);
                CommonLogger.printInfo(this,"leaderLatch start....");
    
                leaderLatch.start();
            } catch(Exception e) {
                CommonLogger.printInfo(this,"client err. "+e.getMessage(),e);
            }
        }
    
    
        public boolean isLeader() {
            return leaderLatch.hasLeadership();
        }
    
        public CuratorFramework getClient(){
            return zkClient;
        }
    
        public LeaderLatch getLatch(){
            return leaderLatch;
        }
    }
    配置依赖
    <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.1.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-client</artifactId>
                <version>4.0.1</version>
            </dependency>
    
            <!-- zookeeper -->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.9</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
  • 相关阅读:
    为了抓包某APP所做的尝试(to be continued)
    VirtualBox的使用的一些Tips 网络配置|硬盘扩充
    斜线和反斜线简要历史,为什么windows和unix采用不同的路径分隔符
    求出二维数组主对角线、次对角线以及周边元素之和
    C#计算两个时间的时间差,精确到年月日时分秒
    C#获取MP3,WMA信息
    C#窗体随意移动
    DEV GridControl小结
    DEV 皮肤的使用
    C#窗体阴影
  • 原文地址:https://www.cnblogs.com/liangmm/p/12024311.html
Copyright © 2011-2022 走看看