spring boot 微服务 redis集群配置

spring boot 微服务 redis集群配置

1.redis 有三种集群模式  主从模式  哨兵模式(Sentinel)  Cluster模式引入redis依赖
   <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>io.lettuce.core</groupId><artifactId>lettuce-core</artifactId></dependency>
2.主从模式配置 一般实现redis的读写分离 yaml配置如下
     spring:redis:host: 127.0.0.1port: 6379password: your_passwordlettuce:pool:max-active: 8max-wait: -1max-idle: 8min-idle: 0shutdown-timeout: 100ms
2.1 在代码配置redis读写分离
  @Configurationpublic class RedisConfig {@Value("${spring.redis.host}")private String redisHost;@Value("${spring.redis.port}")private int redisPort;@Value("${spring.redis.password}")private String redisPassword;@Beanpublic RedisConnectionFactory masterRedisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);config.setPassword(RedisPassword.of(redisPassword));return new LettuceConnectionFactory(config);}@Beanpublic RedisConnectionFactory slaveRedisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("127.0.0.2", 6380);config.setPassword(RedisPassword.of(redisPassword));return new LettuceConnectionFactory(config);}@Beanpublic RedisTemplate<String, Object> masterRedisTemplate(RedisConnectionFactory masterRedisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(masterRedisConnectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}@Beanpublic RedisTemplate<String, Object> slaveRedisTemplate(RedisConnectionFactory slaveRedisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(slaveRedisConnectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}}
3. 哨兵模式(Sentinel)yaml配置
 spring:redis:cluster:nodes: 127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002password: your_redis_passwordtimeout: 5000ms
3.1 在代码中配置
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisClusterConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class RedisConfig {@Beanpublic RedisClusterConfiguration redisClusterConfiguration() {RedisClusterConfiguration configuration = new RedisClusterConfiguration();configuration.setClusterNodes(Arrays.asList("127.0.0.1:7000", "127.0.0.1:7001", "127.0.0.1:7002"));configuration.setPassword("your_redis_password");return configuration;}@Beanpublic LettuceConnectionFactory lettuceConnectionFactory(RedisClusterConfiguration redisClusterConfiguration) {return new LettuceConnectionFactory(redisClusterConfiguration);}@Beanpublic RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(lettuceConnectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}}
4. Cluster模式 yaml 配置如下
     spring:redis:cluster:nodes: 127.0.0.1:7000,127.0.0.2:7001,127.0.0.3:7002,127.0.0.4:7003,127.0.0.5:7004,127.0.0.6:7005database: 0password: your_password
4.1代码配置如下
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisClusterConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class RedisConfig {@Beanpublic RedisClusterConfiguration redisClusterConfiguration() {RedisClusterConfiguration configuration = new RedisClusterConfiguration();configuration.setClusterNodes(Arrays.asList("127.0.0.1:7000", "127.0.0.1:7001", "127.0.0.1:7002"));configuration.setPassword("your_redis_password");return configuration;}@Beanpublic LettuceConnectionFactory lettuceConnectionFactory(RedisClusterConfiguration redisClusterConfiguration) {return new LettuceConnectionFactory(redisClusterConfiguration);}@Beanpublic RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(lettuceConnectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}}
5.如果需要修改redis的键值对 序列化 如下配置
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;import java.nio.charset.Charset;/*** @author borui*/
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");private Class<T> clazz;public FastJson2JsonRedisSerializer(Class<T> clazz) {super();this.clazz = clazz;}@Overridepublic byte[] serialize(T t) throws SerializationException {if (t == null) {return new byte[0];}return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);}@Overridepublic T deserialize(byte[] bytes) throws SerializationException {if (bytes == null || bytes.length <= 0) {return null;}String str = new String(bytes, DEFAULT_CHARSET);return JSON.parseObject(str, clazz, JSONReader.Feature.SupportAutoType);}
}@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport {@Bean@SuppressWarnings(value = { "unchecked", "rawtypes" })public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory){RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);//JdkSerializationRedisSerializer serializer =new JdkSerializationRedisSerializer();// 使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(serializer);// Hash的key也采用StringRedisSerializer的序列化方式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer((new StringRedisSerializer()));template.afterPropertiesSet();return template;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/61817.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【2024 re:Invent现场session参加报告】打造生成式AI驱动的车间智能助手

