Springboot2.x集成Redis哨兵模式
Springboot2.x集成Redis哨兵模式
说明
Redis哨兵模式是Redis高可用方案的一种实现方式,通过哨兵来自动实现故障转移,从而保证高可用。
准备条件
pom.xml中引入相关jar
<!-- 集成Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Jedis 客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- lettuce客户端需要使用到 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
application.yml哨兵模式配置属性示例。
spring:
redis:
host: 192.168.8.121
port: 6379
password: enjoyitlife
timeout: 30000
jedis:
pool:
max-active: 256
max-wait: 30000
max-idle: 64
min-idle: 32
lettuce:
pool:
max-active: 256
max-idle: 64
max-wait: 30000
min-idle: 32
sentinel:
master: mymaster
nodes: 192.168.8.121:26379, 192.168.8.121:26380,192.168.8.121:26381
哨兵模式下的整合教程
Jedis客户端整合
JedisSentinleConfig.java 相关配置
package top.enjoyitlife.redis.jedis;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
@Configuration
@Profile("JedisSentinel")
public class JedisSentinleConfig {
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.sentinel.master}")
private String sentinelName;
@Value("${spring.redis.sentinel.nodes}")
private String[] sentinels;
@Bean
public JedisSentinelPool redisPoolFactory() throws Exception{
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
Set<String> sentinelsets = new HashSet<String>(Arrays.asList(sentinels));
JedisSentinelPool pool = new JedisSentinelPool(sentinelName,sentinelsets,jedisPoolConfig,password);
return pool;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
Lettuce客户端整合
package top.enjoyitlife.redis.lettuce;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@Profile("lettuceSentinel")
public class LettuceSentinelConfig {
@Value("${spring.redis.sentinel.master}")
private String sentinelName;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.sentinel.nodes}")
private String[] sentinels;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisSentinelConfiguration rsc= new RedisSentinelConfiguration();
rsc.setMaster(sentinelName);
List<RedisNode> redisNodeList= new ArrayList<RedisNode>();
for (String sentinel : sentinels) {
String[] nodes = sentinel.split(":");
redisNodeList.add(new RedisNode(nodes[0], Integer.parseInt(nodes[1])));
}
rsc.setSentinels(redisNodeList);
rsc.setPassword(password);
return new LettuceConnectionFactory(rsc);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
Springboot通过RedisSentinelConfiguration来统一了连接哨兵的方式,区别Jedis客户端是通过JedisConnectionFactory进行初始化,而Lettuce客户端是通过LettuceConnectionFactory初始化。
单元测试
Jedis单元测试
package top.enjoyitlife.redis.jedis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest
@ActiveProfiles("JedisSentinel")
class JedisSentinelTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void contextLoads() {
String name=redisTemplate.opsForValue().get("name").toString();
System.out.println(name);
}
}
Lettuce 单元测试
package top.enjoyitlife.redis.lettuce;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.lettuce.LettucePool;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
import redis.clients.jedis.JedisPool;
@SpringBootTest
@ActiveProfiles("lettuceSentinel")
class LettuceSentinelTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void contextLoads() {
String name = redisTemplate.opsForValue().get("name").toString();
System.out.println(name);
}
}
好了以上就是Springboot2.x集成Redis哨兵模式的代码示例,希望对你能有所帮助。谢谢阅读。
Springboot2.x集成Redis哨兵模式的更多相关文章
- Spring Boot 入门(十):集成Redis哨兵模式,实现Mybatis二级缓存
本片文章续<Spring Boot 入门(九):集成Quartz定时任务>.本文主要基于redis实现了mybatis二级缓存.较redis缓存,mybaits自带缓存存在缺点(自行谷歌) ...
- Logstash2.3.4趟坑之集成Redis哨兵模式
最新在使用Lostash2.3.4收集数据的时候,在读取redis数据的时候,报了如下的一个异常: 异常如下 Pipeline aborted due to error {:exception=> ...
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
- Redis哨兵模式主从同步不可以绑定127.0.0.1或者0.0.0.0,不然无法进行主从同步
Redis哨兵模式主从同步不可以绑定127.0.0.1或者0.0.0.0,不然无法进行主从同步,一定要绑定内网IP,而对于跨机房的问题,可以使用iptables进行nat转发来解决.
- Redis哨兵模式大key优化
目前,Redis哨兵模式,内存资源有限,有很多key大于500M,性能待优化.需要迁移至Redis-cluster集群中. 涉及到的key如下: 0,hash,duser_record, ...
- [Redis] Redis哨兵模式部署 - zz胖的博客
1. 部署Redis集群 redis的安装及配置参考[redis部署] 本文以创建一主二从的集群为例. 1.1 部署与配置 先创建sentinel目录,在该目录下创建8000,8001,8002三个以 ...
- Redis 哨兵模式(Sentinel)
上一篇我们介绍了 redis 主从节点之间的数据同步复制技术,通过一次全量复制和不间断的命令传播,可以达到主从节点数据同步备份的效果,一旦主节点宕机,我们可以选择一个工作正常的 slave 成为新的主 ...
- 搭建redis哨兵模式
搭建redis哨兵模式,一主两从三哨兵 1.从官网下载redis安装包:此处是redis-5.0.7.tar.gz 2.上传到目录 /utxt/soft 3.解压 4.cd /utxt/soft/ ...
- Redis哨兵模式的配置
绪论 现有三台设备,192.168.137.11.192.168.137.12和192.168.137.13,要求在三台设备上实现redis哨兵模式,其中192.168.137.11为master,其 ...
随机推荐
- 【HDU3308】LCIS
题目大意:维护一个长度为 N 的序列,支持单点修改,区间查询最长连续上升子序列的长度. 题解: 线段树维护一段区间左端点开始的 LCIS 长度,右端点开始的 LCIS 长度以及区间最优解.考虑进行合并 ...
- 「LCT」
终于在多篇题解和我的个人超常发挥下抄完了lct的所有题,kx死了. 理解 在我看来,实际上lct的板子没有什么考的,更重要的可能是起到一个数据结构的维护作用实际上就是出题人想给你找点乐子. 前几道题都 ...
- 深入理解vue 修饰符sync
[ vue sync修饰符示例] 在说vue 修饰符sync前,我们先看下官方文档:vue .sync 修饰符,里面说vue .sync 修饰符以前存在于vue1.0版本里,但是在在 2.0 中移除了 ...
- 【NOIP2016提高A组模拟9.15】Map
题目 分析 发现,当原图是一棵树的时候,那么新建一条边后,就会变成环套树, 而环内的所有点对都是安全点对,如果环中有k个点,答案就是\(k(k-1)\) 联想到,当把原图做一遍tarjan缩点,每个环 ...
- hbase字典顺序表(即ASCII码表顺序)
- 【leetcode】1217. Play with Chips
题目如下: There are some chips, and the i-th chip is at position chips[i]. You can perform any of the tw ...
- Proxy Class(代理类)
在使用二维数组时,我们可以使用a[][]来访问数组中的元素,这很显然是正确的也无需证明. 但如果要自己实现一个二维数组的时候,会发现如果想要重载符号[][],会被告知没有这个符号,这即引出了C++ o ...
- 模板引擎ejs
1.网站 https://ejs.co/ https://ejs.bootcss.com/ 2.app.js var http=require("http"); var ejs = ...
- 病毒 x
04:病毒 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65535kB 描述 有一天,小y突然发现自己的计算机感染了一种病毒!还好,小y发现这种病毒很弱,只是会把文档中的所有字 ...
- Oracle JET 使用RequireJS第三方工具或库引入
在 Oracle JET 应用程序中使用 RequireJS 添加第三方工具或库. 步骤: 1.如果使用工具框架脚手架,需要一下操作. a.使用 npm 安装你需要的库. npm install my ...