SpringBoot集成Redis Cluster集群(附带Linux部署Redis Cluster高可用集群)

news/2025/2/23 2:43:24/

目录

    • 一、前言
    • 二、集成配置
      • 2.1、POM
      • 2.2、添加配置文件application.yml
      • 2.3、编写配置文件
      • 2.4、编写启动类
      • 2.5、编写测试类测试是否连接成功

一、前言

这里会使用到spring-boot-starter-data-redis包,spring boot 2的spring-boot-starter-data-redis中,默认使用的是lettuce作为redis客户端,也推荐使用lettuce,Redis使用哨兵集群,这里会通过lettuce连接到哨兵获取对应Redis节点地址从而操作Redis。

Linux部署Redis Cluster高可用集群:https://blog.csdn.net/weixin_44606481/article/details/134052367

二、集成配置

工程结构
在这里插入图片描述

2.1、POM

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--springboot中的redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- lettuce pool 缓存连接池--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!-- 使用jackson作为redis数据序列化 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.11.4</version></dependency><!-- SpringBoot测试包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2.2、添加配置文件application.yml

因为我们用的spring-boot-starter-data-redis包会自动配置redis连接,在配置文件中添加对应配置即可

spring:#redis配置信息redis:## Redis数据库索引(默认为0)database: 0## Redis服务器连接密码(默认为空)password: 123456## 连接超时时间(毫秒)timeout: 5000## 集群配置cluster:### 集群中所有节点nodes:- 172.16.8.186:7001- 172.16.8.186:7002- 172.16.8.186:7003- 172.16.8.186:7004- 172.16.8.186:7005- 172.16.8.186:7006### 最大重定向数,最好为集群节点数,比如第一台挂了,连第二台,第二台挂了连第三台,这个是重新连接的最大数量max-redirects: 6lettuce:pool:## 连接池最大连接数(使用负值表示没有限制)max-active: 8## 连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: -1## 连接池中的最大空闲连接max-idle: 8## 连接池中的最小空闲连接min-idle: 1## 集群配置cluster:refresh:# 支持集群拓扑动态感应刷新,自适应拓扑刷新是否使用所有可用的更新,默认false关闭,类似nacos定时刷新服务列表adaptive: true# 定时刷新时间 毫秒period: 2000# 打印lettuce debug日志,方便查看读写分离效果
logging:pattern:console: '%date{yyyy-MM-dd HH:mm:ss.SSS} | %highlight(%5level) [%green(%16.16thread)] %clr(%-50.50logger{49}){cyan} %4line -| %highlight(%msg%n)'level:root: infoio.lettuce.core: debugorg.springframework.data.redis: debug

2.3、编写配置文件

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.lettuce.core.ReadFrom;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.util.HashSet;@Configuration
public class RedisConfig{/*** retemplate相关配置,配置自定义序列化规则为jackson*/@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();// 配置连接工厂template.setConnectionFactory(factory);//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和publicom.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jacksonSeial.setObjectMapper(om);// 值采用json序列化template.setValueSerializer(jacksonSeial);//使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());// 设置hash key 和value序列化模式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(jacksonSeial);template.afterPropertiesSet();return template;}
}

2.4、编写启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class LettuceApplication {public static void main(String[] args) {SpringApplication.run(LettuceApplication.class);}
}

2.5、编写测试类测试是否连接成功

import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest(classes =  LettuceApplication.class)
public class LettuceTest {@Autowiredprivate RedisTemplate<String,Object> redisTemplate;@Testpublic void t1(){String key = "key1";System.out.println("插入数据到redis");redisTemplate.opsForValue().set(key,"value1");Object value = redisTemplate.opsForValue().get(key);System.out.println("从redis中获取到值为 "+value);Boolean delete = redisTemplate.delete(key);System.out.println("删除redis中值 "+delete);}
}

http://www.ppmy.cn/news/1179798.html

相关文章

基于docker的Mysql版本升级

引言 随着版本的迭代和漏洞的提出&#xff0c;Mysql作为一款软件避免不了打补丁的命运。 由于docker版本的升级教程较少&#xff0c;以下基于docker版本的MySQL升级说明。 以下操作均在本地虚拟机中操作验证。 使用runlike查看启动脚本 镜像与镜像的差距就在启动命令的区别…

centos7安装redis(包含各种报错)

本文主要介绍如果在Centos7下安装Redis。 1.安装依赖 redis是由C语言开发&#xff0c;因此安装之前必须要确保服务器已经安装了gcc&#xff0c;可以通过如下命令查看机器是否安装&#xff1a; gcc -v如果没有安装则通过以下命令安装&#xff1a; yum install -y gcc2.下载r…

Redis -- 基础知识2

1.Redis客户端介绍 1.基础介绍 Redis是一种客户端-服务器结构的程序&#xff0c;通过网络进行互动 客户端的多种形态 1.自带了命令行客户端&#xff1a;redis-cil 2.图形化界面的客户端&#xff1a;依赖windows系统&#xff0c;连接服务器有诸多限制&#xff0c;不建议使用 3.基…

这是一个lonely的问题——二进制

这是一个lonely的问题 好嘞是闺蜜、不好嘞是敌蜜. 某天你的敌蜜对你施加了魔法&#xff0c;你被困在了数字世界&#xff01;你变成了一个数字 n. 你想要逃离这个可怕的世界&#xff0c;回到正常的生活。幸运的是&#xff0c;数字世界有一个神奇的魔法&#xff1a;存在一个数字 …

民安:以专业优势助力城市环境优化

随着城市化进程的加速&#xff0c;环境卫生问题日益受到公众的关注。为了解市民对城市环境卫生的满意度和需求&#xff0c;民安智库专业市场调查公司近期受委托开展了一项环境卫生满意度调查。 本次调查旨在全面了解市民对城市环境卫生的评价、对环境卫生工作的需求以及对政府…

基于Java的电影院订票管理系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09; 代码参考数据库参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&am…

搜维尔科技:【应用】配备MTi-3的轻便型ROV,在水下进行地理标记视觉检测

部署潜水员进行水下摄像&#xff0c;不仅难度高而且费用昂贵&#xff0c;需要受过潜水和摄像两方面培训的专业人员来进行。但有些水下作业任务例如拍摄海底管道内部的照片&#xff0c;由于人员无法进入或危险度高的原因&#xff0c;无法由潜水员完成。 如今&#xff0c;俄罗…

扫地机器人,不相信视觉导航

不可置否&#xff0c;激光雷达已经成为扫地机器人的“耶路撒冷”。 导航技术的从无到有 回顾扫地机器人的兴衰&#xff0c;本质是导航技术的从无到有、从弱到强、从少到多&#xff0c;而在这个过程中&#xff0c;激光雷达无疑发挥了无可替代的作用。2010年&#xff0c;第一台…