springboot 集成Redis一主二从三哨兵
1、Centos7 Redis一主二从三哨兵配置
2、接入过程
与集成redis单机不同的是jedis相关的配置做了修改,JedisPool换成了JedisSentinelPool,相关改动如下:
application文件:
# Redis
spring.redis:
sentinel:
#与Redis环境配置的保持一致
master: mymaster
#从节点集合
nodes: localhost:26388,localhost:26380,localhost:26381
password: root
timeout: 1000
jedis.pool:
#jedis最大分配对象
maxTotal: 1024
#jedis最大保存idel状态对象数
maxIdle: 200
#jedis池没有对象返回时,最大等待时间
maxWaitMillis: 10000
testOnBorrow: true
testOnReturn: true
blockWhenExhausted: false
#Idle时进行连接扫描
testWhileIdle: true
#表示idle object evitor两次扫描之间要sleep的毫秒数
timeBetweenEvictionRunsMillis: 30000
#表示idle object evitor每次扫描的最多的对象数
numTestsPerEvictionRun: 10
#表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
minEvictableIdleTimeMillis: 60000
jedis配置类:
@Configuration
@Data
public class RedisConfig { private Logger logger = LoggerFactory.getLogger(RedisConfig.class); @Bean(name = "jedis.pool")
@Autowired
public JedisSentinelPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${spring.redis.sentinel.master}") String clusterName,
@Value("${spring.redis.sentinel.nodes}") String sentinelNodes,
@Value("${spring.redis.timeout}") int timeout,
@Value("${spring.redis.password}") String password) {
logger.info("缓存服务器的主服务名称:" + clusterName + ", 主从服务ip&port:" + sentinelNodes);
Assert.isTrue(StringUtils.isNotEmpty(clusterName), "主服务名称配置为空");
Assert.isTrue(StringUtils.isNotEmpty(sentinelNodes), "主从服务地址配置为空"); Set<String> sentinels = Sets.newHashSet(StringUtils.split(sentinelNodes, ",")); JedisSentinelPool sentinelJedisPool = new JedisSentinelPool(clusterName, sentinels, config, Protocol.DEFAULT_TIMEOUT, password); return sentinelJedisPool;
} @Bean(name = "jedis.pool.config")
public JedisPoolConfig jedisPoolConfig(@Value("${spring.redis.jedis.pool.maxTotal}") int maxTotal,
@Value("${spring.redis.jedis.pool.maxIdle}") int maxIdle,
@Value("${spring.redis.jedis.pool.maxWaitMillis}") int maxWaitMillis,
@Value("${spring.redis.jedis.pool.testOnBorrow}") boolean testOnBorrow,
@Value("${spring.redis.jedis.pool.testOnReturn}") boolean testOnReturn,
@Value("${spring.redis.jedis.pool.blockWhenExhausted}") boolean blockWhenExhausted,
@Value("${spring.redis.jedis.pool.testWhileIdle}") boolean testWhileIdle,
@Value("${spring.redis.jedis.pool.timeBetweenEvictionRunsMillis}") long timeBetweenEvictionRunsMillis,
@Value("${spring.redis.jedis.pool.numTestsPerEvictionRun}") int numTestsPerEvictionRun,
@Value("${spring.redis.jedis.pool.minEvictableIdleTimeMillis}") long minEvictableIdleTimeMillis) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
config.setBlockWhenExhausted(blockWhenExhausted);
config.setTestWhileIdle(testWhileIdle);
config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
config.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); return config;
} }
RedisClient类:
@Component
public class RedisClient { private static final Logger logger = LoggerFactory.getLogger(RedisClient.class); @Autowired
private JedisSentinelPool jedisPool; public Jedis getJedis() {
return jedisPool.getResource();
} /**
* 写入缓存
*
* @param key
* @param value
* @return Boolean
*/
public String set(final String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.set(key, String.valueOf(value));
} catch (Exception e) {
logger.error("[RedisClient] set e,", e);
return "";
} finally {
close(jedis);
}
} /**
* 读取缓存
*
* @param key
* @return
*/
public Optional<String> get(final String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return Optional.ofNullable(jedis.get(key));
} catch (Exception e) {
logger.error("[RedisClient] get exception,", e);
return Optional.empty();
} finally {
close(jedis);
}
}
}
源码参照:Github
springboot 集成Redis一主二从三哨兵的更多相关文章
- docker-compose一键部署redis一主二从三哨兵模式(含密码,数据持久化)
本篇基于centos7服务器进行部署开发 一.拉取redis镜像,使用如下命令 docker pull redis 1.查看镜像是否拉取成功,使用如下命令 docker images 显示如下则证明拉 ...
- redis 一主二从三哨兵
总体部署 一主二从三哨兵 ip地址分配分别为 主 127.0.0.1:6379 从 127.0.0.1:6389 从 127.0.0.1:6399 哨兵 127.0.0.1:26379 哨兵 127. ...
- redis一主二从三哨兵
redis做集群的时候有很多种配置方法,一主二从三哨兵这种模式是官网推荐的.,写配置文件链接的时候,写的是哨兵地址,不是IP,用户名,密码之类的. 一主二从很好理解,一个主的redis,实时备份到两个 ...
- Docker Compose搭建Redis一主二从三哨兵高可用集群
一.Docker Compose介绍 https://docs.docker.com/compose/ Docker官方的网站是这样介绍Docker Compose的: Compose是用于定义和运行 ...
- docker compose搭建redis7.0.4高可用一主二从三哨兵集群并整合SpringBoot【图文完整版】
一.前言 redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群. redis有两种高可用的方案: High availability with Re ...
- redis环境搭建及一主二从三哨兵模式配置
一.单机redis环境搭建 1.安装: OS:linux redhat6.5 下载redis 官网下载链接:https://redis.io/download 把安装包上传到服务器,进行解压 [roo ...
- 实践 - 搭建Redis一主两从三哨兵
实践 - 搭建Redis一主两从三哨兵 原因: 最近在复习Redis的时候,学习到了为了提高Redis集群的高可用性,有一个模式为哨兵模式.哨兵模式的作用是为了在主节点出现阻塞或者错误,无法接收数据的 ...
- redis一主二从加哨兵
redis版本:redis-3.0.6.tar.gz master:192.168.3.180 slave:192.168.3.184 (机器原因,两从都在这上面) 一.redis安装 cd /roo ...
- Docker搭建Redis一主两从三哨兵
作者:oscarwin juejin.im/post/5d26b03de51d454fa33b1960 这次实验准备了三台云主机,系统为Debian,ip分别为:35.236.172.131 ,35. ...
随机推荐
- python decode encode 解码与编码问题
python 解码与编码问题 1.decode 俗称解码,把编码解码成unicode,例如一个字符串变量 str 是utf-8编码,使用str.decode('utf-8') ,就是把utf-8编码 ...
- UVA P12101 【Prime Path】
题库 :UVA 题号 :12101 题目 :Prime Path link :https://www.luogu.org/problemnew/show/UVA12101
- icpc 江苏 D Persona5 组合数学 大数阶乘(分段阶乘) 大数阶乘模板
Persona5 is a famous video game. In the game, you are going to build relationship with your friends. ...
- codeforces 805 D. Minimum number of steps(数学)
题目链接:http://codeforces.com/contest/805/problem/D 题意:只有一个操作就是将ab变成bba直到不能变为止,问最少边几次. 题解:这题可以多列几组来找规律, ...
- codeforces 496 E. Distributing Parts(贪心+set二分)
题目链接:http://codeforces.com/contest/496/problem/E 题意:有n场演出,每场演出都有限制的高音和低音.然后m个人给出每个人的极限高音和低音还有出场次数. 最 ...
- 淘淘购物系统 (Python)
#首页def tao_first(): t1 = '欢迎进入淘淘购物'.center(110) print(t1) print('~' * 130) t2 = '注册'.center(20) prin ...
- Linux开机启动过程(个人理解)
简述Linux启动过程 1)BIOS开机自检 2)MBR引导 3)启动引导程序菜单(GRUB) 4)加载内核 5)加载虚拟文件系统加载函数模块 6)启动系统进程 /sbin/init --->/ ...
- 第12讲-Java中的IO操作及对象的序列化与反序列化
1.知识点 1.1.课程回顾 1.2.本章重点 1.2.1 io操作 1.2.2 对象的序列化与反序列化 2.具体内容 2.1.Java IO 2.1.1.什么是IO IO其实就是输入.输出 I ...
- zookeeper集群部署问题排查记录
今天在三台虚拟机搭建zookeeper集群,一直连不通,然后进行了几个小时的斗争,做个记录. 具体部署方式网上有很多, 不在赘述.产生连接不同的问题主要有以下几个方面: 1.仔细检查配置文件. 是否有 ...
- java多线程之Executor 与 ExecutorService两个基本接口
一.Executor 接口简介 Executor接口是Executor框架的一个最基本的接口,Executor框架的大部分类都直接或间接地实现了此接口. 只有一个方法 void execute(Run ...