# spring-boot-starter-rocketmq
[中文](./README_zh_CN.md)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
Help developers quickly integrate [RocketMQ](http://rocketmq.apache.org/) in [Spring Boot](http://projects.spring.io/spring-boot/). Support the Spring Message specification to facilitate developers to quickly switch from other MQ to RocketMQ.
Features:
- [x] synchronous transmission
- [x] synchronous ordered transmission
- [x] asynchronous transmission
- [x] asynchronous ordered transmission
- [x] orderly consume
- [x] concurrently consume(broadcasting/clustering)
- [x] One-way transmission
- [ ] transaction transmission
- [ ] Pull consume
## Quick Start
Here are some key points listed, the complete example, please refer to: [rocketmq-demo](https://github.com/aqlu/rocketmq-demo)
```xml
<!--add dependency in pom.xml-->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>spring-boot-starter-rocketmq</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
```
### Produce Message
```properties
## application.properties
spring.rocketmq.name-server=127.0.0.1:9876
spring.rocketmq.producer.group=my-group
```
> Note:
>
> Maybe you need change `127.0.0.1:9876` with your real NameServer address for RocketMQ
```java
@SpringBootApplication
public class ProducerApplication implements CommandLineRunner{
@Resource
private RocketMQTemplate rocketMQTemplate;
public static void main(String[] args){
SpringApplication.run(ProducerApplication.class, args);
}
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
}
@Data
@AllArgsConstructor
public class OrderPaidEvent implements Serializable{
private String orderId;
private BigDecimal paidMoney;
}
}
```
> More relevant configurations for produce:
>
> ```properties
> spring.rocketmq.producer.retry-times-when-send-async-failed=0
> spring.rocketmq.producer.send-msg-timeout=300000
> spring.rocketmq.producer.compress-msg-body-over-howmuch=4096
> spring.rocketmq.producer.max-message-size=4194304
> spring.rocketmq.producer.retry-another-broker-when-not-store-ok=false
> spring.rocketmq.producer.retry-times-when-send-failed=2
> ```
### Consume Message
```properties
## application.properties
spring.rocketmq.name-server=127.0.0.1:9876
```
> Note:
>
> Maybe you need change `127.0.0.1:9876` with your real NameServer address for RocketMQ
```java
@SpringBootApplication
public class ConsumerApplication{
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class, args);
}
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public class MyConsumer1 implements RocketMQListener<String>{
public void onMessage(String message) {
log.info("received message: {}", message);
}
}
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-2", consumerGroup = "my-consumer_test-topic-2")
public class MyConsumer2 implements RocketMQListener<OrderPaidEvent>{
public void onMessage(OrderPaidEvent orderPaidEvent) {
log.info("received orderPaidEvent: {}", orderPaidEvent);
}
}
}
```
> More relevant configurations for consume:
>
> see: [RocketMQMessageListener](src/main/java/org/apache/rocketmq/spring/starter/annotation/RocketMQMessageListener.java)
## FAQ
1. How to connected many `nameserver` on production environment?
`spring.rocketmq.name-server` support the configuration of multiple `nameserver`, separated by `;`. For example: `172.19.0.1: 9876; 172.19.0.2: 9876`
1. When was `rocketMQTemplate` destroyed?
Developers do not need to manually execute the `rocketMQTemplate.destroy ()` method when using `rocketMQTemplate` to send a message in the project, and` rocketMQTemplate` will be destroyed automatically when the spring container is destroyed.
1. start exception:`Caused by: org.apache.rocketmq.client.exception.MQClientException: The consumer group[xxx] has been created before, specify another name please`
RocketMQ in the design do not want a consumer to deal with multiple types of messages at the same time, so the same `consumerGroup` consumer responsibility should be the same, do not do different things (that is, consumption of multiple topics). Suggested `consumerGroup` and` topic` one correspondence.
1. How is the message content body being serialized and deserialized?
RocketMQ's message body is stored as `byte []`. When the business system message content body if it is `java.lang.String` type, unified in accordance with` utf-8` code into `byte []`; If the business system message content is not `java.lang.String` Type, then use [jackson-databind](https://github.com/FasterXML/jackson-databind) serialized into the `JSON` format string, and then unified in accordance with` utf-8` code into `byte [] `.
1. How do I specify the `tags` for topic?
RocketMQ best practice recommended: an application as much as possible with one Topic, the message sub-type with `tags` to identify,` tags` can be set by the application free.
When you use `rocketMQTemplate` to send a message, set the destination of the message by setting the` destination` parameter of the send method. The `destination` format is `topicName:tagName`, `:` Precedes the name of the topic, followed by the `tags` name.
> Note:
>
> `tags` looks a complex, but when sending a message , the destination can only specify one topic under a `tag`, can not specify multiple.
1. How do I set the message's `key` when sending a message?
You can send a message by overloading method like `xxxSend(String destination, Message<?> msg, ...)`, setting `headers` of `msg`. for example:
```java
Message<?> message = MessageBuilder.withPayload(payload).setHeader(MessageConst.PROPERTY_KEYS, msgId).build();
rocketMQTemplate.send("topic-test", message);
```
Similarly, you can also set the message `FLAG`,` WAIT_STORE_MSG_OK` and some other user-defined other header information according to the above method.
> Note:
>
> In the case of converting Spring's Message to RocketMQ's Message, to prevent the `header` information from conflicting with RocketMQ's system properties, the prefix `USERS_` was added in front of all `header` names. So if you want to get a custom message header when consuming, please pass through the key at the beginning of `USERS_` in the header.
1. When consume message, in addition to get the message `payload`, but also want to get RocketMQ message of other system attributes, how to do?
Consumers in the realization of `RocketMQListener` interface, only need to be generic for the` MessageExt` can, so in the `onMessage` method will receive RocketMQ native 'MessageExt` message.
```java
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public class MyConsumer2 implements RocketMQListener<MessageExt>{
public void onMessage(MessageExt messageExt) {
log.info("received messageExt: {}", messageExt);
}
}
```
1. How do I specify where consumers start consuming messages?
The default consume offset please refer: [RocketMQ FAQ](http://rocketmq.apache.org/docs/faq/).
To customize the consumer's
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
RocketmqStarter.zip (55个子文件)
RocketmqStarter
.project 1KB
.springBeans 417B
.apt_generated
src
test
java
org
apache
rocketmq
spring
starter
RocketMQAutoConfigurationTests.java 9KB
main
resources
META-INF
spring.factories 125B
java
org
apache
rocketmq
spring
starter
annotation
RocketMQMessageListener.java 3KB
core
DefaultRocketMQListenerContainerConstants.java 2KB
RocketMQTemplate.java 24KB
RocketMQConsumerLifecycleListener.java 952B
DefaultRocketMQListenerContainer.java 10KB
RocketMQListener.java 925B
RocketMQListenerContainer.java 1KB
RocketMQPushConsumerLifecycleListener.java 1KB
enums
ConsumeMode.java 1KB
SelectorType.java 1KB
RocketMQProperties.java 3KB
RocketMQAutoConfiguration.java 10KB
target
generated-test-sources
test-annotations
maven-status
maven-compiler-plugin
testCompile
default-testCompile
createdFiles.lst 155B
inputFiles.lst 130B
compile
default-compile
createdFiles.lst 1KB
inputFiles.lst 2KB
generated-sources
annotations
classes
META-INF
MANIFEST.MF 109B
maven
org.apache.rocketmq
spring-boot-starter-sixmonth-rocketmq
pom.properties 282B
pom.xml 7KB
spring.factories 125B
spring-configuration-metadata.json 3KB
org
apache
rocketmq
spring
starter
RocketMQAutoConfiguration$ListenerContainerConfiguration.class 8KB
annotation
RocketMQMessageListener.class 1KB
core
DefaultRocketMQListenerContainer$DefaultMessageListenerOrderly.class 3KB
RocketMQConsumerLifecycleListener.class 314B
DefaultRocketMQListenerContainer$DefaultMessageListenerConcurrently.class 4KB
RocketMQListenerContainer.class 409B
DefaultRocketMQListenerContainerConstants.class 1KB
RocketMQListener.class 277B
DefaultRocketMQListenerContainer.class 12KB
RocketMQPushConsumerLifecycleListener.class 453B
RocketMQTemplate.class 19KB
RocketMQAutoConfiguration.class 4KB
RocketMQProperties$Producer.class 4KB
enums
ConsumeMode.class 1KB
SelectorType.class 1KB
RocketMQProperties.class 2KB
maven-archiver
pom.properties 155B
archive-tmp
.travis.yml 163B
.factorypath 8KB
.settings
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 794B
org.eclipse.jdt.apt.core.prefs 68B
org.eclipse.core.resources.prefs 155B
LICENSE 11KB
README.md 9KB
README_zh_CN.md 8KB
pom.xml 7KB
.classpath 1KB
.gitignore 79B
共 55 条
- 1
资源评论
尘光掠影
- 粉丝: 385
- 资源: 54
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 采用P-f和Q-V滞控的去中心化逆变器型交流微电网的模拟(Simulink仿真实现)
- 彩虹聚合二级域名DNS管理系统源码v1.3
- 【TOF相机笔记3】Simulink使用方法
- 算法部署-基于C++和Python使用ONNXRuntime部署RT-DETR目标检测算法-附项目源码-优质项目实战.zip
- Bitree.cpp
- 改变浏览器大小,图片(img)内容居中显示
- 全景分割-基于FAIR-DETR对Cityscapes数据集进行微调实现全景分割-附项目源码-优质项目实战.zip
- Tru master.m4a
- 基于ELMAN神经网络的用气量预测,基于ELMAN的天然气消费量预测(代码完整,数据齐全)
- 基于Vue3+ThreeJS实现机械臂控制和预览+源码+开发文档+代码解析(高分优秀项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功