文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

SpringBoot2.4.2下使用Redis配置Lettuce的示例

2024-04-02 19:55

关注

1. Springboot2.4.2下对Redis的基础集成

1.1 maven添加依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
             <version>2.4.2</version>
        </dependency>

注: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;

@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;
    
    @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;
    }
    
    @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;
    }
    
    @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;

@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;


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;

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);
    }
}

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;

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;

@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");
    }
}

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;

@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;

@Slf4j
@Component
public class RedisKeyExpireSubscriber extends KeyExpirationEventMessageListener {
    
    public RedisKeyExpireSubscriber(RedisMessageListenerContainer listenerContainer) {
        super (listenerContainer);
    }
    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.error (message.toString ());
    }
}

注意: Redis需要开启事件

到此这篇关于SpringBoot2.4.2下使用Redis配置Lettuce的文章就介绍到这了,更多相关SpringBoot配置Lettuce内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