通过redis选主进程

2024-02-27 07:20
文章标签 redis 进程 选主

本文主要是介绍通过redis选主进程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

选主进程的方式有很多,完善点的方案就是zookeeper来做。这里介绍的是一种基于redis选主的方案,快速开发,快速实现。

Springside4里有个MasterElector类,细读代码后整理出自己的总结如下。

1.      业务进程启动时生成hostId,hostId的规则是“主机名-随机数”,一台主机部署多个进程实例的情况,也可以改为“主机名-端口”。

2.      业务进程调用jedis.get(masterKey)获取masterFromRedis。

3.      如果masterFromRedis为空,调用setnx(masterKey, hostId)方法设值,方法返回值大于0当前进程选为主进程,并且expire(masterKey,expireSeconds)设置key的有效期。Setnx返回值小于等于0时不能选为主进程。

如果masterFromRedis等于hostId,当前进程选为主进程,并且expire(masterKey, expireSeconds)设置key的有效期。

4.      按时间间隔intervalSeconds定期执行第二步和第三步。

 

 

当get(masterKey)之后获取到的masterFromRdis为空,在setnx之前,有其他进程已经Set了masterKey的值,本进程再调用setnx返回0失败.reids api文档有详细介绍:

 

SETNX key value

将 key 的值设为 value ,当且仅当 key 不存在。

若给定的 key 已经存在,则 SETNX 不做任何动作。

SETNX 是『SET if Not eXists』(如果不存在,则 SET)的简写。

可用版本:

>= 1.0.0

时间复杂度:

O(1)

返回值:

设置成功,返回 1 。

设置失败,返回 0 。

 

intervalSeconds定时时间间隔设置短些,可以减少异常情况下没有主的情况。

附上springside4的代码

 

 

public class MasterElector implementsRunnable {

 

                   publicstatic final String DEFAULT_MASTER_KEY = "master";

 

                   privatestatic Logger logger = LoggerFactory.getLogger(MasterElector.class);

 

                   privatestatic AtomicInteger poolNumber = new AtomicInteger(1);

                   privateScheduledExecutorService internalScheduledThreadPool;

                   privateScheduledFuture electorJob;

                   privateint intervalSeconds;

 

                   privateJedisTemplate jedisTemplate;

 

                   privateint expireSeconds;

                   privateString hostId;

                   privateAtomicBoolean master = new AtomicBoolean(false);

                   privateString masterKey = DEFAULT_MASTER_KEY;

 

                   publicMasterElector(JedisPool jedisPool, int intervalSeconds, int expireSeconds) {

                                      jedisTemplate= new JedisTemplate(jedisPool);

                                      this.expireSeconds= expireSeconds;

                                      this.intervalSeconds= intervalSeconds;

                   }

 

                   /**

                    * 发挥目前该实例是否master

                    */

                   publicboolean isMaster() {

                                      returnmaster.get();

                   }

 

                   /**

                    * 启动抢注线程, 自行创建scheduler线程池.

                    */

                   publicvoid start() {

                                      internalScheduledThreadPool= Executors.newScheduledThreadPool(1,

                                                                            Threads.buildJobFactory("Master-Elector-"+ poolNumber.getAndIncrement() + "-%d"));

                                      start(internalScheduledThreadPool);

                   }

 

                   /**

                    * 启动抢注线程, 使用传入的scheduler线程池.

                    */

                   publicvoid start(ScheduledExecutorService scheduledThreadPool) {

                                      if(intervalSeconds >= expireSeconds) {

                                                         thrownew IllegalArgumentException("periodSeconds must less than expireSeconds.periodSeconds is "

                                                                                               +intervalSeconds + " expireSeconds is " + expireSeconds);

                                      }

 

                                      hostId= generateHostId();

                                      electorJob= scheduledThreadPool.scheduleAtFixedRate(new WrapExceptionRunnable(this), 0,intervalSeconds,

                                                                            TimeUnit.SECONDS);

                                      logger.info("masterElectorstart, hostName:{}.", hostId);

                   }

 

                   /**

                    * 停止分发任务,如果是自行创建的threadPool则自行销毁。

                    */

                   publicvoid stop() {

                                      electorJob.cancel(false);

 

                                      if(internalScheduledThreadPool != null) {

                                                         Threads.normalShutdown(internalScheduledThreadPool,5, TimeUnit.SECONDS);

                                      }

                   }

 

                   /**

                    * 生成host id的方法哦,可在子类重载.

                    */

