SpringAMQP发布与订阅,多消费者、交换机、TopicExchange 、消息转换器

本文详细介绍了如何使用SpringAMQP实现发布订阅模式,包括基础消息队列功能、多消费者处理、交换机配置以及TopicExchange的使用。同时,探讨了SpringAMQP的消息转换器,通过定制MessageConverter实现JSON序列化,提高消息处理灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是SpringAMQP

SpringAmqp的官方地址:Spring AMQP

 

一、利用SpringAMQP实现HelloWorld中的基础消息队列功能

发送消息

步骤1:引入AMQP依赖 

因为publisherconsumer服务都需要amqp依赖,因此这里把依赖直接放到父工程mq-demo中:

     <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

步骤2:

1.在publisher服务中编写application.yml,添加mq连接信息:

spring:
  rabbitmq:
    host: 112.74.108.223 # 主机名
    port: 5672 # 端口
    virtual-host: / # 虚拟主机
    username: itcast # 用户名
    password: 123321 # 密码

2.在publisher服务中新建一个测试类,编写测试方法:

package cn.itcast.mq.helloworld;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSimpleQueue() {
        String queueName = "simple.queue";
        String message = "hello, spring amqp!";
        rabbitTemplate.convertAndSend(queueName, message);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值