RabbitMQ - Spring boot 整合

本文详细介绍了如何在Spring Boot项目中整合RabbitMQ,涵盖了从新建项目到实现RPC异步调用的全过程。内容包括:设置POM和配置文件,创建主程序,实现简单、工作、ACK模式,发布/订阅、路由和主题模式,以及RPC模式的消费者和服务端。示例代码展示了生产者、消费者和测试类的编写方法。
摘要由CSDN通过智能技术生成

新建项目

新建项目
选择依赖

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.tedu</groupId>
	<artifactId>rabbitmq-springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>rabbitmq-springboot</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

application.yml

spring:
  rabbitmq:
    host: 192.168.64.140
    username: admin
    password: admin

主程序

删除自动创建的主程序

我们为每种模式创建一个包,在每个包中创建各自的主程序,单独测试.

在这里插入图片描述

简单模式

主程序

Spring提供的Queue类,是队列的封装对象,它封装了队列的参数信息.

RabbitMQ的自动配置类,会发现这些Queue实例,并在RabbitMQ服务器中定义这些队列.

package cn.tedu.m1;

import org.springframework.amqp.core.Queue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

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

	@Bean
	public Queue task_queue() {
   
		/*
		 * 可用以下形式: 
		 * new Queue("helloworld") - 持久,非排他,非自动删除
		 * new Queue("helloworld",false,false,false,null)
		 */
		return new Queue("helloworld",false);
	}
}

生产者

AmqpTemplate是rabbitmq客户端API的一个封装工具,提供了简便的方法来执行消息操作.

AmqpTemplate由自动配置类自动创建

package cn.tedu.m1;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SimpleSender {
   
	@Autowired
	AmqpTemplate t;
	
	public void send() {
   
		// 这里向 helloworld 队列发送消息
		t.convertAndSend("helloworld", "Hello world!! "+System.currentTimeMillis());
		System.out.println("消息已发送");
	}
}

消费者

通过@RabbitListener从指定的队列接收消息

使用@RebbitHandler注解的方法来处理消息

package cn.tedu.m1;

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

@Component
@RabbitListener(queues = "helloworld")
public class SimpleReceiver {
   
	@RabbitHandler
	public void receive(String msg) {
   
		System.out.println("收到: "+msg);
	}
}

这里还可以使用另一种形式:

@Component
public class SimpleReceiver {
    
	@RabbitListener(queues = "helloworld")
	public void receive(String msg) {
    
		System.out.println("收到: "+msg);
	}
}

另外,@RabbitListener 注解中也可以直接定义队列:

	@RabbitListener(queuesToDeclare = @Queue(name = "helloworld"
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扎瓦江石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值