当前位置: 首页 > news >正文

Springboot集成redis--不同环境切换

1.单机配置

spring:redis:mode: singletonhost: 127.0.0.1port: 6379lettuce:pool:max-active: 8   #连接池最大阻塞等待时间(使用负值表示没有限制max-idle: 2     #连接池中的最大空闲连接min-idle: 1     #连接池最大阻塞等待时间(使用负值表示没有限制password: 123456

2.集群配置

spring:redis:cluster:nodes: 192.168.68.152:7000,192.168.68.152:7001,192.168.68.152:7002,192.168.68.152:7003,192.168.68.152:7004,192.168.68.152:7005lettuce:pool:max-active: 8   #连接池最大阻塞等待时间(使用负值表示没有限制max-idle: 2     #连接池中的最大空闲连接min-idle: 1     #连接池最大阻塞等待时间(使用负值表示没有限制

3.配置文件编写

package com.example.demo.config;import io.lettuce.core.ReadFrom;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import lombok.AllArgsConstructor;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
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.*;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import javax.annotation.Resource;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
/*** @author fuhao* @create 2023-09-07 15:42**/
@Configuration
@AllArgsConstructor
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(@Qualifier("redisConnectionFactory") RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());//设置value的序列化器GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setConnectionFactory(redisConnectionFactory);return redisTemplate;}@ResourceRedisProperties redisProperties;@Beanpublic GenericObjectPoolConfig poolConfig() {GenericObjectPoolConfig config = new GenericObjectPoolConfig();config.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());config.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());config.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());config.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis());return config;}/*** sentinel 哨兵模式configuration** */@Bean@ConditionalOnProperty(value = "spring.redis.mode",havingValue = "sentinel")public RedisSentinelConfiguration redisConfigurationModeSentinel() {RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration();redisConfig.setMaster(redisProperties.getSentinel().getMaster());if(redisProperties.getSentinel().getNodes()!=null) {List<RedisNode> sentinelNode=new ArrayList<RedisNode>();for(String sen : redisProperties.getSentinel().getNodes()) {String[] arr = sen.split(":");sentinelNode.add(new RedisNode(arr[0],Integer.parseInt(arr[1])));}redisConfig.setDatabase(redisProperties.getDatabase());redisConfig.setPassword(redisProperties.getPassword());redisConfig.setSentinelPassword(redisConfig.getPassword());redisConfig.setSentinels(sentinelNode);}return redisConfig;}/*** singleten单机 模式configuration** */@Bean@ConditionalOnProperty(value = "spring.redis.mode",havingValue = "singleton")public RedisStandaloneConfiguration redisConfigurationModeSingleton() {RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();standaloneConfiguration.setDatabase(redisProperties.getDatabase());standaloneConfiguration.setHostName(redisProperties.getHost());standaloneConfiguration.setPassword(redisProperties.getPassword());standaloneConfiguration.setPort(redisProperties.getPort());return standaloneConfiguration;}/*** cluster 模式configuration** */@Bean@ConditionalOnProperty(value = "spring.redis.mode",havingValue = "cluster")public RedisClusterConfiguration redisClusterConfigurationModeCluster() {RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());redisClusterConfiguration.setPassword(redisProperties.getPassword());return redisClusterConfiguration;}/*** singleton单机 模式redisConnectionFactory**/@Bean("redisConnectionFactory")@ConditionalOnProperty(value = "spring.redis.mode",havingValue = "singleton")public LettuceConnectionFactory redisConnectionFactoryModeSingleton(@Qualifier("poolConfig") GenericObjectPoolConfig config,RedisStandaloneConfiguration redisStandaloneConfiguration) {//注意传入的对象名和类型RedisSentinelConfigurationLettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfiguration);}/*** sentinel哨兵 模式redisConnectionFactory**/@Bean("redisConnectionFactory")@ConditionalOnProperty(value = "spring.redis.mode",havingValue = "sentinel")public LettuceConnectionFactory redisConnectionFactoryModeSentinel(@Qualifier("poolConfig") GenericObjectPoolConfig config,RedisSentinelConfiguration redisConfig) {//注意传入的对象名和类型RedisSentinelConfigurationLettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();return new LettuceConnectionFactory(redisConfig, clientConfiguration);}/*** cluster 模式redisConnectionFactory**/@Bean("redisConnectionFactory")@ConditionalOnProperty(value = "spring.redis.mode",havingValue = "cluster")public LettuceConnectionFactory redisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration) {ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enablePeriodicRefresh().enableAllAdaptiveRefreshTriggers().refreshPeriod(Duration.ofSeconds(5)).build();ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder().topologyRefreshOptions(clusterTopologyRefreshOptions).build();LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().readFrom(ReadFrom.REPLICA_PREFERRED).clientOptions(clusterClientOptions).build();return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);}}

