rabbitmq学习笔记2——消息发送与接收
创建项目
使用idea(版本2018.2),基于jdk1.8,springboot版本2.1,maven3.5.4
分别创建3个项目,group都为com.study.rabbitmq
项目1:framework,框架,存放共用的实体类
项目2:producer,生产者,存放消息发送服务
项目3:consumer,消费者,存放消息处理服务
1.framework项目,
创建package,命名为entity,在其下创建实体类Order,只为模拟,设置了两个属性,id与no
注意:需要实现Serializable接口,否则消息进行序列化时会报错。
package com.study.rabbitmq.framework.entity; import lombok.Data; import java.io.Serializable; @Data public class Order implements Serializable { private String id; private String no; }
2.producer项目
a.pom中增加对framework的依赖
<dependency>
<groupId>com.study.rabbitmq</groupId>
<artifactId>framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
b.配置文件中增加rabbitmq连接信息
spring:
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
server:
port: 8080
c.创建package,命名为service,其下创建消息发送服务,命名为MessageSendService
package com.study.rabbitmq.producer.service; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import com.study.rabbitmq.framework.entity.Order; import org.springframework.stereotype.Service; import java.util.UUID; @Service @Slf4j public class MessageSendService { @Autowired private RabbitTemplate rabbitTemplate; public void sendOrder(Order order) { String messageId=UUID.randomUUID().toString(); log.info(messageId); CorrelationData correlationData=new CorrelationData(); correlationData.setId(messageId); rabbitTemplate.convertAndSend("order-exchange","123",order,correlationData); } }
3.consumer项目
a.pom中增加对framework的依赖
<dependency>
<groupId>com.study.rabbitmq</groupId>
<artifactId>framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
b.配置文件中增加rabbitmq连接信息
spring:
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
listener:
simple:
acknowledge-mode: manual
concurrency: 5
max-concurrency: 10
server:
port: 8081
c.创建package,命名为service,其下创建消息处理服务,命名为MessageHandleService
package com.study.rabbitmq.consumer.service; import com.study.rabbitmq.framework.entity.Order; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.*; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.stereotype.Service; @Service @Slf4j public class MessageHanlerService { @RabbitListener(bindings = @QueueBinding( value = @Queue("order-queue"), exchange =@Exchange("order-exchange"), key ="123" )) @RabbitHandler public void handleOrder(@Payload Order order) { log.info("开始处理订单信息"); log.info("订单编号为:"+order.getNo()); } }
配置rabbitmq服务
使用rabbitmq自带的管理页面,http://localhost:15672/#/
1.创建exchange,命名为order-exchange
2.创建queue,命名为order-queue
3.绑定二者,路由key设置为123
我们的应用向rabbitmq发送消息需要以上信息。
此处涉及一些rabbitmq的基本概念,交换机、队列,请自行了解rabbitmq框架体系。
发送消息测试
为producer项目下的MessageSendService创建Junit测试
package com.study.rabbitmq.producer.service; import com.study.rabbitmq.framework.entity.Order; import org.junit.Test; import org.junit.runner.RunWith; 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 MessageSendServiceTest { @Autowired private MessageSendService orderService; @Test public void sendOrder() { Order order=new Order(); order.setId("1"); order.setNo("单号1247"); orderService.sendOrder(order); } }
运行测试
无报错,进入rabbitmq管理页面,http://localhost:15672/#/,进入queues的tab页,可看到,已经收到一条消息处于ready状态
接收消息
启动运行consumer项目,查看控制台输出的消息,确实消息已被接收及处理。