Springboot2.x集成Redis集群模式
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集群模式的更多相关文章
- 7.redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗?
作者:中华石杉 面试题 redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗? 面试官心理分析 在前几年, ...
- 突破Java面试-Redis集群模式的原理
1 面试题 Redis集群模式的工作原理说一下?在集群模式下,key是如何寻址的?寻址都有哪些算法?了解一致性hash吗? 2 考点分析 Redis不断在发展-Redis cluster集群模式,可以 ...
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- Redis集群模式配置
redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...
- Redis集群模式之分布式集群模式
前言 Redis集群模式主要有2种: 主从集群 分布式集群. 前者主要是为了高可用或是读写分离,后者为了更好的存储数据,负载均衡. 本文主要讲解主从集群.本章主要讲解后一半部分,Redis集群. 与本 ...
- springmvc3.2集成redis集群
老项目需要集成redis集群 因为spring版本才从2.x升级上来,再升级可能改动较大,且并非maven项目升级麻烦,故直接集成. jar包准备: jedis-2.9.0.jar -- 据说只有这 ...
- AWS 创建redis 集群模式遇到的问题
问题描述 前几天在aws 平台创建了Redis 集群模式,但是链接集群的时候发现无法连接,返回信息超时. 通过参数组创建redis的时候提示报错:Replication group with spec ...
- 5分钟实现用docker搭建Redis集群模式和哨兵模式
如果让你为开发.测试环境分别搭一套哨兵和集群模式的redis,你最快需要多久,或许你需要一天?2小时?事实是可以更短. 是的,你已经猜到了,用docker部署,真的只需要十几分钟. 一.准备工作 拉取 ...
- Spring Boot集成Redis集群(Cluster模式)
目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...
随机推荐
- SpringMVC @ModelAttribute详解
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...
- python修炼之路——控制语句
Python编程之print python2.x和python3.x的print函数区别:python3.x的print函数需要加括号(),python2.x可以不加. #-*- coding:utf ...
- Apache Flink CEP 实战
本文根据Apache Flink 实战&进阶篇系列直播课程整理而成,由哈啰出行大数据实时平台资深开发刘博分享.通过一些简单的实际例子,从概念原理,到如何使用,再到功能的扩展,希望能够给打算使用 ...
- underscore的使用
1.链接 npm underscore:https://www.npmjs.com/package/underscore 官网:https://underscorejs.org/ 2.npm安装:np ...
- Linux培训教程 linux磁盘分区详解
在学习 Linux 的过程中,安装 Linux 是每一个初学者的第一个门槛.在这个过程中间,最大的困惑莫过于给硬盘进行分区.虽然,现在各种发行版本的 Linux 已经提供了友好的图形交互界面,但是很多 ...
- BZOJ 3197: [Sdoi2013]assassin 树形DP + 最小费用流 + 树的同构
Description Input Output 其实就是给出两颗树,求一种两种树同构的方式,使得不同颜色个数最少$.$树的重新构建,其实就是指定不同的点为根节点$.$ 好在树的重心有一个重要的性质: ...
- 拨号操作——android.intent.action.CALL
button_14.setOnClickListener(new View.OnClickListener() { @Override public void onClick ...
- cvpr 2019 workshop&oral session
1. Verification and Certification of Neural Networks神经网络的验证与认证 2. Automated Analysis of Marine Video ...
- bitmap相关工具类
一,bitmap工具 封装了以下方法: 1,获取activity屏幕截图,保存为图片文件 2,从文件中获取截图,返回bitmap对象 package com.ctbri.weather.utils; ...
- ScvQ的技术栈
前端:spring mybatis hibernate struts2 SpringMVC echache quartz 后台:servlet jsp jdbc io html css javascr ...