SpringBoot项目配置SpringDataRedis

ops/2024/10/21 13:40:26/

1 在SpringBoot中常规的配置

        <dependency><!--自带lettcute客户端--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>

RedisProperties类会映射yaml文件中的redis相关配置

    redis:host: 10.20.5.47port: 6379password:database: 0lettuce:pool:# Whether to enable the pool. Enabled automatically if "commons-pool2" is available. With Jedis, pooling is implicitly enabled in sentinel mode and this setting only applies to single node setup. 项目里需要引入commons-pool2依赖enabled: true min-idle: 16max-idle: 16max-active: 64max-wait: 3000
java">// 基于 spring boot starter data redis 3.0@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {/**key、value序列化配置*/@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);template.setKeySerializer(RedisSerializer.string());template.setHashKeySerializer(RedisSerializer.string());template.setHashValueSerializer(RedisSerializer.json());template.setValueSerializer(RedisSerializer.json());return template;}/*** 前提条件:redis使用lettuce作为连接池* 配置RedisTemplate,读取数据库1的数据.*/@Bean("redisTemplate1")public RedisTemplate<String, Object> redisTemplate1(LettuceConnectionFactory lettuceConnectionFactory) {LettuceClientConfiguration clientConfiguration = lettuceConnectionFactory.getClientConfiguration();RedisStandaloneConfiguration standaloneConfiguration = lettuceConnectionFactory.getStandaloneConfiguration();//新创建RedisStandaloneConfiguration实例, 记得设置所有的字段RedisStandaloneConfiguration newSandaloneConfiguration = new RedisStandaloneConfiguration(standaloneConfiguration.getHostName(), standaloneConfiguration.getPort());//指定要连接的索引newSandaloneConfiguration.setDatabase(1);newSandaloneConfiguration.setUsername(standaloneConfiguration.getUsername());newSandaloneConfiguration.setPassword(standaloneConfiguration.getPassword());LettuceConnectionFactory lettuceConnectionFactory1 = new LettuceConnectionFactory(newSandaloneConfiguration, clientConfiguration);// 不能忽略这个方法的调用,否则会出现异常。lettuceConnectionFactory1.afterPropertiesSet();RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(lettuceConnectionFactory1);template.setKeySerializer(RedisSerializer.string());template.setHashKeySerializer(RedisSerializer.string());template.setHashValueSerializer(RedisSerializer.json());template.setValueSerializer(RedisSerializer.json());return template;}
}

Redis连接池参数配置建议

阿里云参考文档


http://www.ppmy.cn/ops/27198.html

相关文章

uniapp + uView动态表单校验

项目需求&#xff1a;动态循环表单&#xff0c;并实现动态表单校验 页面&#xff1a; <u--form label-position"top" :model"tmForm" ref"tmForm" label-width"0px" :rulesrules><div v-for"(element, index) in tmForm…

R语言之如何安装R和RStudio软件

目录 简介下载R安装包安装R软件安装RStudio软件总结 简介 R软件是一种用于统计计算和数据分析的编程语言&#xff0c;它提供了丰富的函数和包来处理和分析各种数据集。R具有广泛的应用领域&#xff0c;包括统计学、生物信息学、金融学等。它是一个开源&#xff0c;免费的软件。…

AIGC | 如何使用AI绘画工具stable diffusion做品牌视觉延展

设计师在从事品牌视觉相关设计的过程中&#xff0c;往往离不开使用品牌视觉符号进行主题化或风格化设计&#xff0c;下面就为大家分享如何使用stable diffusion来辅助进行品牌视觉logo延展设计。 环境搭建 在进行设计之前我们需要先下载Controlnet QR Code Monster模型&#x…

限流的学习

限流算法&#xff1a; 滑动窗口算法 滑动日志算法 漏桶算法 令牌桶算法 redis分布式限流 1、固定窗口限流 固定窗口算法又叫计数器算法&#xff0c;是一种简单方便的限流算法。主要通过一个支持原子操作的计数器来累计 1 秒内的请求次数&#xff0c;当 1 秒内计数达到限流阈值…

Bert类模型也具备指令遵循能力吗?

深度学习自然语言处理 原创作者&#xff1a;Winnie BERT模型&#xff0c;依托Transformer架构及其大规模预训练&#xff0c;为自然语言处理领域带来了深远的影响。BERT模型架构包含多层双向Transformer编码器&#xff0c;通过这种结构&#xff0c;BERT及其家族成员&#xff0c;…

关于ChatGPT的论文Demo

ChatGPT: 解锁人工智能的无限可能 引言 随着科技的飞速发展&#xff0c;人工智能&#xff08;AI&#xff09;逐渐成为改变世界的强大力量。而在众多AI技术中&#xff0c;ChatGPT以其独特的魅力和广泛的应用领域&#xff0c;吸引了全球的关注。本文将深入探讨ChatGPT的技术原理…

Android SQLiteDatabase的使用详解

1、数据库–公共变量&#xff1a; 2、数据库–打开&#xff1a; 3、数据库–增&#xff1a; 4、数据库–删&#xff1a; 5、数据库–改&#xff1a; 6、数据库–查&#xff1a; 7、数据库–关闭&#xff1a; 8、数据库–辅助工具&#xff1a; 9、数据库–效果&…

“云卷数潮”云原生数据库分论坛亮点回顾!

4月29日&#xff0c;2024中国移动算力网络大会“云卷数潮”云原生数据库分论坛在江苏苏州举行。本次论坛不仅是技术交流的盛宴&#xff0c;更是行业发展趋势的风向标。论坛汇聚了众多企业领袖、专家学者及行业精英&#xff0c;共话云原生数据库技术发展&#xff0c;探讨行业最新…