栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMQ整合SpringBoot

RabbitMQ整合SpringBoot

maven依赖: 



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    com.tech
    tech-rabbitmq
    1.0
    tech-rabbitmq
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

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

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


配置:

server:
  port: 8080
spring:
  rabbitmq:
    host: 192.168.50.134
    port: 5672
    virtual-host: /dev
    username: tech
    password: tech

配置声明交换机和队列:

package com.tech.rabbitmq.spring;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {
    public static final String EXCHANGE_NAME="exchange_order";
    public static final String QUEUE="order_queue";

    
    @Bean
    public Exchange orderExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable(QUEUE).build();
    }

    
    @Bean
    public Binding orderBinding(Queue queue,Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("order.#").noargs();
    }
}

消息发送者:

package com.tech.rabbitmq.spring;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class MsgSendController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("send")
    String send(){
        for (int i = 0; i < 5; i++) {
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"order.new","新订单");
        }
        return "ok";
    }
}

消息监听者:

package com.tech.rabbitmq.spring;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Component
@RabbitListener(queues = "order_queue")
public class OrderMQListener {

    @RabbitHandler
    public void messageHandler(String body, Message message){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("msgTag="+deliveryTag);
        System.out.println("message="+message.toString());
        System.out.println("body="+body);
        System.out.println("*****************************");
    }
}

启动后访问 /send接口发送消息成功后,可以看到控制台输出日志:

sgTag=1
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=1, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=2
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=2, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=3
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=3, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=4
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=4, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************
msgTag=5
message=(Body:'新订单' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_order, receivedRoutingKey=order.new, deliveryTag=5, consumerTag=amq.ctag-f12rU2Z7lwPNGp7fa_tRjw, consumerQueue=order_queue])
body=新订单
*****************************

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389422.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号