主要内容Q?/strong>
2. 使用步骤
2.1 d依赖
首先Q在Spring Boot目?pom.xml 文g中添加RabbitMQ的依赖?/span>
<dependencies>
<!-- Spring Boot RabbitMQ Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 其他依赖... -->
</dependencies>
2.2 配置RabbitMQ
?application.properties ?application.yml 文g中配|RabbitMQ的连接信息?br data-filtered="filtered" />
application.properties CZ:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
或者,如果你?application.yml Q则配置如下Q?/span>
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
2.3 创徏消息发送?/strong>
接下来,我们创Z个消息发送者,使用RabbitTemplate来发送消息?/span>
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
@Autowired
private RabbitTemplate rabbitTemplate;
// 发送消息到名ؓ(f)"hello"的队?br data-filtered="filtered" />
public void send(String message) {
rabbitTemplate.convertAndSend("hello", message);
}
}