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哨兵模式的更多相关文章

  1. Spring Boot 入门(十):集成Redis哨兵模式,实现Mybatis二级缓存

    本片文章续<Spring Boot 入门(九):集成Quartz定时任务>.本文主要基于redis实现了mybatis二级缓存.较redis缓存,mybaits自带缓存存在缺点(自行谷歌) ...

  2. Logstash2.3.4趟坑之集成Redis哨兵模式

    最新在使用Lostash2.3.4收集数据的时候,在读取redis数据的时候,报了如下的一个异常: 异常如下 Pipeline aborted due to error {:exception=> ...

  3. Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...

  4. Redis哨兵模式主从同步不可以绑定127.0.0.1或者0.0.0.0,不然无法进行主从同步

    Redis哨兵模式主从同步不可以绑定127.0.0.1或者0.0.0.0,不然无法进行主从同步,一定要绑定内网IP,而对于跨机房的问题,可以使用iptables进行nat转发来解决.

  5. Redis哨兵模式大key优化

    目前,Redis哨兵模式,内存资源有限,有很多key大于500M,性能待优化.需要迁移至Redis-cluster集群中.        涉及到的key如下: 0,hash,duser_record, ...

  6. [Redis] Redis哨兵模式部署 - zz胖的博客

    1. 部署Redis集群 redis的安装及配置参考[redis部署] 本文以创建一主二从的集群为例. 1.1 部署与配置 先创建sentinel目录,在该目录下创建8000,8001,8002三个以 ...

  7. Redis 哨兵模式(Sentinel)

    上一篇我们介绍了 redis 主从节点之间的数据同步复制技术,通过一次全量复制和不间断的命令传播,可以达到主从节点数据同步备份的效果,一旦主节点宕机,我们可以选择一个工作正常的 slave 成为新的主 ...

  8. 搭建redis哨兵模式

    搭建redis哨兵模式,一主两从三哨兵   1.从官网下载redis安装包:此处是redis-5.0.7.tar.gz 2.上传到目录 /utxt/soft 3.解压 4.cd /utxt/soft/ ...

  9. Redis哨兵模式的配置

    绪论 现有三台设备,192.168.137.11.192.168.137.12和192.168.137.13,要求在三台设备上实现redis哨兵模式,其中192.168.137.11为master,其 ...

随机推荐

  1. container_of机制

    #include <stdio.h> #include <stdlib.h> /* 计算成员变量首部相对于结构变量首部的偏移量 */ #define offsetof(TYPE ...

  2. Docker MongoDB 部署

    docker search mongo 命令来查看可用版本: $ docker search mongo NAME DESCRIPTION STARS OFFICIAL AUTOMATED mongo ...

  3. rsync+inotify实时数据同步多目录实战

    rsync+inotify实时数据同步多目录实战       inotify配置是建立在rsync服务基础上的配置过程 操作系统 主机名 网卡eth0 默认网关 用途 root@58server1 1 ...

  4. 2019 第十届 SWPUCTF writeup(Pwn)

    p1KkHeap 0.环境 1.文件信息 2.文件开启的保护 3.IDA分析 main函数 add show edit delete delete功能出现了指针悬浮的问题,配合上tcache,可以任意 ...

  5. sqlserver表值函数调用方式

    Connection conn = sqlServerManage.sqlServerConn(); Statement stmt; ResultSet rs; // 组装sql StringBuff ...

  6. 【NOIP2014模拟8.17】Magical GCD

    题目 对于一个由正整数组成的序列, Magical GCD 是指一个区间的长度乘以该区间内所有数字的最大公约数.给你一个序列,求出这个序列最大的 Magical GCD. 分析 根据暴力的思想, \( ...

  7. Haproxy-4层和7层代理负载实战

    目录 HAProxy是什么 HAProxy的核心能力和关键特性 HAProxy的核心功能 HAProxy的关键特性 HAProxy的安装和运行 安装 运行 添加日志 使用HAProxy搭建L7负载均衡 ...

  8. UNIX环境--线程

    一.线程的概念 1.线程在进程中是负责执行代码的一个单位,可以说线程是进程的一部分.一个进程中至少要有一个主线程,进程可以拥有多个线程. 2.线程和进程一样,线程会共享进程的一些信息.比如,代码段.全 ...

  9. CDOJ 1146 A - 秋实大哥与连锁快餐店 最小生成树 Prim算法 稠密图

    题目链接 A - 秋实大哥与连锁快餐店 Time Limit:3000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu S ...

  10. Shell中Bash的基本功能(二)

    1 历史命令 1)历史命令的查看[root@localhost ~]# history [选项] [历史命令保存文件]选项:-c: 清空历史命令-w: 把缓存中的历史命令写入历史命令保存文件.如果不手 ...