分布式环境下定时任务单点运行

场景

提供接口服务的API负载均衡在了7台机器上,要保证定时任务只在一台机器上跑,因为有些定时任务不能同时进行,并且多台机器同时执行定时任务也浪费资源,下面来讲讲如何实现定时任务单点运行。


方案一:根据机器的IP来限制 


因为部署服务的7台机器ip是已知的,那么就可以通过ip来限制哪台机器上的应用可以跑定时任务,获取本服务器或者客户端ip方法,请移步至: Java获取客户端、本机IP 

@Component
@Slf4j
public class RegularTask {
    @Lazy
    @Scheduled(cron = "")
    public void send() {
        //获取本台服务器ip
        String ip = IPUtil.getLocalIP(); 
        //允许执行定时任务的ip强烈建议配置在数据库中,做成可配置化,不要写死
        String allowIp = PropertiesUtil.get("RUN_TASK_IP");
        //ip不匹配直接return
        if (allowIp.indexOf(ip) == -1) { 
            log.info("……");
            return;
        }
        //TODO deal with task
    }
}

方案二:分布式锁实现

让7台机器上的应用各自去争取同一把锁,谁抢到了锁就让谁执行,实现方式可以使用zookeeper或者redis,基于redis实现分布式锁:https://blog.csdn.net/fanrenxiang/article/details/79803037基于zookeeper实现分布式锁:https://blog.csdn.net/fanrenxiang/article/details/81704691。比如redis实现分布式锁的部分代码:

@Component
public class RegularTask {

    @Autowired
    private JedisPool jedisPool;

    @Lazy
    @Scheduled(cron = "")
    public void send() {
        Jedis jedis =null;
        try {
            jedis = jedisPool.getResource();
            boolean isLock = this.tryLock(jedis, "lock_ip_key", RandomStringUtils.randomNumeric(16), 1800); //过期时间为30min
            if (isLock) {
                //TODO  do task
            }
        }catch (Exception e){
            log.error("");
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }

    private static final String LOCKED_SUCCESS = "OK";
    private static final String NX = "NX";
    private static final String EXPIRE_TIME = "EX";

    public static boolean tryLock(Jedis jedis, String lockKey, String uniqueId, long expireTime) {
        String result = jedis.set(lockKey, uniqueId, NX, EXPIRE_TIME, expireTime);
        return LOCKED_SUCCESS.equals(result);
    }
}

方案三:基于zookeeper的master选举

利用zookeeper来实现Master选举,只有Master机器(leader)上能执行定时任务。分布式机器同时在zookeeper中的同一节点下创建节点,zookeeper保证了只有一个能创建成功(临时节点),Curator里面就封装了这些操作。选举分为Leader Latch和Leader Election两种选举方案,这里使用Leader Latch实现。更多的关于zookeeper客户端curator,请移步这里

<dependency>
   <groupId>org.apache.curator</groupId>
   <artifactId>curator-recipes</artifactId>
   <version>2.4.1</version>
</dependency>
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RegularTask {
    private static final Logger log = LoggerFactory.getLogger(RegularTask.class);
    //zookeeper集群,实例数大于等于3
    private static final String ZOOKEEPER_STR = "10.25.142.55:2181,10.48.24.36:2181,10.48.24.36:2182,10.48.124.36:2181,10.48.125.36:2181";
    private static CuratorFramework curatorFramework;
    private static LeaderLatch leaderLatch;


    static {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        curatorFramework = CuratorFrameworkFactory.newClient(ZOOKEEPER_STR, retryPolicy);
        curatorFramework.start();
        leaderLatch = new LeaderLatch(curatorFramework, "/regulartask");
        try {
            leaderLatch.start();
        } catch (Exception e) {
            log.error("LeaderLatch start error:{}", e);
        }
    }
    
    @Lazy
    @Scheduled(cron = "××")
    public void send() {
        try {
            //判断是否为master
            if (!leaderLatch.hasLeadership()) {
                log.info("current mechine is not a leader");
                return;
            }
            //TODO 定时任务逻辑
        } catch (Exception e) {
            log.error("regulartask run error:{}", e);
        } finally {
            try {
                if (leaderLatch != null) {
                    leaderLatch.close();
                }
            } catch (IOException e) {
                log.error("leaderLatch close error:{}", e);
                e.printStackTrace();
            }
        }
    }
}

我们项目中使用的就是zookeeper的master选举实现的,可靠,稳定,线上还没出现过问题!


books 使用quartz实现分布式定时任务(包含管理界面):https://blog.csdn.net/fanrenxiang/article/details/85539918

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值