基于zk分布式锁的spring定时任务

环境

1.zk的版本 apache-zookeeper-3.6.1
2.curator的版本2.12.0

实现

引入依赖

    <dependency>
       <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>2.12.0</version>
    </dependency>

zkclient

package com.wisea.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class ZkClientConfig {
    private static final int BASE_SLEEP_TIME_MS = 5000; //定义失败重试间隔时间 单位:毫秒
    private static final int MAX_RETRIES = 3; //定义失败重试次数
    private static final int SESSION_TIME_OUT = 1000000; //定义会话存活时间,根据业务灵活指定 单位:毫秒
    private static final String ZK_URI = "192.168.29.128:2181";//你自己的zkurl和端口号
    private static final String NAMESPACE = "chinoukin_ns";
    //工作空间,可以不指定,建议指定,功能类似于项目包,之后创建的所有的节点都会在该工作空间下,方便管理

    private static CuratorFramework client;

    @PostConstruct
    public void init() {
        client = CuratorFrameworkFactory
                .builder()
                .connectString(ZK_URI)
                .retryPolicy(new ExponentialBackoffRetry(BASE_SLEEP_TIME_MS, MAX_RETRIES))
                .namespace(NAMESPACE)
                .sessionTimeoutMs(SESSION_TIME_OUT)
                .build();
        client.start();
    }

    public static CuratorFramework getZkClient() {
        return client;
    }
}

spring的scheduling定时任务类

package com.wisea.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.util.concurrent.TimeUnit;

/**
 * Zookeeper分布式锁
 */
@Service
public class CronService {

    @Autowired
    ZkClientConfig zkClientConfig;

    //@Scheduled(cron = "0 5,6,7,8,9,10,11,12,13,14 * * * ? ")
    @Scheduled(cron = "0 5/1 * * * ? ")// 从5分开始每隔一分钟执行一次
    private void execute() throws Exception {
        CuratorFramework client = zkClientConfig.getZkClient();

        //client.start();
        String lockPath = "/plus_lock";
        // curator实现好的分布式锁
        InterProcessMutex lock = new InterProcessMutex(client, lockPath);

        if (lock.acquire(3, TimeUnit.SECONDS)) {
            try {
                System.out.println(OffsetDateTime.now() + "定时任务进入");
                Thread.sleep(3000);
            } finally {
                lock.release();
//                if (client.isStarted()) {
//                    client.close();
//                }
            }
        } else {
//            if (client.isStarted()) {
//                client.close();
//            }
        }
    }
}

你可能感兴趣的:(SpringBoot,java基础)