spring boot中用redis实现消息队列

常用消息队列工具

1.RabbitMQ: 相对重量级高并发的情况,比如数据的异步处理 任务的串行执行等. 

2.Kafka: 基于Pull的模式来处理,具体很高的吞吐量,一般用来进行 日志的存储和收集. 

3.Redis: 轻量级高并发,实时性要求高的情况,比如缓存,秒杀,及时的数据分析(ELK日志分析框架,使用的就是Redis).

一、创建Maven工程,pom文件内容如下:

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.liuchao</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot</name>
  <description>springboot</description>
  <!-- 定义公共资源版本 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath /> 
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <!-- 上边引入 parent,因此 下边无需指定版本 -->
    <!-- 包含 mvc,aop 等jar资源 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 热部署 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>   
</dependency>
<!-- Redis消息队列 -->
<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>

二、创建Receiver消息接收者类:

package com.light.springboot.message;

import java.util.concurrent.CountDownLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class Receiver {
private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);
private CountDownLatch latch;

@Autowired
public Receiver(CountDownLatch latch){
this.latch = latch;
}
public void receiverMessage(String message){
LOGGER.info("Received <" + message + ">");
latch.countDown();
}

}

三、注入消息接收者和消息监听容器

@Bean

RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, 
MessageListenerAdapter listenerAdapter){
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("msg"));
return container;
}

@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver){
return new MessageListenerAdapter(receiver, "receiverMessage");
}

@Bean
Receiver receiver(CountDownLatch latch){
return new Receiver(latch);
}

@Bean
CountDownLatch latch(){
return new CountDownLatch(1);
}

@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory){
return new StringRedisTemplate(connectionFactory);

}

四、测试

在springboot入口的main方法加上如下内容:

public static void main(String[] args) throws Exception{
ApplicationContext ctx = SpringApplication.run(SpringbootApplication.class, args);
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
CountDownLatch latch = ctx.getBean(CountDownLatch.class);
LOGGER.info("Sending message...");
template.convertAndSend("msg", "Hello World!");
latch.await();
System.exit(0);

}

五、启动Spring Boot程序

控制台打印如下:



源码下载:https://github.com/lcGit0615/springboot

(*注)源码中bean、controller两个包下的类与本次项目的消息队列无关,可忽略。resource资源文件下有多个properties文件,是用于多环境切换,在本项目中只需添加application.properties与application-dev.properties。

推荐文章:SpringBoot基于Redis快速实现消息队列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值