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>
  • 相关阅读:
    使用Python画ROC曲线以及AUC值
    Machine Learning : Pre-processing features
    资源 | 数十种TensorFlow实现案例汇集:代码+笔记
    在 Mac OS X 终端里使用 Solarized 配色方案
    编译安装GCC 4.7.2
    Office -Word 公式插件Aurora的使用 ——在 Word 中插入 LaTex 公式
    LaTeX 写中文论文而中文显示不出来
    LaTeX 公式编辑之 把符号放在正下方
    Python 判断字符串是否含有指定字符or字符串
    Python 中使用 pandas Dataframe 删除重复的行
  • 原文地址:https://www.cnblogs.com/liangmm/p/12024311.html
Copyright © 2011-2022 走看看