前言 这次参加了 re:Invent 2024 的 Builders Session 「Building a generative AI–powered shop floor assistant」&#xff0c;在这里和大家分享一下内容&#xff01; Session 概要 Learn how to build a generative AI assistant to analyze data from industrial IoT se…

Golang HTTP 标准库的使用实现原理

一.使用&#xff1a; 启动http服务&#xff1a; package mainimport "net/http"func main() {http.HandleFunc("/wecunge", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("wecunge"))})http.ListenAndServe(":8080&quo…

深入理解 GitHub 高级应用:从分支管理到自动化工作流

GitHub 是世界上最流行的代码托管平台&#xff0c;提供了丰富的功能来帮助开发者管理和协作项目。从基本的代码版本控制到复杂的协作工作流&#xff0c;GitHub 提供了大量高级功能来提升团队效率和代码质量。本文将介绍 GitHub 的一些高级应用和功能&#xff0c;包括分支管理、…

【C++】数组

1.概述 所谓数组&#xff0c;就是一个集合&#xff0c;该集合里面存放了相同类型的数据元素。 数组特点&#xff1a; &#xff08;1&#xff09;数组中的每个数据元素都是相同的数据类型。 &#xff08;2&#xff09;数组是有连续的内存空间组成的。 2、一维数组 2.1维数组定…

速盾:高防cdn的搜索引擎回源是什么?

高防CDN&#xff08;Content Delivery Network&#xff09;是一种用于加速网站访问速度和增加安全性的服务&#xff0c;它通过将静态和动态内容缓存在全球分布的服务器上&#xff0c;从而将用户请求的响应时间降至最低&#xff0c;并提供有效的防御攻击的能力。在实际使用过程中…

[VUE]框架网页开发02-如何打包Vue.js框架网页并在服务器中通过Tomcat启动

在现代Web开发中&#xff0c;Vue.js已经成为前端开发的热门选择之一。然而&#xff0c;将Vue.js项目打包并部署到生产环境可能会让一些开发者感到困惑。本文将详细介绍如何将Vue.js项目打包&#xff0c;并通过Tomcat服务器启动运行。 1. 准备工作 确保你的项目能够正常运行,项…

ESP32-S3模组上跑通ES8388(13)

接前一篇文章&#xff1a;ESP32-S3模组上跑通ES8388&#xff08;12&#xff09; 二、利用ESP-ADF操作ES8388 2. 详细解析 上一回解析了es8388_init函数中的第6段代码&#xff0c;本回继续往下解析。为了便于理解和回顾&#xff0c;再次贴出es8388_init函数源码&#xff0c;在…

openEuler 22.03 使用cephadm安装部署ceph集群

目录 目的步骤规格步骤ceph部署前准备工作安装部署ceph集群ceph集群添加node与osdceph集群一些操作组件服务操作集群进程操作 目的 使用ceph官网的cephadm无法正常安装&#xff0c;会报错ERROR: Distro openeuler version 22.03 not supported 在openEuler上实现以cephadm安装部…

DevOps工程技术价值流:GitLab源码管理与提交流水线实践

在当今快速迭代的软件开发环境中&#xff0c;DevOps&#xff08;开发运维一体化&#xff09;已经成为提升软件交付效率和质量的关键。而GitLab&#xff0c;作为一个全面的开源DevOps平台&#xff0c;不仅提供了强大的版本控制功能&#xff0c;还集成了持续集成/持续交付(CI/CD)…

C++ 游戏开发入门

一、为什么选择 C 进行游戏开发 C 在游戏开发领域具有独特的地位。它兼具高效性与对底层硬件的良好控制能力&#xff0c;这使得它非常适合开发对性能要求极高的游戏核心引擎部分。许多知名的大型游戏&#xff0c;如《使命召唤》系列、《虚幻竞技场》等&#xff0c;其底层架构都…

