SpringBoot2.4.2下怎么使用Redis配置Lettuce
发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,这篇文章主要讲解了"SpringBoot2.4.2下怎么使用Redis配置Lettuce",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringBo
千家信息网最后更新 2025年01月18日SpringBoot2.4.2下怎么使用Redis配置Lettuce
这篇文章主要讲解了"SpringBoot2.4.2下怎么使用Redis配置Lettuce",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringBoot2.4.2下怎么使用Redis配置Lettuce"吧!
1. Springboot2.4.2下对Redis的基础集成
1.1 maven添加依赖
org.springframework.boot spring-boot-starter-data-redis 2.4.2
注:Springboot2.4.2下默认使用的就是Lettuce而不是Jedis因此无需在依赖进行排除Jedis
1.2 添加Redis配置文件
首先Redis需要准备一个配置文件,本文设定一个单独的文件redis.properties 放在resource文件夹下
redis.properties
hostName = localhost port = 6379 password = password pool.maxIdle = 10000 pool.minIdle = 1000 pool.maxWaitMillis = 5000 pool.maxTotal = 2 database = 10
1.3 注册RedisTemplate和StringRedisTemplate的Bean
LettuceRedisConfig.java
package com.xxx.demo.redis;import com.fasterxml.jackson.databind.ObjectMapper;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;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.PropertySource;import org.springframework.data.redis.connection.RedisStandaloneConfiguration;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.core.StringRedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.io.Serializable;import java.time.Duration;/** * @author linkanyway * @version 1.0 * @name LettuceRedisConfig * @description TODO * @date 2022/01/11 22:44 */@Configuration@PropertySource("classpath:redis.properties")public class LettuceRedisConfig { @Value("${hostName}") private String hostName; @Value("${password}") private String password; @Value("${port}") private int port; @Value("${database}") private int database; @Value("${pool.maxIdle}") private int maxIdle; @Value("${pool.minIdle}") private int minIdle; @Value("${pool.maxWaitMillis}") private int maxWaitMillis; @Value("${pool.maxTotal}") private int maxTotal; /** * LettuceConnectionFactory * * @return */ @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration (); redisStandaloneConfiguration.setHostName (hostName); redisStandaloneConfiguration.setPort (port); redisStandaloneConfiguration.setPassword (password); redisStandaloneConfiguration.setDatabase (database); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig (); poolConfig.setMaxIdle (maxIdle); poolConfig.setMinIdle (minIdle); poolConfig.setMaxWaitMillis (maxWaitMillis); poolConfig.setMaxTotal (maxTotal); LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder ().commandTimeout (Duration.ofSeconds (10)).shutdownTimeout (Duration.ZERO).poolConfig (poolConfig).build (); LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory (redisStandaloneConfiguration, lettucePoolingClientConfiguration); lettuceConnectionFactory.setShareNativeConnection (false); return lettuceConnectionFactory; } /** * RedisTemplate * * @param connectionFactory * @return */ @Bean public RedisTemplateredisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate redisTemplate = new RedisTemplate<> (); redisTemplate.setKeySerializer (new StringRedisSerializer ()); redisTemplate.setValueSerializer (new GenericJackson2JsonRedisSerializer ()); redisTemplate.setConnectionFactory (connectionFactory); return redisTemplate; } /** * @param factory * @return */ @Bean public StringRedisTemplate configStringRedisTemplate(@Autowired LettuceConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate (factory); template.setEnableTransactionSupport (true); ObjectMapper mapper; GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer (); template.setValueSerializer (new StringRedisSerializer ()); template.setKeySerializer (new StringRedisSerializer ()); template.setHashKeySerializer (new StringRedisSerializer ()); template.setHashValueSerializer (new StringRedisSerializer ()); template.afterPropertiesSet (); return template; }}
1.4 编写一个控制器示例进行redis操作
package com.xx.demo.controller;import com.xxx.demo.redis.MessagePublisher;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author linkanyway * @version 1.0 * @name RedisController * @description TODO * @date 2022/01/11 22:37 */@RestController@RequestMapping("redis")public class RedisController { final StringRedisTemplate redisTemplate; public RedisController(StringRedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } @GetMapping("add") public String add() { redisTemplate.opsForValue ().set ("a", "1"); return "hi"; }}
2. 使用redis进行发布订阅
2.1 添加一个发布者的接口
package com.xxx.demo.redis;/** * @author linkanyway * @version 1.0 * @name MessagePublisher * @description TODO * @date 2022/01/11 23:45 */public interface MessagePublisher { void publish(final String message);}
2.2 添加一个发布者的实现类
RedisMessagePublisher.java
package com.xxx.demo.redis;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.listener.ChannelTopic;import java.io.Serializable;/** * @author linkanyway * @version 1.0 * @name RedisMessagePublisher * @description TODO * @date 2022/01/11 23:46 */public class RedisMessagePublisher implements MessagePublisher { @Autowired private RedisTemplateredisTemplate; @Autowired private ChannelTopic topic; public RedisMessagePublisher() { } public RedisMessagePublisher(final RedisTemplate redisTemplate, final ChannelTopic topic) { this.redisTemplate = redisTemplate; this.topic = topic; } @Override public void publish(final String message) { redisTemplate.convertAndSend (topic.getTopic (), message); }}
2.3 添加一个消息监听bean
RedisMessageSubscriber.java
package com.xxx.demo.redis;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import org.springframework.scheduling.annotation.Async;/** * @author linkanyway * @version 1.0 * @name RedisMessageSubscriber * @description TODO * @date 2022/01/11 23:47 */public class RedisMessageSubscriber implements MessageListener { @Override @Async public void onMessage(Message message, byte[] pattern) { System.out.println ("Message received: " + new String (message.getBody ())); }}
2.4 添加bean注册
RedisMessageConfig.java
package com.xxx.demo.redis;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.listener.ChannelTopic;import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;import java.io.Serializable;/** * @author linkanyway * @version 1.0 * @name RedisMessageConfig * @description TODO * @date 2022/01/11 23:44 */@Configurationpublic class RedisMessageConfig { @Bean MessageListenerAdapter messageListener() { return new MessageListenerAdapter (new RedisMessageSubscriber ()); } @Bean RedisMessageListenerContainer redisContainer(LettuceConnectionFactory factory) { final RedisMessageListenerContainer container = new RedisMessageListenerContainer (); container.setConnectionFactory (factory); container.addMessageListener (messageListener (), topic ()); return container; } @Bean MessagePublisher redisPublisher(@Autowired RedisTemplateredisTemplate) { return new RedisMessagePublisher (redisTemplate, topic ()); } @Bean ChannelTopic topic() { return new ChannelTopic ("pubsub:queue"); }}
2.5 改写之前的控制器如下
package com.xxx.demo.controller;import com.kreakin.demo.redis.MessagePublisher;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author linkanyway * @version 1.0 * @name RedisController * @description TODO * @date 2022/01/11 22:37 */@RestController@RequestMapping("redis")public class RedisController { final StringRedisTemplate redisTemplate; final MessagePublisher publisher; public RedisController(StringRedisTemplate redisTemplate, MessagePublisher publisher) { this.redisTemplate = redisTemplate; this.publisher = publisher; } @GetMapping("hi") public String hi() { redisTemplate.opsForValue ().set ("a", "1"); return "hi"; } @GetMapping("pub") public String pub() { publisher.publish ("sdfsf"); return "ok"; }}
3. 监听key的过期事件
RedisKeyExpireSubscriber.java
package com.xxx.demo.redis;import lombok.extern.slf4j.Slf4j;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.stereotype.Component;/** * @author linkanyway * @version 1.0 * @name RedisKeyExpireSubscriber * @description TODO * @date 2022/01/12 00:00 */@Slf4j@Componentpublic class RedisKeyExpireSubscriber extends KeyExpirationEventMessageListener { /** * Creates new {@link } for {@code __keyevent@*__:expired} messages. * * @param listenerContainer must not be {@literal null}. */ public RedisKeyExpireSubscriber(RedisMessageListenerContainer listenerContainer) { super (listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { log.error (message.toString ()); }}
注意: Redis需要开启事件
感谢各位的阅读,以上就是"SpringBoot2.4.2下怎么使用Redis配置Lettuce"的内容了,经过本文的学习后,相信大家对SpringBoot2.4.2下怎么使用Redis配置Lettuce这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
配置
文件
学习
事件
内容
发布者
就是
控制器
控制
监听
基础
思路
情况
接口
文件夹
文章
更多
消息
知识
知识点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
原神怎么查找服务器
吉安市软件开发
一个软件开发的整个流程
广州旅游软件开发电话
网络安全法第六十五条
珠海ibm服务器
北京大数据软件开发费用是多少
台式电脑服务器
软件开发后端指什么
垃圾服务器生产厂家
网络技术研究中心 项目
dns服务器 c
平度网络安全厂家
网络安全有哪些部门管理
杭州前端软件开发定制
怎么辨别服务器安装在哪台电脑
c 数据库读取数据库连接
c 开发数据库管理系统
韩国ip代理服务器
打鱼软件开发多少钱
数据库主要工作内容及结论
美国网络安全新规
浅谈你对网络安全的看法
注入网络安全
滨州快消品管理软件开发公司
程序的数据库文件在哪里
面向空间数据库面向高端用户
奇酷互联网络科技招兼职吗
自学网络安全工程师好吗
软件开发可以开设计费