RabbitMQ 笔记二

1.Spring 整合RabbitMQ

生产者 消费者
  1. 创建生产者工程
  2. 添加依赖
  3. 配置整合
  4. 编写代码发送消息
  1. 创建消费者工程
  2. 添加依赖
  3. 配置整合
  4. 编写消息监听器

2.创建工程RabbitMQ Producers

spring-rabbitmq-producers



    4.0.0
    
        org.example
        spring_rabbit_mq
        1.0-SNAPSHOT
    

    spring-rabbitmq-producers

    
        8
        8
        UTF-8
    

    
        
            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
        
    

RabbitMQ配置信息:rabbitmq.properties

rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.virtual-host=/

 RabbitMQ的配置信息:spring-rabbitmq-producer.xml




    
    
    
    
    
    
    
    

    
    
    
    

    
    
        
            
            
        
    

    
    
    
    
    

    
    
        
            
            
            
        
    

    
    

rabbitMQ发送消息的代码:ProducerTest.java

package org.example;

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 ProducerTest {

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

    /**
     * 测试rabbitMQ的简单工作模式
     */
    @Test
    public void testHelloWorld(){
        //2.发送消息
        rabbitTemplate.convertAndSend("spring_queue","hello world spring ....");
    }

    @Test
    public void testFanout(){
        //2.发送消息
        rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout ....");
    }

    @Test
    public void testTopic(){
        //2.发送消息
        rabbitTemplate.convertAndSend("spring_topic_exchange","heima.hehe.haha","spring topic ....");
    }
}

3.创建工程RabbitMQ Consumers

RabbitMQ consumer的pom文件和spring-rabbitmq-consumers项目的pom文件一致

RabbitMQ Consumer的配置文件:rabbitmq.properties和spring-rabbitmq-consumers项目的rabbitmq.properties文件一致

RabbitMQ Consumer的配置文件:spring-rabbitmq-consumer.xml




    
    
    
    

    
    
    
    
    
    
    

    
    
        
        
        
        
        
        
    

    
    

编写Listener代码

package org.example.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

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

监听器在配置文件绑定了对应的队列,当消费者启动起来的时候,监听到消息时,自动消费并打印。

ConsumerTest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class ConsumerTest {


    @Test
    public void test1() throws InterruptedException {
        Thread.sleep(5000);

    }
}

4.SpringBoot整合RabbitMQ

生产者

1.创建生产者SpringBoot工程

2.引入依赖

3.编写yml配置,基本信息配置

4.定义交换机,队列及绑定关系的配置类

5.注入RabbitTemplate,调用方法,完成消息发送

消费者

1.创建消费者SpringBoot工程

2.引入依赖

3.编写yml配置,基本信息配置

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

总结:

  1. springboot提供了快速整合RabbitMQ的方式
  2. 基本信息在yml中配置,队列、交换机及绑定关系在配置类中使用Bean的方式配置
  3. 生产端直接注入RabbitTemplate完成消息发送
  4. 消费端直接使用@RabbitListener完成消息接收

5.创建SpringBoot RabbitMQ Producers

pom.xml文件



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.7.9
		 
	
	com.example
	springboot-rabbitmq-producers
	0.0.1-SNAPSHOT
	springboot-rabbitmq-producers
	Demo project for Spring Boot  Producers
	
		8
	
	
		
			org.springframework.boot
			spring-boot-starter-amqp
		

		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.amqp
			spring-rabbit-test
			test
		
		
			junit
			junit
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


配置文件:application.yml

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtual-host: /

配置类:RabbitMQConfig.java

package com.example.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Configurable;
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

    /**
     * 绑定
     * @param queue 知道绑定哪个队列
     * @param exchange 知道哪个交换机
     * routingKey 路由键
     * @return
     */
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,@Qualifier("bootExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
}

测试方法

package com.example.test;

import com.example.config.RabbitMQConfig;
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.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
    //1.注入RabbitTemplage
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    @Test
    public void testSend(){
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello ~~~");
    }

}

6.创建SpringBoot RabbitMQ Consumers

配置文件:pom.xml和application.yml 和RabbitMQ Producer一样

Consumer的Listener 

package com.example.listener;

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()));
    }
}

测试方法

package com.example.test;


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.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ConsumerTest {

    @Test
    public void testReceive(){
        System.out.println("接收成功");
    }
}

你可能感兴趣的:(中间件,rabbitmq,笔记,分布式)