-
-
-
org.springframework.boot -
spring-boot-starter -
-
-
-
org.springframework.boot -
spring-boot-devtools -
runtime -
true -
-
-
org.projectlombok -
lombok -
true -
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
org.springframework.boot -
spring-boot-starter-amqp -
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
-
org.projectlombok -
lombok -
-
-
-
-
-
- spring:
- rabbitmq:
- host: 43.143.246.208
- port: 5672
- username: root
- password: root
- virtual-host: /itcast
- @Configuration
- public class RabbitmqConfig {
- public static final String EXCHANGE_NAME = "boot_topic_exchange";
- public static final String QUEUE_NAME = "boot_queue";
- //1.交换机
- @Bean("bootExchange")
- public Exchange bootExchange(){
- return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
- }
- //2.Queue 队列
- @Bean("bootQueue")
- public Queue bootQueue(){
- return (Queue) QueueBuilder.durable(QUEUE_NAME).build();
- }
- //3. 队列和交互机绑定关系 Binding
- /*
- 1. 知道哪个队列
- 2. 知道哪个交换机
- 3. routing key
- */
- @Bean
- public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
- return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
- }
- }
- @SpringBootTest
- class MqsendApplicationTests {
- //1.注入RabbitTemplate
- @Autowired
- private RabbitTemplate rabbitTemplate;
- @Test
- public void testSend(){
- rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
- }
- }

- @Component
- public class RabbimtMQListener {
- @RabbitListener(queues = "boot_queue")
- public void ListenerQueue(Message message){
- //System.out.println(message);
- System.out.println(new String(message.getBody()));
- }
- }
运行结果
