SpringBoot整合RabbitMQ,笔记整理

1创建生产者工程springboot-rabbitmq-produce

2.修改pom.xml文件



    org.springframework.boot
    spring-boot-starter-parent
    2.6.0
    


    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
    
        org.springframework.boot
        spring-boot-starter-amqp
    
    
        org.springframework.boot
        spring-boot-starter-test
    
    
        junit
        junit
        test
    

3.启动类  com.javacto.ProducerApplication

package com.javacto;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}

4.创建application.yml

# 配置RabbitMQ的基本信息  ip 端口 username  password..
spring:
  rabbitmq:
    host: 192.168.64.139 # ip
    port: 5672
    username: javacto
    password: javacto
    virtual-host: /javacto

5.创建RabbitMQ队列与交换机绑定的配置类com.javacto.rabbitmq.config.RabbitMQConfig


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@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 QueueBuilder.durable(QUEUE_NAME).build();
    }
    //3. 队列和交互机绑定关系 Binding
    /*
        1. 知道哪个队列
        2. 知道哪个交换机
        3. routing key
     */
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        //  bind(队列).to(交换机).with(routingKey).noargs(不需要指定参数) 如果需要指它就.add()
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
}

6.创建com.javacto.test.ProducerTest 然后运行

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ="classpath:spring-rabbitmq-producer.xml")
public class ProductTest {
    //1.注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //2.发送消息
    @Test
    public  void testHelloWorld(){
        rabbitTemplate.convertAndSend("spring_queue","hello world spring222");
    }
    //2.发送消息
    @Test
    public  void testFanout(){
        rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout...");
    }
    //2.发送消息
    @Test
    public  void testTopics(){
        rabbitTemplate.convertAndSend("spring_topic_exchange","javacto.add","spring topic...");
    }
}

1创建消费者工程springboot-rabbitmq-produce

2.修改pom.xml文件



    org.springframework.boot
    spring-boot-starter-parent
    2.6.0
    


    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
    
        org.springframework.boot
        spring-boot-starter-amqp
    
    
        org.springframework.boot
        spring-boot-starter-test
    
    
        junit
        junit
        test
    

3.启动类  com.javacto.ProducerApplication

package com.javacto;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}

4.创建application.yml

# 配置RabbitMQ的基本信息  ip 端口 username  password..
spring:
  rabbitmq:
    host: 192.168.64.139 # ip
    port: 5672
    username: javacto
    password: javacto
    virtual-host: /javacto

5.创建RabbitMQ队列与交换机绑定的配置类com.javacto.rabbitmq.config.RabbitMQConfig

package com.javacto.mq;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQListener {
    @RabbitListener(queues = "boot_queue")
    public  void listenerQueue(Message message){
        System.out.println(new String(message.getBody()));
    }
}

6.启动springBoot工程即可

你可能感兴趣的:(RabbitMQ,spring,boot,java-rabbitmq,rabbitmq)