您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot2.4.2下怎么使用Redis配置Lettuce”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“SpringBoot2.4.2下怎么使用Redis配置Lettuce”吧!
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.4.2</version> </dependency>
注:Springboot2.4.2下默認(rèn)使用的就是Lettuce而不是Jedis因此無(wú)需在依賴進(jìn)行排除Jedis
首先Redis需要準(zhǔn)備一個(gè)配置文件,本文設(shè)定一個(gè)單獨(dú)的文件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
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 RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate<String, Serializable> 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; } }
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"; } }
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); }
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 RedisTemplate<String, Serializable> redisTemplate; @Autowired private ChannelTopic topic; public RedisMessagePublisher() { } public RedisMessagePublisher(final RedisTemplate<String, Serializable> redisTemplate, final ChannelTopic topic) { this.redisTemplate = redisTemplate; this.topic = topic; } @Override public void publish(final String message) { redisTemplate.convertAndSend (topic.getTopic (), message); } }
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 ())); } }
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 */ @Configuration public 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 RedisTemplate<String, Serializable> redisTemplate) { return new RedisMessagePublisher (redisTemplate, topic ()); } @Bean ChannelTopic topic() { return new ChannelTopic ("pubsub:queue"); } }
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"; } }
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 @Component public 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”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot2.4.2下怎么使用Redis配置Lettuce這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。