栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

都能看会的springboot整合RabbitMQ教程,一看就懂

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

都能看会的springboot整合RabbitMQ教程,一看就懂

1.pom.xml配置

我们想在springboot中使用rabbitmq,就要在pom.xml文件中引入rabbitMQ依赖

        
            org.springframework.boot
            spring-boot-starter-amqp
        
2.配置信息

在yml文件中,配置相应的rabbitMQ基本信息,用于创建连接工厂

#配置rabbitmq的基本信息 ip,端口 username password 虚拟机
spring:
  rabbitmq:
    host: 127.0.0.1 #ip
    port: 5672
    username: guest
    password: guest
    Virtual-host: /
3.配置类 交换机

这里使用ExchangeBuilder构建一个交换机对象,这里选用的是topicExchange交换机。

参数是交换机的名字,durable指的是持否持久化,build是构建交换机。

队列

QueueBuilder,创建一个队列,durable指的是持久化,参数是队列的名称,build是构建队列。

绑定关系

 @Qualifier用名字注入交换机

1.知道哪个队列

2.知道哪个交换机

3.routing key

noargs指的是没有参数

import org.springframework.amqp.core.*;
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 = "topic_exchange";
    public static final String Queue_NAME = "topic_queue";

    //1.交换机
    @Bean("exchange")
    public Exchange ConfigExchange(){
    //交换机的名字    是否持久化   构建
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //2.Queue队列
    @Bean("queue")
    public Queue creatQueue(){
        return QueueBuilder.durable(Queue_NAME).build();
    }

    //3.队列和交换机的绑定关系

    

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

 注意:别到错包!

4.测试类(测试生产者发送消息)

这里使用RabbitTemplate去发送消息

第一个参数是交换机的名字

第二个参数传入routing key

第三个参数传入的是消息

import com.LLL.RabbitMQ.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 ProducerTest {

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

    @Test
    public void testSend(){
        
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"topic.t1","hello word");
    }

}

注意:别到错包! 

访问本地端口的15672

运行之后登录我们的RabbitMQ可视化控制台就可以看到队列中确实有消息

 点击第二个队列,找到下面的Get messages,点击GetMessage,可以看到我们传入的信息

 5.实现消费者

新建一个springboot项目,配置与上面相同

消费者只要获取指定队列的信息,即可封装到message中,然后启动主程序类打印出来。

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


@Component
public class RabbitMqListener {

    //监听队列名称
    @RabbitListener(queues = "topic_queue")
    public void ListenerQueue(Message message){
        System.out.println(message);
    }
}

注意:别到错包! 

使用System.out.println(new String(message.getBody()));可以获取到消息体内的信息!

 

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

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

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