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>
  • 相关阅读:
    系统振动的稳定性分析
    算法
    九眼智能:信息安全是网络发展的关键
    运用大数据技术筑起网络安全防火墙
    网络安全维护九眼智能大数据显身手
    九眼智能大数据技术助力网络信息安全
    九眼智能:用大数据技术为网络信息加层“滤网”
    大数据如何解决人工智能对文本挖掘的挑战
    “键盘侠”行为规则出台网络信息盼清洁
    灵玖NLPIRParser大数据挖掘系统智能摘要
  • 原文地址:https://www.cnblogs.com/liangmm/p/12024311.html
Copyright © 2011-2022 走看看