当前位置: 首页 > news >正文

16 redis高可用读写分离方案

在前面说的JedisSentinelPool只能实现主从的切换,而无法实现读写的分离

1.哨兵的客户端实现主从切换方案
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/redis.clients/jedis --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.6.3</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
# Redis哨兵服务器地址
redis.sentinel=aliyun:26885,aliyun:26886,aliyun:26887
redis.master=mymaster
# Redis服务器连接密码(默认为空)
redis.password=null
redis.timeout=30000
# 连接池最大连接数(使用负值表示没有限制)
redis.maxTotal=30
# 连接池中的最大空闲连接
redis.maxIdle=10
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.minEvictableIdleTimeMillis=1800000
redis.softMinEvictableIdleTimeMillis=10000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
redis.maxWaitMillis=1500
redis.testOnBorrow=true
redis.testWhileIdle=true
redis.blockWhenExhausted=false
redis.JmxEnabled=true
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;import java.util.HashSet;
import java.util.Set;@Configuration
@PropertySource("classpath:application.properties")
public class RedisSentinelConfig {@Value("${redis.sentinel}")private String hosts;@Value("${redis.master}")private String master;@Value("${redis.timeout}")private int timeout;@Value("${redis.maxIdle}")private int maxIdle;@Value("${redis.maxWaitMillis}")private int maxWaitMillis;@Value("${redis.blockWhenExhausted}")private Boolean blockWhenExhausted;@Value("${redis.JmxEnabled}")private Boolean JmxEnabled;@Beanpublic JedisPoolConfig  jedisPoolConfigFactory() {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxIdle(maxIdle);jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);// 连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认truejedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);// 是否启用pool的jmx管理功能, 默认truejedisPoolConfig.setJmxEnabled(JmxEnabled);jedisPoolConfig.setTestOnBorrow(true);jedisPoolConfig.setTestOnReturn(true);return jedisPoolConfig;}@Beanpublic JedisSentinelPool JedisSentinelPoolFactory(JedisPoolConfig jedisPoolConfig){Set<String> nodeSet = new HashSet<>();//获取到节点信息String nodeString = hosts;//判断字符串是否为空if(nodeString == null || "".equals(nodeString)){throw new RuntimeException("RedisSentinelConfiguration initialize error nodeString is null");}String[] nodeArray = nodeString.split(",");//判断是否为空if(nodeArray == null || nodeArray.length == 0){throw new RuntimeException("RedisSentinelConfiguration initialize error nodeArray is null");}//循环注入至Set中for(String node : nodeArray){System.out.println("Read node : "+node);nodeSet.add(node);}//创建连接池对象JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(master,nodeSet,jedisPoolConfig ,timeout);return jedisSentinelPool;}}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisSentinelPool;/*** 操作字符串类型*/
@Component
public class RedisString {public final static String RS_STR_NS = "rs:";@Autowiredprivate JedisSentinelPool jedisSentinelPool;/*** 向Redis中存值,永久有效*/public String set(String key, String value) {Jedis jedis = null;try {jedis = jedisSentinelPool.getResource();return jedis.set(RS_STR_NS +key, value);} catch (Exception e) {throw new RuntimeException("向Redis中存值失败!");} finally {jedis.close();}}/*** 批量向Redis中存值,永久有效*/public String msetRaw(String... keysvalues) {Jedis jedis = null;try {jedis = jedisSentinelPool.getResource();return jedis.mset(keysvalues);} catch (Exception e) {throw new RuntimeException("批量向Redis中存值失败!");} finally {jedis.close();}}/*** 根据传入Key获取指定Value*/public String get(String key) {Jedis jedis = null;try {jedis = jedisSentinelPool.getResource();return jedis.get(RS_STR_NS +key);} catch (Exception e) {throw new RuntimeException("获取Redis值失败!");} finally {jedis.close();}}}
2、读写分离的实现

需要自己去封装代码实现。
待补充

http://www.lryc.cn/news/241858.html

相关文章:

  • Nginx模块开发之http handler实现流量统计(2)
  • 案例012:Java+SSM+uniapp基于微信小程序的科创微应用平台设计与实现
  • vue3+elementPlus登录向后端服务器发起数据请求Ajax
  • 存储区域
  • C#串口通信从入门到精通(27)——高速通信下解决数据处理慢的问题(20ms以内)
  • Redis-Redis高可用集群之水平扩展
  • 2023全球数字贸易创新大赛-人工智能元宇宙-4-10
  • go defer用法_类似与python_java_finially
  • Log4j2.xml不生效:WARN StatusLogger Multiple logging implementations found:
  • 【LeetCode】挑战100天 Day14(热题+面试经典150题)
  • VMware安装windows操作系统
  • 历时半年,我发布了一款习惯打卡小程序
  • 被DDOS了怎么办 要如何应对
  • 时间序列预测实战(十七)PyTorch实现LSTM-GRU模型长期预测并可视化结果(附代码+数据集+详细讲解)
  • 【免费使用】基于PaddleSeg开源项目开发的人像抠图Web API接口
  • Centos7 Python环境和yum修复
  • Ubuntu下使用protoBuf
  • AT89S52单片机
  • 数字孪生智慧校园 Web 3D 可视化监测
  • Python Web框架的三强之争:Flask、Django和FastAPI
  • 本地缓存与分布式缓存
  • LabVIEW如何获取波形图上游标所在位置的数值
  • 八股文面试day6
  • 【Unity】EventSystem.current.IsPointerOverGameObject()对碰撞体起作用
  • 形态学操作—闭运算
  • HEVC-SCC rgb file input
  • XG916Ⅱ轮式装载机后驱动桥设计机械设计CAD
  • pcr扩增原理中的变性 退火 延申扩增
  • C语言——输入一个4位正整数,输出其逆数。
  • jQuery_02 引入jQuery,初试牛刀