Files
2023-08-10 19:20:56 +08:00

69 lines
3.2 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#### 快速使用
##### pom引入
```
<dependency>
<groupId>com.njcn</groupId>
<artifactId>rocket-mq-springboot-starter</artifactId>
<version>1.0.0</version>
</dependency>
```
##### 使用问题:
* WARN No appenders could be found for logger启动项目时会在日志中看到如下告警
```
RocketMQLog:WARN No appenders could be found for logger(io.netty.util.internal.InternalThreadLocalMap).
RocketMQLog:WARN Please initialize the logger system properly.
```
此时我们只需要在启动类中设置环境变量 [rocketmq.client.logUseSlf4j](http://rocketmq.client.loguseslf4j/) 为 true 明确指定RocketMQ的日志框架
```java
@SpringBootApplication
public class RocketDemoApplication {
public static void main(String[] args) {
/*
* 指定使用的日志框架,否则将会告警
* RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.InternalThreadLocalMap).
* RocketMQLog:WARN Please initialize the logger system properly.
*/
System.setProperty("rocketmq.client.logUseSlf4j", "true");
SpringApplication.run(RocketDemoApplication.class, args);
}
}
```
同时还得在配置文件中调整日志级别不然在控制台会一直看到broker的日志信息
```yml
logging:
level:
RocketmqClient: ERROR
io:
netty: ERROR
```
#### 1、RocketMQ官方sdk中存在的问题
##### 1.1、不支持LocalDate 和 LocalDateTime
原因RocketMQ内置使用的转换器是**RocketMQMessageConverter**转换Json时使用的是MappingJackson2MessageConverter但是这个转换器不支持时间类型。
解决办法需要自定义消息转换器将MappingJackson2MessageConverter进行替换并添加支持时间模块。
##### 1.2、RockeMQ环境隔离
原因在使用RocketMQ时通常会在代码中直接指定消息主题(topic)而且开发环境和测试环境可能共用一个RocketMQ环境。如果没有进行处理在开发环境发送的消息就可能被测试环境的消费者消费测试环境发送的消息也可能被开发环境的消费者消费从而导致数据混乱的问题。
解决方法我们可以根据不同的环境实现自动隔离。通过简单配置一个选项如dev、test、prod等不同环境所有的消息都会被自动隔离。例如当发送的消息主题为consumer_topic时可以自动在topic后面加上环境后缀如consumer_topic_dev。
详细查看EnvironmentIsolationConfig 配置。
##### 1.3、避免大量的重复代码
一个完整的消息传递链路:从生产者到消费者应包括 准备消息、发送消息、记录消息日志、发送失败处理、记录接受消息日志、处理业务逻辑、异常处理和异常重试等步骤。
虽然原生的rocketMQ可以完成这些动作但是每个生产者和消费者都需要编写大量重复的代码来完成相同的任务。最终的目的是让开发人员只需要准备好消息实体并用封装后的工具类发送而消费者只需处理核心业务逻辑其他公共逻辑会得到统一处理。