4.pom.xml

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.37</version>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.11.1</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency>

相关文章:

Springboot集成redis--不同环境切换

1.单机配置 spring:redis:mode: singletonhost: 127.0.0.1port: 6379lettuce:pool:max-active: 8 #连接池最大阻塞等待时间&#xff08;使用负值表示没有限制max-idle: 2 #连接池中的最大空闲连接min-idle: 1 #连接池最大阻塞等待时间&#xff08;使用负值表示没有限…...

稀疏数组的实现

文章目录 目录 文章目录 前言 一 什么是稀疏数组? 二 稀疏数组怎么存储数据? 三 稀疏数组的实现 总结 前言 大家好,好久不见了,这篇博客是数据结构的第一篇文章,望大家多多支持! 一 什么是稀疏数组? 稀疏数组&#xff08;Sparse Array&#xff09;是一种数据结构&a…...

表达式语言的新趋势!了解SPEL如何改变开发方式

文章首发地址 SpEL&#xff08;Spring Expression Language&#xff09;是一种表达式语言&#xff0c;由Spring框架提供和支持。它可以在运行时对对象进行解析和计算&#xff0c;用于动态地构建和操作对象的属性、方法和表达式。以下是SpEL的一些特性和功能&#xff1a; 表达式…...

一套成熟的实验室信息管理系统(云LIS源码)ASP.NET CORE

一套成熟的实验室信息管理系统&#xff0c;集前处理、检验、报告、质控、统计分析、两癌等模块为一体的网络管理系统。它的开发和应用将加快检验科管理的统一化、网络化、标准化的进程。 LIS把检验、检疫、放免、细菌微生物及科研使用的各类分析仪器&#xff0c;通过计算机联…...

NPM使用技巧

NPM使用技巧 前言技巧全局模块位置PowerShell报错安装模块冲突 NPM介绍NPM命令使用方法基本命令模块命令查看模块运行命令镜像管理 常用模块rimrafyarn 前言 本文包含NodeJS中NPM包管理器的使用技巧&#xff0c;具体内容包含NPM介绍、NPM命令、常用模块等内容&#xff0c;还包…...

java学习一

目录 Java 与 C 的区别 Java程序是编译执行还是解释执行 编译型语言 解释型语言 Java 与 C 的区别 Java 是纯粹的面向对象语言&#xff0c;所有的对象都继承自 java.lang.Object&#xff0c;C 兼容 C &#xff0c;不但支持面向对象也支持面向过程。Java 通过虚拟机从而实现…...

PV PVC in K8s

摘要 在Kubernetes中&#xff0c;PV&#xff08;Persistent Volume&#xff09;和PVC&#xff08;Persistent Volume Claim&#xff09;是用于管理持久化存储的重要资源对象。PV表示存储的实际资源&#xff0c;而PVC表示对PV的声明性要求。当应用程序需要使用持久化存储时&…...

SAP-PP:基础概念笔记-5(物料主数据的MRP1~4视图)

