springboot里面增加rabbitmq
时间: 2023-08-22 09:09:48 浏览: 48
在Spring Boot中增加RabbitMQ的配置可以按照以下步骤进行操作:
1. 在项目的pom.xml文件中添加RabbitMQ的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
```
2. 在application.yml或application.properties文件中添加RabbitMQ的配置信息,包括主机地址、虚拟主机、端口号、用户名和密码等:
```yaml
spring:
rabbitmq:
host: 101.26.156.147
virtual-host: test_host
port: 5672
username: test
password: 123456
```
3. 创建一个启动类,并使用`@SpringBootApplication`注解标记为Spring Boot应用的入口:
```java
@SpringBootApplication
public class RabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqApplication.class, args);
}
}
```
4. 创建交换器、队列并实现它们之间的绑定关系。可以使用`@RabbitListener`注解标记一个方法作为消息的接收者,并使用`@RabbitHandler`注解指定处理消息的方法:
```java
@Service
public class RabbitServiceImpl implements rabbitService {
@RabbitListener(queues = {"hello-java-queue"})
@RabbitHandler
public void receiveMessage(HelloEntity content) throws InterruptedException {
System.out.println("内容:" + content);
}
}
```
这样,你就可以在Spring Boot中成功增加RabbitMQ的配置了。