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,其 ...
随机推荐
- Python分布式+云计算
参考: http://wiki.python.org/moin/ParallelProcessing http://wiki.python.org/moin/DistributedProgrammin ...
- pyqt5-表格TableWidGet
from PyQt5.QtWidgets import QApplication,QTableWidget,QWidget,QHeaderView,QPushButton,QTableWidgetIt ...
- ios获取系统当前日期并以一定格式显示
NSDate *date=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateform ...
- shiro框架学习-5-自定义Realm
1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...
- 新建的COM组件中没有 MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
创建ATL组件之后,添加简单ATL对象,添加对话框资源,随后发现没有m_hWnd句柄,不响应初始化函数. 于是重新创建ATL组件,之后添加ATL控件,添加对话框资源,有m_hWnd句柄,但仍然不响应初 ...
- PHP大文件分片上传断点续传实例源码
1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...
- BZOJ 4245: [ONTAK2015]OR-XOR 贪心 + 位运算
Description 给定一个长度为n的序列a[1],a[2],...,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的异或和,则总费用为c[1] or c[2] or ...
- #420 Div2 Problem B Okabe and Banana Trees (math && 暴力枚举)
题目链接 :http://codeforces.com/contest/821/problem/B 题意 :给出 m 和 b 表示在坐标轴上的一条直线 要求你在这条直线和x.y轴围成的区域中找出一个 ...
- 2018百度之星初赛B轮 p1m2
p1m2 Accepts: 954 Submissions: 4063 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/1310 ...
- VTK 编译过程中出现的hdf5长度(I64)错误解决办法
最近在使用vtk和cuda做大规模图像处理方面的问题研究,在编译vtk的过程中发现第三方库hdf5不能够解决I64长度的探测识别问题.为了节约大家的时间,现在把我经过实践得到的解决方案共享出来,这里要 ...