项目结构
本示例包含两个模块:
- RabbitMQ 生产者:发送消息到 RabbitMQ。
- RabbitMQ 消费者:从 RabbitMQ 接收消息。
1. 添加依赖
在 pom.xml
文件中添加 Spring Boot 和 RabbitMQ 的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2. 配置 RabbitMQ
在 application.yml
文件中配置 RabbitMQ 的连接信息:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
3. 生产者代码
创建一个简单的生产者,通过 HTTP 接口发送消息到 RabbitMQ。
3.1 生产者配置
@Configuration
public class RabbitConfig {
public static final String QUEUE_NAME = "hello_queue";
@Bean
public Queue helloQueue() {
return new Queue(QUEUE_NAME, true);
}
}
3.2 生产者控制器
@RestController
public class RabbitController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/send")
public String sendMessage(@RequestParam String message) {
rabbitTemplate.convertAndSend(RabbitConfig.QUEUE_NAME, message);
return "Message sent: " + message;
}
}
3.3 生产者主类
@SpringBootApplication
public class RabbitProducerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitProducerApplication.class, args);
}
}
4. 消费者代码
创建一个简单的消费者,从 RabbitMQ 接收消息。
4.1 消费者配置
@Configuration
public class RabbitConfig {
public static final String QUEUE_NAME = "hello_queue";
@Bean
public Queue helloQueue() {
return new Queue(QUEUE_NAME, true);
}
}
4.2 消费者监听器
@Component
public class RabbitListener {
@RabbitListener(queues = RabbitConfig.QUEUE_NAME)
public void receiveMessage(String message) {
System.out.println("Received message: " + message);
}
}
4.3 消费者主类
@SpringBootApplication
public class RabbitConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitConsumerApplication.class, args);
}
}
5. 测试
- 启动 RabbitMQ 服务。
- 启动生产者和消费者应用。
- 访问生产者的 HTTP 接口:
http://localhost:8080/send?message=HelloRabbitMQ
- 消费者控制台将打印接收到的消息:
Received message: HelloRabbitMQ
6. 总结
通过以上步骤,你可以在 Spring Boot 应用中快速集成 RabbitMQ,实现消息的发送和接收。这个简单的 “Hello World” 示例展示了 Spring Cloud 和 RabbitMQ 的基本用法,适合初学者入门。
- Queue:队列。计算机数据结构中的一种基本类型,遵循“先入先出”(FIFO)的原则,比如我们日常生活中常见的排队时的队伍就是一个队列。
- Message
Queue:消息队列,简称MQ。消息队列本质上也是队列,只不过队列中的元素为Message(消息),而消息则是服务之间最常见的通信方式。流行的MQ框架主要有RabbitMq、ActiveMq、ZeroMq、kafka,以及阿里开源的RocketMQ。 - AMQP:Advanced Message Queuing
Protocol,是一个提供统一消息服务的应用层标准高级消息队列协议,简单来说,它就是一个消息列队的协议,其标准高,要求严。 - Erlang:Erlang是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信所辖的CS-Lab开发,目的是创造一种可以应对大规模并发活动的编程语言和运行环境。
- RabbitMQ:RabbitMQ是一个实现了AMQP高级消息队列协议的消息队列服务,用Erlang语言实现。
package com.didispace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
package com.didispace.rabbit;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}
package com.didispace.rabbit;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@RabbitListener(queues = "hello")
public class Receiver {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver : " + hello);
}
}
package com.didispace.rabbit;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "hello " + new Date();
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("hello", context);
}
}
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=springcloud
spring.rabbitmq.password=123456
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>rabbitmq-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rabbitmq-hello</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>