redis提供了redis列表这种轻量级的队列订阅监听服务,
redis队列的特点
1相对于kafka等队列消息服务,redis队列由于是基于内存操作,所以速度更快
2处理的数据量不如kafka大
3在一些异常场景下可能会丢失消息。
springboot实现redis队列订阅监听
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
redis队列消息发布类RedisSender
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
/**
* 生产者
*/
@Service
public class RedisSender {
@Autowired
private StringRedisTemplate stringRedisTemplate;
//向通道发送消息的方法
public void sendChannelMess(String channel, String message) {
stringRedisTemplate.convertAndSend(channel, message);
}
}
redis队列订阅监听类RedisSubListenerConfig
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework