简约版本RocketMQ消费者:
pom引入依赖:
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.1.6.RELEASEversion> <relativePath/> parent> <groupId>com.hellobikegroupId> <artifactId>rocketmqconsumerartifactId> <version>0.0.1-SNAPSHOTversion> <name>rocketmqconsumername> <description>RocketMqConsumerdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> <scope>runtimescope> <optional>trueoptional> dependency> <dependency> <groupId>org.apache.rocketmqgroupId> <artifactId>rocketmq-clientartifactId> <version>4.3.0version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.6version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <optional>trueoptional> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-configuration-processorartifactId> <optional>trueoptional> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
消费者方法类:
package com.xxx.rocketmqconsumer.consumer; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; import org.apache.rocketmq.client.exception.MQClientException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextRefreshedEvent; @Configuration public class RocketConfiguration implements ApplicationListener{ protected static final Logger logger = LoggerFactory.getLogger(RocketConfiguration.class); private static final String consumerGroup = "basic-test"; private static final String namesrvAddr = "test1-rocketmq.ttbike.com.cn:9876;test2-rocketmq.ttbike.com.cn:9876"; private static final String topic = "TopicTest"; @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup); consumer.setNamesrvAddr(namesrvAddr); try { if (contextRefreshedEvent.getApplicationContext().getParent() == null) { consumer.subscribe(topic, "*"); consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> { return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }); consumer.start(); } } catch (MQClientException mQClientException) { mQClientException.printStackTrace(); } } }
RocketMQ1.0消费者配置文件抽出:
pom.xml引入依赖:
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.1.6.RELEASEversion> <relativePath/> parent> <groupId>com.hellobikegroupId> <artifactId>rocketmqconsumerartifactId> <version>0.0.1-SNAPSHOTversion> <name>rocketmqconsumername> <description>RocketMqConsumerdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> <scope>runtimescope> <optional>trueoptional> dependency> <dependency> <groupId>org.apache.rocketmqgroupId> <artifactId>rocketmq-clientartifactId> <version>4.3.0version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.6version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <optional>trueoptional> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-configuration-processorartifactId> <optional>trueoptional> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
package com.xxx.rocketmqconsumer.consumer; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "rocketmq.comsumer") @Data public class RocketMQComsumerProperties { // nameServer 定义实体 private String nameServer = "localhost:9876"; // Topic 实体定义 private String topic; // 消费者Group定义 private String comsumerGroup; }
增加logback-spring.xml 配置文件
xml version="1.0" encoding="utf-8"?> <configuration> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d - %msg%npattern> layout> appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERRORlevel> <onMatch>DENYonMatch> <onMismatch>ACCEPTonMismatch> filter> <encoder> <pattern>%msg%npattern> encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/workspace/carkey/RocketMqConsumer/info.%d.logfileNamePattern> rollingPolicy> appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERRORlevel> filter> <encoder> <pattern>%msg%npattern> encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/workspace/carkey/RocketMqConsumer/error.%d.logfileNamePattern> rollingPolicy> appender> <root level="info"> <appender-ref ref="consoleLog"/> <appender-ref ref="fileInfoLog"/> <appender-ref ref="fileErrorLog"/> root> configuration>
配置文件配置:
rocketmq.comsumer.nameServer=test1-rocketmq.ttbike.com.cn:9876;test2-rocketmq.ttbike.com.cn:9876 rocketmq.comsumer.topic=TopicTest rocketmq.comsumer.comsumerGroup=basic-test logging.file=app.log logging.level.com.hellobike.rocketmqconsumer.consumer=error