Spring 邮件发送

Spring 邮件发送 1. 主要内容&#xff08;了解&#xff09; 2. JavaMail 概述&#xff08;了解&#xff09; JavaMail&#xff0c;顾名思义&#xff0c;提供给开发者处理电⼦邮件相关的编程接⼝。JavaMail 是由 Sun 定义的⼀套收发电⼦邮件的 API&#xff0c;它可以⽅便地执⾏⼀…

VSCode如何关闭Vite项目本地自启动

某些情况下VSCode打开Vite项目不需要自动启动&#xff0c;那么如何关闭该功能 文件>首选项>设置 搜索vite 将Vite:Auto Start 勾选取消即可

算法训练营day27(回溯算法03:组合总和,组合总和2,分割回文串)

第七章 回溯算法part03● 39. 组合总和 ● 40.组合总和II ● 131.分割回文串详细布置 39. 组合总和 本题是 集合里元素可以用无数次&#xff0c;那么和组合问题的差别 其实仅在于 startIndex上的控制题目链接/文章讲解&#xff1a;https://programmercarl.com/0039.%E7%BB%84%E…

一种多功能调试工具设计方案开源

一种多功能调试工具设计方案开源 设计初衷设计方案具体实现HUB芯片采用沁恒微CH339W。TF卡功能网口功能SPI功能IIC功能JTAG功能下行USB接口 安路FPGA烧录器功能Xilinx FPGA烧录器功能Jlink OB功能串口功能RS232串口RS485和RS422串口自适应接口 CAN功能烧录器功能 目前进度后续计…

【Go底层】select原理

目录 1、背景2、go版本3、 selectgo函数解释【1】函数参数解释【2】函数具体解释第一步&#xff1a;遍历pollorder&#xff0c;选出准备好的case第二步&#xff1a;将当前goroutine放到所有case通道中对应的收发队列上第三步&#xff1a;唤醒groutine 4、总结 1、背景 select多…

Spark和MapReduce场景应用和区别

文章目录 Spark和MapReduce场景应用和区别一、引言二、MapReduce和Spark的应用场景1. MapReduce的应用场景2. Spark的应用场景 三、MapReduce和Spark的区别1. 内存使用和性能2. 编程模型和易用性3. 实时计算支持 四、使用示例1. MapReduce代码示例2. Spark代码示例 五、总结 Sp…

Python办公——openpyxl处理Excel每个sheet每行 修改为软雅黑9号剧中+边框线

目录 专栏导读背景1、库的介绍①&#xff1a;openpyxl 2、库的安装3、核心代码4、完整代码5、最快的方法(50万行44秒)——表头其余单元格都修改样式总结 专栏导读 &#x1f338; 欢迎来到Python办公自动化专栏—Python处理办公问题&#xff0c;解放您的双手 &#x1f3f3;️‍…

【C#】书籍信息的添加、修改、查询、删除

文章目录 一、简介二、程序功能2.1 Book类属性&#xff1a;方法&#xff1a; 2.2 Program 类 三、方法&#xff1a;四、用户界面流程&#xff1a;五、程序代码六、运行效果 一、简介 简单的C#控制台应用程序&#xff0c;用于管理书籍信息。这个程序将允许用户添加、编辑、查看…

01-树莓派基本配置-基础配置配置

树莓派基本配置 文章目录 树莓派基本配置前言硬件准备树莓派刷机串口方式登录树莓派接入网络ssh方式登录树莓派更换国内源xrdp界面登录树莓派远程文件传输FileZilla 前言 树莓派是一款功能强大且价格实惠的小型计算机&#xff0c;非常适合作为学习编程、物联网项目、家庭自动化…

Netty面试内容整理-核心组件和概念

Netty 的核心组件和概念是理解其工作机制和开发网络应用的重要基础。以下是 Netty 中的核心组件和它们的功能: Channel ● 概念:Channel 是 Netty 中用于网络通信的基本抽象,表示一个连接,类似于 Java NIO 中的 SocketChannel 和 ServerSocketChannel。 ● 功能: