分布式集群定时多台机器执行定时器问题

package www.test.com.common.task;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

import org.redisson.Redisson;
import org.redisson.api.RLock;

@Slf4j
@Component
public class ExportJobTask {
    @Autowired
    private Redisson redisson;

	//单位毫秒 每分钟执行一次
    @Scheduled(fixedDelayString = "60000")
    public void test() {
        log.info("---------------开始定时任务-----------------");
        String locKey = "test:key";
        RLock redissonLock =  redisson.getLock(locKey);
        redissonLock.lock();
        try {
            try{
                //TODO 业务逻辑
            }catch (Exception e){
                //异常处理
                e.printStackTrace();
            }
        } finally {
            redissonLock.unlock();
            log.info("---------------结束定时任务-----------------");
        }
    }
}

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.11.3</version>
        </dependency>
        <dependency>
		    <groupId>org.projectlombok</groupId>
		    <artifactId>lombok</artifactId>
		    <version>1.18.4</version>
		</dependency>

你可能感兴趣的:(spring,boot,spring,java)