Springboot2.x集成Redis集群模式

说明

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
cluster:
nodes:
- 192.168.8.121:7000
- 192.168.8.121:7001
- 192.168.8.121:7002
- 192.168.8.121:7003
- 192.168.8.121:7004
- 192.168.8.121:7005
- 192.168.8.121:7006
- 192.168.8.121:7007

nodes节点读取。因为nodes是集合方式,所以spring中的@value$("xxx.xxx.xx")是无法读取的,本文提供了一种获取改节点属性的方式。

RedisClusterNodesCfg.java 获取nodes节点数据的代码示例。

package top.enjoyitlife.redis;

import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterNodesCfg { private List<String> nodes; public List<String> getNodes() {
return nodes;
} public void setNodes(List<String> nodes) {
this.nodes = nodes;
} }

集群模式下的整合教程

Jedis客户端整合

JedisClusterConfig.java 相关配置

package top.enjoyitlife.redis.jedis;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import top.enjoyitlife.redis.RedisClusterNodesCfg; @Configuration
@Profile("JedisCluster")
public class JedisClusterConfig { @Autowired
private RedisClusterNodesCfg redisClusterNodesCfg; @Bean
public JedisConnectionFactory redisPoolFactory() throws Exception{
RedisClusterConfiguration rcc=new RedisClusterConfiguration();
List<String> nodesList=redisClusterNodesCfg.getNodes();
String host=null;
int port=0;
for(String node:nodesList) {
host=node.split(":")[0];
port=Integer.valueOf(node.split(":")[1]);
rcc.addClusterNode(new RedisNode(host,port));
}
return new JedisConnectionFactory(rcc);
} @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.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import top.enjoyitlife.redis.RedisClusterNodesCfg; @Configuration
@Profile("lettuceCluster")
public class LettuceClusterConfig { @Autowired
private RedisClusterNodesCfg redisClusterNodesCfg; @Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisClusterConfiguration rcc=new RedisClusterConfiguration();
List<String> nodesList=redisClusterNodesCfg.getNodes();
String host=null;
int port=0;
for(String node:nodesList) {
host=node.split(":")[0];
port=Integer.valueOf(node.split(":")[1]);
rcc.addClusterNode(new RedisNode(host,port));
}
return new LettuceConnectionFactory(rcc);
} @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通过RedisClusterConfiguration来统一了连接集群的方式,区别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("JedisCluster")
class JedisClusterTest { @Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void contextLoads() {
String name=redisTemplate.opsForValue().get("name").toString();
redisTemplate.opsForValue().set("hahha", "enjoyitlife2020");
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.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles; @SpringBootTest
@ActiveProfiles("lettuceCluster")
class LettuceClusterTest { @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. 7.redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗?

    作者:中华石杉 面试题 redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗? 面试官心理分析 在前几年, ...

  2. 突破Java面试-Redis集群模式的原理

    1 面试题 Redis集群模式的工作原理说一下?在集群模式下,key是如何寻址的?寻址都有哪些算法?了解一致性hash吗? 2 考点分析 Redis不断在发展-Redis cluster集群模式,可以 ...

  3. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  4. Redis集群模式配置

    redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...

  5. Redis集群模式之分布式集群模式

    前言 Redis集群模式主要有2种: 主从集群 分布式集群. 前者主要是为了高可用或是读写分离,后者为了更好的存储数据,负载均衡. 本文主要讲解主从集群.本章主要讲解后一半部分,Redis集群. 与本 ...

  6. springmvc3.2集成redis集群

    老项目需要集成redis集群 因为spring版本才从2.x升级上来,再升级可能改动较大,且并非maven项目升级麻烦,故直接集成. jar包准备: jedis-2.9.0.jar  -- 据说只有这 ...

  7. AWS 创建redis 集群模式遇到的问题

    问题描述 前几天在aws 平台创建了Redis 集群模式,但是链接集群的时候发现无法连接,返回信息超时. 通过参数组创建redis的时候提示报错:Replication group with spec ...

  8. 5分钟实现用docker搭建Redis集群模式和哨兵模式

    如果让你为开发.测试环境分别搭一套哨兵和集群模式的redis,你最快需要多久,或许你需要一天?2小时?事实是可以更短. 是的,你已经猜到了,用docker部署,真的只需要十几分钟. 一.准备工作 拉取 ...

  9. Spring Boot集成Redis集群(Cluster模式)

    目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...

随机推荐

  1. rsync+inotify实时数据同步单目录实战

    rsync+inotify实时数据同步单目录实战   rsync+inotify实时数据同步单目录实战 inotify是一个强大的.细粒度的.异步的文件系统事件监控机制,linux内核从2.6.13起 ...

  2. CodeForces-721C-Journey(DAG, DP)

    链接: https://vjudge.net/problem/CodeForces-721C 题意: Recently Irina arrived to one of the most famous ...

  3. tf.split( )和tf.unstack( )

    import tensorflow as tf A = [[1, 2, 3], [4, 5, 6]] a0 = tf.split(A, num_or_size_splits=3, axis=1)#不改 ...

  4. Memcached在Asp.net下的应用

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  5. PHP培训教程 php几个不起眼儿的小技巧

    说是不起眼儿的小技巧,其实应该说是不常用的常规应用吧.很多事情就是这样,知道是一马事儿,会用是一马事儿,精习又是另外一马事儿.而成为高手更是需要扎实的基本功. str_repeat 重复输出字符串就靠 ...

  6. CF 149E Martian Strings 后缀自动机

    这里给出来一个后缀自动机的题解. 考虑对 $s$ 的正串和反串分别建后缀自动机. 对于正串的每个节点维护 $endpos$ 的最小值. 对于反串的每个节点维护 $endpos$ 的最大值. 这两个东西 ...

  7. Android使用SDKManager下载SDK速度慢 容易丢包和异常的解决办法

    第一步, SDK Manager启动之后,会先解析一些google的xml文件,会在Log里面打印出一些日志信息. 解析完之后,把日志全部拷贝到一个记事本里(这一步是因为日志窗口不支持查找),在里面查 ...

  8. python学习之路(23)

    类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各 ...

  9. 微信小程序那些开发缺点

    早闻微信小程序是个坑,结果名不虚传,细数一下我开发小程序遇过到坑. UI组件过度封装. 微信小程序的组件是模仿react.js或vue.js的web组件设计的,并且封装了weui.css样式. PS: ...

  10. 微服务SpringCloud系列

    https://my.oschina.net/hmilyylimh?tab=newest&catalogId=5703366