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

SpringBoot整合RabbitMQ详解

SpringBoot整合RabbitMQ详解

SpringBoot项目整合RabbitMQ详解 1. pom.xml引入依赖
 
   org.springframework.boot
   spring-boot-starter-amqp
 
2. application.yml配置rabbitMQ连接信息
# 应用名称
spring:
  application:
    name: sb-rabbitmq
  rabbitmq:
    host: 192.168.1.201
    port: 5672
    username: guest
    password: guest
    virtual-host: /
3. rabbitMQ配置类信息
package com.kkarma.config;


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

@Configuration
public class RabbitMQConfig {
    public static final String EXCHANGE_NAME = "springboot-ex-topic";
    public static final String QUEUE_NAME = "springboot-queue-one";
    public static final String ROUTING_KEY = "*.kkarma.*";

    @Bean
    public Exchange getExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).build();
    }

    @Bean
    public Queue getQueue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    @Bean
    public Binding getBinding(Queue queue, Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();
    }
}

4. 测试生产者发送消息
package com.kkarma;

import com.kkarma.config.RabbitMQConfig;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class PublisherTest {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public PublisherTest(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Test
    public void push() throws Exception {
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "com.kkarma.controller", "hello springboot rabbitmq");
        System.out.println("消息发送成功...");
    }
}
5. 测试消费者消费消息
package com.kkarma.consumer;


import com.kkarma.config.RabbitMQConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public Consumer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void pull(String msg, Channel channel, Message message) throws Exception {
        System.out.println("消费消息内容: " + msg);
        String correlationId = message.getMessageProperties().getCorrelationId();
        System.out.println("唯一标识: " + correlationId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}

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

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

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