# SpringBoot integrates RabbitMQ

SpringBoot integrates RabbitMQ

RabbitMQ

concept

RabbitMQ is a kind of message middleware that implements AMQP (Advanced Message Queuing Protocol). It originally originated in financial systems and used to store and forward messages in distributed systems. It performs well in terms of ease of use, scalability, and high availability. . RabbitMQ is mainly implemented to achieve two-way decoupling between systems. When the producer generates a large amount of data, the consumer cannot consume it quickly, so an intermediate layer is needed. Save this data.

Windows installation
Brief description
  • queue: queue, each queue can have multiple consumers, but a message will only be consumed by one consumer

  • exchange: Exchange, the queue can be bound to the exchange, and the exchange sends messages to the queue based on routing or other matching information.

mode
  • Simple mode: Direct connection mode without a switch. There is only one consumer in a queue

  • Work mode: multiple consumers in a queue

  • Direct mode: a switch is required, the routing key of the switch is used to accurately match the queue and send to the corresponding queue

  • Topic mode: A mode of fuzzy matching through routing and routing key. Wildcards are available. For example, key.1 will be obtained by the queue bound to routing key.*

  • fanout: Broadcast mode, no routing key is required, for all queues bound to the switch


SpringBoot integrates RabbitMq

Simple example

Introduce Maven dependency
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置 yml
spring:  
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
Create queues, exchanges
@Configuration
public class RebbitMqConfig {
    
    

    /**
     * 队列名
     */
    private static final String LOG_QUEUE = "log_queue";

    /**
     * 交换机名
     */
    public static final String LOG_EXCHANGE = "log_exchange";
    public static final String MESSAGE_QUEUE = "message_queue";
    public static final String MESSAGE_EXCHANGE = "message_exchange";
    public static final String DIRECT_ROUTINGKEY = "test";

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue createLogQueue() {
    
    
        return new Queue(LOG_QUEUE);
    }

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue createMessageQueue() {
    
    
        return new Queue(MESSAGE_QUEUE);
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public FanoutExchange logExchange() {
    
    
        return new FanoutExchange(LOG_EXCHANGE);
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public DirectExchange messageExchange() {
    
    
        return new DirectExchange(MESSAGE_EXCHANGE);
    }

    /**
     * 队列与交换机进行绑定
     * @return
     */
    @Bean
    public Binding bindingFanout() {
    
    
        return BindingBuilder.bind(createLogQueue()).to(logExchange());
    }

    /**
     * 队列与交换机绑定并添加路由key(direct和topic模式)
     * @return
     */
    @Bean
    public Binding bindingDirect() {
    
    
        return BindingBuilder.bind(createMessageQueue()).to(messageExchange()).with(DIRECT_ROUTINGKEY);
    }
}
Simple producer example
@RestController
@RequestMapping("/rabbit")
public class RabbitMqProduct {
    
    

    /**
     * 队列名
     */
    private static final String LOG_QUEUE = "log_queue";

    /**
     * 交换机名
     */
    public static final String LOG_EXCHANGE = "log_exchange";
    
    public static final String DIRECT_ROUTINGKEY = "test";


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/test1")
    public void sendMessage() {
    
    
        String context = "simple---> " + new Date();
        //如果没有配置默认交换机,直接传入queue的name
        rabbitTemplate.convertAndSend(LOG_QUEUE, context);
        //如果配置了默认的交换机,(交换机,queue_name,内容)
        rabbitTemplate.convertAndSend("", DIRECT_ROUTINGKEY, context);
    }

    @RequestMapping("/test2")
    public void sendMessages() {
    
    
        String context = "direct---> " + new Date();
        //(交换机名称,路由的key,内容)
        rabbitTemplate.convertAndSend(LOG_EXCHANGE, DIRECT_ROUTINGKEY, context);
    }
}

Simple consumer example
@Component
public class RabbitMqConsume {
    
    

    //基础注解,指定queue的名称,可以多个。如果是simple不需要交换机的直接写队列名称就好。
    //如果是其他的也想只指定一个queues——name的话,需要上面的配置类配置queue或者其他绑定关系
    @RabbitListener(queues = "log_queue")
    @RabbitHandler
    public void processSimpleMsg(String message) {
    
    
        System.out.println("########################received simple" + message);
    }

}

Simple integration is completed. . .

Guess you like

Origin blog.csdn.net/qq_37248504/article/details/112593639