                   protectedString generateHostId() {

                                      Stringhost = "localhost";

                                      try{

                                                         host= InetAddress.getLocalHost().getHostName();

                                      }catch (UnknownHostException e) {

                                                         logger.warn("cannot get hostName", e);

                                      }

                                      host= host + "-" + new SecureRandom().nextInt(10000);

 

                                      returnhost;

                   }

 

                   @Override

                   publicvoid run() {

                                      jedisTemplate.execute(newJedisActionNoResult() {// NOSONAR

                                                                                               @Override

                                                                                               publicvoid action(Jedis jedis) {

                                                                                                                  StringmasterFromRedis = jedis.get(masterKey);

 

                                                                                                                  logger.debug("masteris {}", masterFromRedis);

 

                                                                                                                  //if master is null, the cluster just start or the master had crashed, try toregister myself

                                                                                                                  //as master

                                                                                                                  if(masterFromRedis == null) {

                                                                                                                                     //use setnx to make sure only one client can register as master.

                                                                                                                                     if(jedis.setnx(masterKey, hostId) > 0) {

                                                                                                                                                        jedis.expire(masterKey,expireSeconds);

                                                                                                                                                        master.set(true);

 

                                                                                                                                                        logger.info("masteris changed to {}.", hostId);

                                                                                                                                                        return;

                                                                                                                                     }else {

                                                                                                                                                        master.set(false);

                                                                                                                                                        return;

                                                                                                                                     }

                                                                                                                  }

 

                                                                                                                  //if master is myself, update the expire time.

                                                                                                                  if(hostId.equals(masterFromRedis)) {

                                                                                                                                     jedis.expire(masterKey,expireSeconds);

                                                                                                                                     master.set(true);

                                                                                                                                     return;

                                                                                                                  }

 

                                                                                                                  master.set(false);

                                                                                               }

                                                                            });

                   }

 

                   /**

                    * 如果应用中有多种master,设置唯一的mastername

                    */

                   publicvoid setMasterKey(String masterKey) {

                                      this.masterKey= masterKey;

                   }

 

                   //for test

                   publicvoid setHostId(String hostId) {

                                      this.hostId= hostId;

                   }

}

 

这篇关于通过redis选主进程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/751604

相关文章

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Redis延迟队列的实现示例

《Redis延迟队列的实现示例》Redis延迟队列是一种使用Redis实现的消息队列,本文主要介绍了Redis延迟队列的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、什么是 Redis 延迟队列二、实现原理三、Java 代码示例四、注意事项五、使用 Redi

Redis缓存问题与缓存更新机制详解

《Redis缓存问题与缓存更新机制详解》本文主要介绍了缓存问题及其解决方案,包括缓存穿透、缓存击穿、缓存雪崩等问题的成因以及相应的预防和解决方法,同时,还详细探讨了缓存更新机制,包括不同情况下的缓存更... 目录一、缓存问题1.1 缓存穿透1.1.1 问题来源1.1.2 解决方案1.2 缓存击穿1.2.1

redis-cli命令行工具的使用小结

《redis-cli命令行工具的使用小结》redis-cli是Redis的命令行客户端,支持多种参数用于连接、操作和管理Redis数据库,本文给大家介绍redis-cli命令行工具的使用小结,感兴趣的... 目录基本连接参数基本连接方式连接远程服务器带密码连接操作与格式参数-r参数重复执行命令-i参数指定命

深入理解Redis大key的危害及解决方案

《深入理解Redis大key的危害及解决方案》本文主要介绍了深入理解Redis大key的危害及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、背景二、什么是大key三、大key评价标准四、大key 产生的原因与场景五、大key影响与危

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

Redis过期键删除策略解读

《Redis过期键删除策略解读》Redis通过惰性删除策略和定期删除策略来管理过期键,惰性删除策略在键被访问时检查是否过期并删除,节省CPU开销但可能导致过期键滞留,定期删除策略定期扫描并删除过期键,... 目录1.Redis使用两种不同的策略来删除过期键,分别是惰性删除策略和定期删除策略1.1惰性删除策略

python多进程实现数据共享的示例代码

《python多进程实现数据共享的示例代码》本文介绍了Python中多进程实现数据共享的方法,包括使用multiprocessing模块和manager模块这两种方法,具有一定的参考价值,感兴趣的可以... 目录背景进程、进程创建进程间通信 进程间共享数据共享list实践背景 安卓ui自动化框架,使用的是

Linux(Centos7)安装Mysql/Redis/MinIO方式

《Linux(Centos7)安装Mysql/Redis/MinIO方式》文章总结:介绍了如何安装MySQL和Redis,以及如何配置它们为开机自启,还详细讲解了如何安装MinIO,包括配置Syste... 目录安装mysql安装Redis安装MinIO总结安装Mysql安装Redis搜索Red

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re