文章目录 前言一、MRP1视图Base Unit of Measure&#xff08;UoM&#xff09;MRP 组采购组ABC 指示器Plant-Specific Material Status 特定的工厂物料状态MRP 类型 MRP TypeMRP 类型 MRP TypeMaster Production Scheduling(MPS) 主生产计划基于消耗的计划(CBP)再订货点Reorder-…...

【C语言】初阶测试 (带讲解)

目录 ① 选择题 1. 下列程序执行后&#xff0c;输出的结果为( ) 2. 以下程序的输出结果是&#xff1f; 3. 下面的代码段中&#xff0c;执行之后 i 和 j 的值是什么&#xff08;&#xff09; 4. 以下程序的k最终值是&#xff1a; 5. 以下程序的最终的输出结果为&#xff…...

用huggingface.Accelerate进行分布式训练

诸神缄默不语-个人CSDN博文目录 本文属于huggingface.transformers全部文档学习笔记博文的一部分。 全文链接&#xff1a;huggingface transformers包 文档学习笔记&#xff08;持续更新ing…&#xff09; 本部分网址&#xff1a;https://huggingface.co/docs/transformers/m…...

unity 物体至视图中心以及新对象创建位置

如果游戏对象不在视野中心或在视野之外&#xff0c; 一种方法是双击Hierarchy中的对象名称 另一种是选中后按F 新建物体时对象的位置不是在坐标原点&#xff0c;而是在当前屏幕的中心...

