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,其 ...
随机推荐
- Spring、SpringMVC注解方式整合
1 原理 Web容器在启动的时候,会扫描每个jar包下的META-INF/services/javax.servlet.ServletContainerInitializer文件. 加载META-IN ...
- python如何导入自定义文件和模块全部方法
项目中想使用自定义python文件(本地代码) 有6种方式, 1.这种最简单,也可能最不实用,将你的外部文件放到跟需要调用外部文件的文件同一个包下,同一目录 folder------toinvoke. ...
- SOA架构及其架构分析
一.什么是SOA SOA即面向服务的架构.分为三层结构:表示层(服务层).中间业务逻辑层.数据访问层. SOA是一种粗粒度.松耦合服务架构,服务之间通过简单.精确定义接口进行通讯,不涉及底层编程接口和 ...
- DevOps之持续集成SonarQube代码质量扫描
一.SonarQube介绍 SonarQube是一个用于代码质量检测管理的开放平台,可以集成不同的检测工具,代码分析工具,以及持续集成工具.SonarQube 并不是简单地把不同的代码检查 ...
- ef复杂模型添加
模型浏览器 函数导入-添加存储过程名称 添加复杂实体.复杂实体可以手动在xml里面创建.在complextype里面
- js能否上传文件夹
文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...
- Qt第三方库----QCustomPlot
一.软件下载 下载地址:http://www.qcustomplot.com/index.php/download 这里推荐下载第一个链接的内容: 注:这里的第三方库要放在非中文目录下. 二.配置 ( ...
- 更好的构建 Node 服务的工具
更好的构建 Node 服务的工具 无论前端项目在打包后都发送给后台, 有时候自己想看看效果在运行 npm run build 后只是看到一个 build 文件夹,但是直接打开是无法浏览,因此需要开启一 ...
- 织梦DedeCms技术资料
Dedecms调用文章发布时间的方法 11-20 样式 ([field:pubdate function='strftime("%m-%d",@me)'/]) May 15, 20 ...
- 8.Python标识符命名规范
简单地理解,标识符就是一个名字,就好像我们每个人都有属于自己的名字,它的主要作用就是作为变量.函数.类.模块以及其他对象的名称. Python 中标识符的命名不是随意的,而是要遵守一定的命令规则,比如 ...