SpringBoot整合RabbitMQ

目录

一、Spring整合RabbitMQ

1、创建生产者

2、创建消费者

二、SpringBoot整合RabbitMQ

1、创建生产者

2、创建消费者


一、Spring整合RabbitMQ

1、创建生产者

1、创建工程

2、添加依赖


        
            org.springframework
            spring-context
            5.1.7.RELEASE
        

        
            org.springframework.amqp
            spring-rabbit
            2.1.8.RELEASE
        

        
            junit
            junit
            4.12
        

        
            org.springframework
            spring-test
            5.1.7.RELEASE
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.0
                
                    1.8
                    1.8
                
            
        
    

3、编写配置

rabbitmq.properties文件

rabbitmq.host=192.168.138.129
rabbitmq.port=5672
rabbitmq.username=admin
rabbitmq.password=admin
rabbitmq.virtual-host=/

spring-rabbitmq-producer.xml



    
    

    
    
    
    

    
        

    

    
    
    

    
    

    
    
        
            
            
        
    

    
    
    
    
    
    
    

    
        
            
            
            
        
    

    
    

4、编写代码发送消息

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testHelloWorld(){
        rabbitTemplate.convertAndSend("spring_queue","hello world spring...");
    }

    /**
     * 发送fanout消息
     */
    @Test
    public void testFanout(){
        //2.发送消息

        rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout....");
    }

    /**
     * 发送topic消息
     */
    @Test
    public void testTopics(){
        //2.发送消息

        rabbitTemplate.convertAndSend("spring_topic_exchange","zhangsan.heiihei.haha","spring topic....");
    }

}

 结果

SpringBoot整合RabbitMQ_第1张图片

2、创建消费者

1、创建工程

2、添加依赖


        
            org.springframework
            spring-context
            5.1.7.RELEASE
        

        
            org.springframework.amqp
            spring-rabbit
            2.1.8.RELEASE
        

        
            junit
            junit
            4.12
        

        
            org.springframework
            spring-test
            5.1.7.RELEASE
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.0
                
                    1.8
                    1.8
                
            
        
    

3、编写配置



    
    

    
    

    
   
    
        
       
    

4、编写消息监听器

public class SpringQueueListener  implements MessageListener {
    @Override
    public void onMessage(Message message) {
        //打印消息
        System.out.println(new String(message.getBody()));
    }
}

 SpringBoot整合RabbitMQ_第2张图片

SpringBoot整合RabbitMQ_第3张图片

二、SpringBoot整合RabbitMQ

1、创建生产者

1、创建springboot工程

2、添加依赖

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

3、配置yml文件

spring:
  rabbitmq:
    host: 192.168.138.129
    username: admin
    password: admin
    virtual-host: /
    port: 5672

4、定义交换机,队列及绑定关系

@Configuration
public class RabbitMQConfig {

    //定义交换机名称
    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    //定义队列名称
    public static final String QUEUE_NAME = "boot_queue";

    //创建交换机
    @Bean("bootExchange")
    public Exchange bootExchange(){
        //durable:是否持久化
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //创建队列
    @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){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }

}

5、注入RabbitTemplate,调用方法,发送消息

@SpringBootTest
class ProducerSpringbootApplicationTests {

    //1.注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void contextLoads() {
    }

    @Test
    public void testSend(){
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
    }
}

2、创建消费者

1、创建springboot工程

2、添加依赖

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

3、修改配置文件

spring:
  rabbitmq:
    host: 192.168.138.129
    username: admin
    password: admin
    virtual-host: /
    port: 5672

4、定义监听类,使用@RabbitListener注解完成队列监听

@Component
public class RabbimtMQListener {

    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue(Message message){
        System.out.println(new String(message.getBody()));
    }

}

SpringBoot整合RabbitMQ_第4张图片

 

删除消息队列

SpringBoot整合RabbitMQ_第5张图片

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