船舶稳定性和静水力计算——绘图体平面图,静水力,GZ计算(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

Python 网页爬虫的原理是怎样的?

网页爬虫是一种自动化工具&#xff0c;用于从互联网上获取和提取信息。它们被广泛用于搜索引擎、数据挖掘、市场研究等领域。 网页爬虫的工作原理可以分为以下几个步骤&#xff1a;URL调度、页面下载、页面解析和数据提取。 URL调度&#xff1a; 网页爬虫首先需要一个初始的U…...

python技术面试题合集(二)

python技术面试题 1、简述django FBV和CBV FBV是基于函数编程&#xff0c;CBV是基于类编程&#xff0c;本质上也是FBV编程&#xff0c;在Djanog中使用CBV&#xff0c;则需要继承View类&#xff0c;在路由中指定as_view函数&#xff0c;返回的还是一个函数 在DRF中的使用的就是…...

【linux命令讲解大全】089.使用tree命令快速查看目录结构的方法

文章目录 tree补充说明语法选项列表选项文件选项排序选项图形选项XML / HTML / JSON 选项杂项选项 参数实例 从零学 python tree 树状图列出目录的内容 补充说明 tree 命令以树状图列出目录的内容。 语法 tree [选项] [参数]选项 列表选项 -a&#xff1a;显示所有文件和…...

【C++】—— 单例模式详解

前言&#xff1a; 本期&#xff0c;我将要讲解的是有关C中常见的设计模式之单例模式的相关知识&#xff01;&#xff01; 目录 &#xff08;一&#xff09;设计模式的六⼤原则 &#xff08;二&#xff09;设计模式的分类 &#xff08;三&#xff09;单例模式 1、定义 2、…...

TheRouter 框架原理

TheRouter 框架入口方法 通过InnerTheRouterContentProvider 注册在AndroidManifest.xml中&#xff0c;在应用启动时初始化 <application><providerandroid:name"com.therouter.InnerTheRouterContentProvider"android:authorities"${applicationId}.…...

系列十二、Java操作RocketMQ之带标签Tag的消息

一、带标签的Tag消息 1.1、概述 RocketMQ提供消息过滤的功能&#xff0c;通过Tag或者Key进行区分。我们往一个主题里面发送消息的时候&#xff0c;根据业务逻辑可能需要区分&#xff0c;比如带有tagA标签的消息被消费者A消费&#xff0c;带有tagB标签的消息被消费者B消费&…...

Java面向对象学习笔记-1

前言 “Java 学习笔记” 是为初学者和希望加深对Java编程语言的理解的人们编写的。Java是一门广泛应用于软件开发领域的强大编程语言&#xff0c;它的语法和概念对于初学者来说可能有些复杂。这份学习笔记的目的是帮助读者逐步学习Java的基本概念&#xff0c;并提供了一系列示…...

el-table根据data动态生成列和行

css //el-table-column加上fixed后会导致悬浮样式丢失&#xff0c;用下面方法可以避免 .el-table__body .el-table__row.hover-row td{background-color: #083a78 !important; } .el-table tbody tr:hover>td {background: #171F34 !important; }html <el-table ref&quo…...

AI Agent与Agentic AI:原理、应用、挑战与未来展望

文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例&#xff1a;使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例&#xff1a;使用OpenAI GPT-3进…...

为什么需要建设工程项目管理?工程项目管理有哪些亮点功能?

在建筑行业&#xff0c;项目管理的重要性不言而喻。随着工程规模的扩大、技术复杂度的提升&#xff0c;传统的管理模式已经难以满足现代工程的需求。过去&#xff0c;许多企业依赖手工记录、口头沟通和分散的信息管理&#xff0c;导致效率低下、成本失控、风险频发。例如&#…...

什么是库存周转?如何用进销存系统提高库存周转率?

你可能听说过这样一句话&#xff1a; “利润不是赚出来的&#xff0c;是管出来的。” 尤其是在制造业、批发零售、电商这类“货堆成山”的行业&#xff0c;很多企业看着销售不错&#xff0c;账上却没钱、利润也不见了&#xff0c;一翻库存才发现&#xff1a; 一堆卖不动的旧货…...

docker 部署发现spring.profiles.active 问题

报错&#xff1a; org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...

LRU 缓存机制详解与实现(Java版) + 力扣解决

&#x1f4cc; LRU 缓存机制详解与实现&#xff08;Java版&#xff09; 一、&#x1f4d6; 问题背景 在日常开发中&#xff0c;我们经常会使用 缓存&#xff08;Cache&#xff09; 来提升性能。但由于内存有限&#xff0c;缓存不可能无限增长&#xff0c;于是需要策略决定&am…...

Python网页自动化Selenium中文文档

1. 安装 1.1. 安装 Selenium Python bindings 提供了一个简单的API&#xff0c;让你使用Selenium WebDriver来编写功能/校验测试。 通过Selenium Python的API&#xff0c;你可以非常直观的使用Selenium WebDriver的所有功能。 Selenium Python bindings 使用非常简洁方便的A…...

第一篇:Liunx环境下搭建PaddlePaddle 3.0基础环境(Liunx Centos8.5安装Python3.10+pip3.10)

第一篇&#xff1a;Liunx环境下搭建PaddlePaddle 3.0基础环境&#xff08;Liunx Centos8.5安装Python3.10pip3.10&#xff09; 一&#xff1a;前言二&#xff1a;安装编译依赖二&#xff1a;安装Python3.10三&#xff1a;安装PIP3.10四&#xff1a;安装Paddlepaddle基础框架4.1…...

阿里云Ubuntu 22.04 64位搭建Flask流程(亲测)

cd /home 进入home盘 安装虚拟环境&#xff1a; 1、安装virtualenv pip install virtualenv 2.创建新的虚拟环境&#xff1a; virtualenv myenv 3、激活虚拟环境&#xff08;激活环境可以在当前环境下安装包&#xff09; source myenv/bin/activate 此时&#xff0c;终端…...

负载均衡器》》LVS、Nginx、HAproxy 区别

虚拟主机 先4&#xff0c;后7...

小智AI+MCP

什么是小智AI和MCP 如果还不清楚的先看往期文章 手搓小智AI聊天机器人 MCP 深度解析&#xff1a;AI 的USB接口 如何使用小智MCP 1.刷支持mcp的小智固件 2.下载官方MCP的示例代码 Github&#xff1a;https://github.com/78/mcp-calculator 安这个步骤执行 其中MCP_ENDPOI…...