SpringBoot 七、整合消息队列RabbitMQ

本文介绍如何在SpringBoot项目中整合RabbitMQ消息中间件,并通过示例演示消息发送与接收的过程。包括配置RabbitMQ依赖及参数设置、自定义消息序列化方式等关键技术点。

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

学习内容来自尚硅谷 雷丰阳老师的课程尚硅谷-SpringBoot整合篇

一、为什么需要消息队列?

二、

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES
550253309e6b        68bdffcb99c0        "docker-entrypoint..."   About an hour ago   Up About an hour    4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   myrabbitmq
f1ca149fdbd5        71a81cb279e3        "docker-entrypoint..."   5 days ago          Up About an hour    0.0.0.0:6379->6379/tcp                                                                       myredis
848c6f760608        mysql:5.5           "docker-entrypoint..."   13 days ago         Up About an hour    0.0.0.0:3306->3306/tcp                                                                       mysql01550253309e6b        68bdffcb99c0        "docker-entrypoint..."   About an hour ago   Up About an hour    4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   myrabbitmq
f1ca149fdbd5        71a81cb279e3        "docker-entrypoint..."   5 days ago          Up About an hour    0.0.0.0:6379->6379/tcp                                                                       myredis
848c6f760608        mysql:5.5           "docker-entrypoint..."   13 days ago         Up About an hour    0.0.0.0:3306->3306/tcp                                                                       mysql01

docker start rabbitmq

连接http://192.168.100.152:15672,用户名密码:guest,add new Exchanges and Queues, test publish message and get message:

在Springboot工程中加入rabbitmq 的依赖

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

application.xml中配置rabbitmq

spring.rabbitmq.host=192.168.100.152
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

使用RabbitTemplate 测试发送消息,可以看到发送消息成功,但是消息内容像乱码,这是因为content_type:application/x-java-serialized-object,我们可以使用MessageConverter来自定义序列化机制

@Autowired
	RabbitTemplate rabbitTemplate;

	@Test
	public void testSend(){
		Map<String,Object> map = new HashMap<>();
		map.put("msg","this exchange:exchange.direct to routingKey:atguitu.emp");
		map.put("data", Arrays.asList("hello",true,123));
		rabbitTemplate.convertAndSend("exchange.direct","atguigu.emps",map);
	}
	@Test
	public void testRecive(){
		//Message msg = rabbitTemplate.receive("atguigu.emps");
		Object msg = rabbitTemplate.receiveAndConvert("atguigu.emps");
		System.out.println(msg.getClass());
		System.out.println(msg);
	}
@Configuration
public class MyAmqpConfig {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

 

 

 

 

 

class org.springframework.amqp.core.Message
(Body:'[B@1473b8c0(byte[19])' MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=null, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=NON_PERSISTENT, expiration=null, priority=null, redelivered=true, receivedExchange=exchange.topic, receivedRoutingKey=atguigu, receivedDelay=null, deliveryTag=1, messageCount=2, consumerTag=null, consumerQueue=null])

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值