码迷,mamicode.com
首页 > 其他好文 > 详细

zk保证定时任务集群部署时单个节点执行

时间:2019-12-11 19:29:08      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:his   exception   log   framework   for   tor   %s   not   ram   

@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>

zk保证定时任务集群部署时单个节点执行

标签:his   exception   log   framework   for   tor   %s   not   ram   

原文地址:https://www.cnblogs.com/liangmm/p/12024311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!