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!

```

相关推荐
非 白几秒前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼6 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒9 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
m0_7482386317 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
甜可儿24 分钟前
redis序列化设置
数据库·redis
Forget the Dream38 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek41 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务