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

1.pom.xml配置

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

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;

/**
 * @author 45669
 */
@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.队列和交换机的绑定关系

    /**
     * @Qualifier用名字注入交换机
     * 1.知道哪个队列
     * 2.知道哪个交换机
     * 3.routing key
     * @param queue
     * @param exchange
     * @return
     */

    @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(){
        /*
        1.传入交换机的名称
        2.routing key (之前定义了匹配以topic开头的路由,t1可以换成别的)
        3.消息
         */
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"topic.t1","hello word");
    }

}

注意:别到错包! 

访问本地端口的15672

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

9dc8749619674d569d40212ecd9a718d.png

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

ace741d652e74533aba8065f3340518d.png

 5.实现消费者

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

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

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

/**
 * @author 45669
 */
@Component
public class RabbitMqListener {

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

注意:别到错包! 

3662160885dc42e08fc48b6f68c08439.png

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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗头实习生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值