生产者发送MQ消息,消费者处理MQ消息,消息中间件的使用RocketMQ
也可以参照官网的例子:https://github.com/apache/rocketmq-spring (英)
https://github.com/apache/rocketmq-spring/blob/master/README_zh_CN.md (中)
0.RocketMQ环境部署:
1. 安装:http://rocketmq.apache.org/release_notes/release-notes-4.4.0/ 选择Binary进行下载
2. 配置系统变量 ROCKETMQ_HOME : D:\environment\rocketmq-all-4.4.0-bin-release
3. 启动 :Cmd命令框执行进入至‘MQ文件夹\bin’下,然后执行‘start mqnamesrv.cmd’,启动NAMESERVER。成功后会弹出提示框,此框勿关闭。然后执行mqbroker.cmd -n 127.0.0.1:9876;192.168.0.1:9876 autoCreateTopicEnable=true
1. 创建空项目项目mq,然后建生产者mq-producer和消费者mq-consumer两个module
两个项目引入相同的依赖:
<!--apache提供的rocketmq starter-->
<!-- https://mvnrepository.com/artifact/org.apache.rocketmq/rocketmq-spring-boot -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot</artifactId>
<version>2.0.1</version>
</dependency>
<!--引入日志依赖-->
<!-- Spring Boot log4j依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
2.生产者mq-producer和消费者mq-consumer,进行application.properties的配置
mq-producer中:
## mq-producer application name
spring.application.name=provider
# 请将上述示例配置中的接口,替换成真实RocketMQ的NameServer地址与端口
rocketmq.name-server=127.0.0.1:9876
rocketmq.producer.group=my-group
rocketmq.producer.send-message-timeout=300000
rocketmq.producer.compress-message-body-threshold=4096
rocketmq.producer.max-message-size=4194304
rocketmq.producer.retry-times-when-send-async-failed=0
rocketmq.producer.retry-next-server=true
rocketmq.producer.retry-times-when-send-failed=2
mq-consumer中
## mq-consumer application name
spring.application.name=consumer
rocketmq.name-server=127.0.0.1:9876
3.实体类
package com.xx.entity;
import java.io.Serializable;
import java.math.BigDecimal;
public class OrderPaidEvent implements Serializable {
private String orderId;
private BigDecimal paidMoney;
public OrderPaidEvent() {
}
public OrderPaidEvent(String orderId, BigDecimal paidMoney) {
this.orderId = orderId;
this.paidMoney = paidMoney;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public BigDecimal getPaidMoney() {
return paidMoney;
}
public void setPaidMoney(BigDecimal paidMoney) {
this.paidMoney = paidMoney;
}
}
4. 生产者代码编写:
@SpringBootApplication
public class MqProducerApplication implements CommandLineRunner {
@Resource
private RocketMQTemplate rocketMQTemplate;
public static void main(String[] args) {
SpringApplication.run(MqProducerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
rocketMQTemplate.convertAndSend("test-topic-2", new OrderPaidEvent("T_001", new BigDecimal("88.00")));
// rocketMQTemplate.destroy();
// notes: once rocketMQTemplate be destroyed, you can not send any message again with this rocketMQTemplate
}
// 在发送客户端发送事务性消息并且实现回查Listener
@RocketMQTransactionListener(txProducerGroup="test")
class TransactionListenerImpl implements RocketMQLocalTransactionListener {
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
// ... local transaction process, return bollback, commit or unknown
return RocketMQLocalTransactionState.UNKNOWN;
}
@Override
public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
// ... check transaction status and return bollback, commit or unknown
return RocketMQLocalTransactionState.COMMIT;
}
}
}
5. 消费者代码编写:
@SpringBootApplication
public class MqConsumerApplication {
private Logger logger = LoggerFactory.getLogger(MqConsumerApplication.class);
public static void main(String[] args) {
SpringApplication.run(MqConsumerApplication.class, args);
}
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public class MyConsumer1 implements RocketMQListener<String> {
public void onMessage(String message) {
logger.info("received message: {}", message);
}
}
@Service
@RocketMQMessageListener(topic = "test-topic-2", consumerGroup = "my-consumer_test-topic-2")
public class MyConsumer2 implements RocketMQListener<OrderPaidEvent>{
public void onMessage(OrderPaidEvent orderPaidEvent) {
logger.info("received orderPaidEvent: {}", orderPaidEvent);
}
}
}
6.先运行消费者,然后运行生产者,在消费者端就可以看到控制台消息的打印。(运行前先部署第0步:RocketMQ的环境部署)