Spring Boot 整合RabbitMQ(一):Hello World

Spring Boot 整合
环境:

RabbitMQ:3.7.4
Spring Boot:2.0.1.RELEASE
因为有 Starter POMs,在 Spring Boot 中整合 RabbitMQ 是一件非常容易的事,其中的 AMQP 模块就可以很好的支持 RabbitMQ。
我们可以使用 Spring Intializr 或 https://start.spring.io/ 创建一个 Spring Boot 工程,并勾选 RabbitMQ。
或者手动在 pom.xml 文件中加入了解springcloud架构可以加求求:三五三六二四七二五九

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

在 application.yml 中配置关于 RabbitMQ 的连接和用户信息,如果没有改 RabbitMQ 的默认配置的话,这里零配置即可启动。这里我们还定义了一些额外的配置备用。

spring:
  profiles:
    active: usage_message
  rabbitmq:
    port: 5672
tutorial:
  client:
    duration: 10000

生产者
Spring AMQP 让我们用少量的代码就能轻松实现消息的发送和接收。通过注入 AmqpTemplate 接口的实例来实现消息的发送,AmqpTemplate 接口定义了一套针对 AMQP 协议的基础操作。在 Spring Boot 中会根据配置来注入其具体实现 (AmqpTemplate 的默认实现就是 RabbitTemplate)。

public class Tut1Sender {
   

    @Autowired

    private AmqpTemplate template;

    @Autowired

    private Queue queue;

    /**
     * 用定时任务来模拟生产者定时发送消息
     */
    @Scheduled (fixedDelay = 1000, initialDelay = 500)
    public void send() {
   
        Stri
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值