1.创建一个springboot工程
2.导入rabbitMQ相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3.yml配置文件配置mq五大参数即加上虚拟主机
spring:
rabbitmq:
host: 192.168.200.129 #rabbitMQ的IP地址
port: 5672 #rabbitMQ连接端口
username: test #登录用户名
password: test #登录密码
virtual-host: /test #虚拟主机
4.声明exchange、queue,并且绑定它们,这是一个配置类
@Configuration
public class RabbitMQConfig {
//1. 创建exchange - topic
@Bean
public TopicExchange getTopicExchange(){
return new TopicExchange("boot-topic-exchange",true,false);
}
//2. 创建queue
@Bean
public Queue getQueue(){
return new Queue("boot-queue",true,false,false,null);
}
//3. 绑定在一起
@Bean
public Binding getBinding(TopicExchange