Spring Boot整合RabbitMQ

创建提供者

  1. 通过idea的springboot项目快速搭建,创建springboot-rabbitmq-provider模块
  2. 保证pom.xml中存在这几个依赖
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
  1. 创建application.yml配置文件
spring:
  rabbitmq:
    port: 5672
    host: localhost
    virtual-host: /
    username: guest
    password: guest
  1. 将队列和交换机通过注解和配置类注入spring容器中
    因为启动类就相当于一个配置类了,所以直接在启动类下面进行bean注入
package com.javaee.springbootrabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringbootRabbitmqApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    }


    /**
     * 创建topic交换机并设置为长期存在
     *
     * @return topic交换机对象
     */
    @Bean("topicExchange")
    public Exchange topicExchange() {
        return ExchangeBuilder.topicExchange("topicExchange").durable(true).build();
    }

    /**
     * 创建topic队列交给spring
     *
     * @return 队列对象
     */
    @Bean("topicQueue")
    public Queue topicQueue() {
        return QueueBuilder.durable("topicQueue").build();
    }

    /**
     * 绑定交换机和队列
     *
     * @param topicExchange 绑定的topic交换机
     * @param topicQueue    绑定的队列
     * @return 返回绑定关系
     */
    @Bean
    public Binding topicQueueExchange(@Qualifier("topicExchange") Exchange topicExchange,
                                      @Qualifier("topicQueue") Queue topicQueue) {
		//指定交换机和队列进行绑定,并指定路由key的通配符
        return BindingBuilder.bind(topicQueue).to(topicExchange).with("order.#").noargs();
    }
}

  1. 写测试类
    通过测试类写入三条消息
package com.javaee.springbootrabbitmq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmqApplicationTests {

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Test
	public void contextLoads() {
		rabbitTemplate.convertAndSend("topicExchange","order.insert","执行订单插入。。。");
		rabbitTemplate.convertAndSend("topicExchange","order.update","执行订单修改。。。");
		rabbitTemplate.convertAndSend("topicExchange","order.delete","执行订单删除。。。");
	}

}

执行后能够从控制台发现确实存在三条消息
在这里插入图片描述

创建消费者

  1. 通过idea的springboot项目快速搭建,创建springboot-rabbitmq-consumer
  2. 保证pom.xml中存在这几个依赖
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
  1. 创建application.yml配置文件
spring:
  rabbitmq:
    port: 5672
    host: localhost
    virtual-host: /
    username: guest
    password: guest
  1. 编写消息监听器
package com.javaee.springbootrabbitmqconsumer;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MyListener {

	//将消费者绑定到指定队列
    @RabbitListener(queues = "topicQueue")
    public void myListener(Message message){
        System.out.println("消费者接受到的指令是:" + new String(message.getBody));
    }
   
}

  1. 执行springboot启动类,因为导入了rabbitMQ的依赖并且配置了消息监听器,所以程序运行后会先去监听并执行队列中的消息,执行完毕后会进入阻塞状态,等待队列出现消息。

执行结果
在这里插入图片描述
可以看到已经执行完消息队列中的所有消息,并进入阻塞状态。当还有用户发送消息时,阻塞中的程序会继续执行队列中的消息。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值