一般mq这种消息中间件都是在服务端创建而非在管理端创建
1.先导依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.先建立一个constants的包里面存放的都是一个静态变量
package cn.itcast.hotel.constants;
public class MqConstants {
//交换机
public final static String HOTEL_EXCHANGE="hotel.topic";
//增改消息队列
public final static String HOTEL_INSERT_QUEUE="hotel.insert.queue";
//删除消息队列
public final static String HOTEL_DELETE_QUEUE="hotel.delete.queue";
//增改Routingkey
public final static String HOTEL_INSERT_KEY="hotel.insert";
//删除Routingkey
public final static String HOTEL_DELETE_KEY="hotel.delete";
}
3.然后利用config中bean的方式来创建具体的bean
package cn.itcast.hotel.config;
import cn.itcast.hotel.constants.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MqConfig {
@Bean
public TopicExchange topicExchange(){
return new TopicExchange(MqConstants.HOTEL_EXCHANGE,true,false);
}
@Bean
public Queue insertQueue(){
return new Queue(MqConstants.HOTEL_INSERT_QUEUE,true);
}
@Bean
public Queue deleteQueue(){
return new Queue(MqConstants.HOTEL_DELETE_QUEUE,true);
}
@Bean
public Binding insertQueueBinding(){
return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.HOTEL_INSERT_KEY);
}
@Bean
public Binding deleteQueueBinding(){
return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstants.HOTEL_DELETE_KEY);
}
}
4.配置yml文件
spring:
rabbitmq:
host: 192.168.125.131
port: 5672
username: itcast
password: 123321
virtual-host: /
版权声明:本文为weixin_52369679原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。