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. ...
随机推荐
- 多线程编程学习六(Java 中的阻塞队列).
介绍 阻塞队列(BlockingQueue)是指当队列满时,队列会阻塞插入元素的线程,直到队列不满:当队列空时,队列会阻塞获得元素的线程,直到队列变非空.阻塞队列就是生产者用来存放元素.消费者用来获取 ...
- javaScript 基础知识汇总(四)
1.对象 概念:对象可以通过花括号{...} 和其中包含一些可选的属性来创建. 属性时一个键值对,键是一个字符串,值可以是任何类型. 对象的创建 let user = new Object(); // ...
- 在IIS下部署PHP
没有.net ramework 4.0 的要先安装 dotNetFx40_Full_x86_x64.exe PHP压缩包 推荐用5.6.29版 IIS下PHP压缩包下载地址:"http:// ...
- PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx
PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 在写 PyTorch 代码时,我们会发现一些功能重复的操作,比如卷积.激活.池化等操作.这些操作分别可 ...
- 【EDU68 E】 Count The Rectangles 数据结构算几何
CF # 题意 总共有5000条线段,这些线段要么水平,要么垂直,问这些线段组成了多少矩形. # 思路 这是一个n*n*(log)的思路 自己一开始想着枚举两条垂直边,想着怎么把水平的边插入,再进行冗 ...
- POJ-1062 昂贵的聘礼 (最短路)
POJ-1062 昂贵的聘礼:http://poj.org/problem?id=1062 题意: 有一个人要到1号点花费最少的钱,他可以花费一号点对应的价格,也可以先买下其他一些点,使得费用降低. ...
- codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))
题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...
- Atcode B - Colorful Hats(思维)
题目链接:http://agc016.contest.atcoder.jp/tasks/agc016_b 题解:挺有意思的题目主要还是模拟出最多有几种不可能的情况,要知道ai的差距不能超过1这个想想就 ...
- hdu6354 杭电第五场 Everything Has Changed 计算几何
Everything Has Changed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- lightoj 1074 - Extended Traffic(spfa+负环判断)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...