今天来用一个spring-data-redis封装好的redis队列功能。
首先要有个redis,没有去官网下载:https://redis.io/
基于springboot,省掉一些配置
首先在idea创建maven工程redis-message,目录结构如下
然后配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xzg</groupId>
<artifactId>redis</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
然后稍等一会儿,让依赖下载完毕,完成后应该是这样子:
开始写代码了,首先写个消息接收类,这类负责接受消息然后处理
package com.xzg.redis.message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Receiver {
private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);
public void receiveMessage(String message) {
LOGGER.info("Received <" + message + ">");
//TODO process data
}
}
然后写启动类Application,为了简单我把配置类和消息发送代码都放在了application里面。
其实应该分成Configuration.java和Sender.java的。
package com.xzg.redis;
import com.xzg.redis.message.Receiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@SpringBootApplication
public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("shop"));
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
@Bean
Receiver receiver() {
return new Receiver();
}
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
LOGGER.info("Sending message --> Buy a pair of shoes");
template.convertAndSend("shop", "Buy a pair of shoes");
}
}
这代码里面启动了RedisMessageListenerContainer并监听topic是shop的消息。
并注册接受类receiver,声明调用的方法receiverMessage
然后在main方法里通过redisTemplate给shop这个topic发消息,然后receiver应该能马上接受到消息。
开始运行!
妈蛋,报错了,错误信息很明显,没连上redis,我的redis在192.168.1.22上
在resources新增配置application.properties,内容如下:
spring.redis.host=192.168.1.22
再运行
完美!
redis的消息队列适用于并发量比较小的任务。而且作为mq也没有分组,消息容量速率等信息,关键是没有持久化机制数据易丢失,如果并发量大,对安全要求高就用kafka吧。