springboot redis 实现消息队列

在Spring Boot中使用Redis作为消息队列,可以通过以下步骤实现:

  1. 添加依赖

在`pom.xml`文件中添加Spring Boot Redis和Jedis的依赖:

```xml

<dependencies>

<!-- Spring Boot Redis -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<!-- Jedis -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

</dependency>

</dependencies>

```

  1. 配置Redis

在`application.properties`文件中配置Redis连接信息:

```properties

spring.redis.host=localhost

spring.redis.port=6379

```

  1. 创建生产者

创建一个生产者类,用于发送消息到Redis消息队列:

```java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

@Component

public class Producer {

@Autowired

private RedisTemplate<String, String> redisTemplate;

public void sendMessage(String key, String message) {

redisTemplate.convertAndSend(key, message);

}

}

```

  1. 创建消费者

创建一个消费者类,用于从Redis消息队列接收消息:

```java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.connection.Message;

import org.springframework.data.redis.connection.MessageListener;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

@Component

public class Consumer implements MessageListener {

@Autowired

private RedisTemplate<String, String> redisTemplate;

@Override

public void onMessage(Message message, byte[] pattern) {

System.out.println("Received message: " + message.toString());

}

}

```

  1. 配置消费者监听

在配置类中,将消费者添加到Redis消息队列的监听器列表中:

```java

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.RedisConnectionFactory;

import org.springframework.data.redis.listener.ChannelTopic;

import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration

public class RedisConfig {

@Autowired

private Consumer consumer;

@Bean

public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,

MessageListenerAdapter listenerAdapter) {

RedisMessageListenerContainer container = new RedisMessageListenerContainer();

container.setConnectionFactory(connectionFactory);

container.addMessageListener(listenerAdapter, topic());

return container;

}

@Bean

public MessageListenerAdapter listenerAdapter() {

return new MessageListenerAdapter(consumer, "onMessage");

}

@Bean

public ChannelTopic topic() {

return new ChannelTopic("messageQueue");

}

}

```

  1. 测试发送和接收消息

在Spring Boot应用的主类中,注入生产者并发送一条消息:

```java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Application implements CommandLineRunner {

@Autowired

private Producer producer;

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

@Override

public void run(String... args) throws Exception {

producer.sendMessage("messageQueue", "Hello, Redis!");

}

}

```

运行应用后,控制台将输出接收到的消息:

```

Received message: Hello, Redis!

```

相关推荐
简 洁 冬冬1 分钟前
046 购物车
redis·购物车
xmh-sxh-131416 分钟前
jdk各个版本介绍
java
soulteary31 分钟前
突破内存限制:Mac Mini M2 服务器化实践指南
运维·服务器·redis·macos·arm·pika
天天扭码35 分钟前
五天SpringCloud计划——DAY2之单体架构和微服务架构的选择和转换原则
java·spring cloud·微服务·架构
程序猿进阶35 分钟前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
FIN技术铺40 分钟前
Spring Boot框架Starter组件整理
java·spring boot·后端
小曲程序1 小时前
vue3 封装request请求
java·前端·typescript·vue
陈王卜1 小时前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、1 小时前
Spring Boot 注解
java·spring boot
午觉千万别睡过1 小时前
RuoYI分页不准确问题解决
spring boot