Github:https://github.com/yihonglei/daisy-framework
自定义rocketmq的starter,使用rocketmq时直接引入jar包使用即可。
annotation:生产者和消费者注解;
configuration:RocketMQ基础配置;
lifecycle:主要管理生产者和消费者对象生命周期;
producer:生产者,含常用方法;
consumer:消费者,含常用消费方式;
resources/META-INF/springl.factories:spi机制,spring boot自动装配机制EnableAutoConfiguration;
先clean install打包,引入pom.xml,在rose-framework中,统一抽象了rose-parent,所以引入到了rose-parent中。
com.jpeony
daisy-rocketmq-spring-boot-starter
1.0.0
1)application-dev.yml中配置rocketmq,修改为自己的rocketmq地址即可。
# rocketmq
rocketmq:
producer:
testProducer:
namesrvAddr: 10.6.202.110:9876;10.6.202.111:9876
groupName: testProducerGroup
topic: testTopic
instanceName: testProducerInstance
consumer:
testConsumer:
namesrvAddr: 10.6.202.110:9876;10.6.202.111:9876
groupName: testConsumerGroup
topic: testTopic
tag: '*'
batchMaxSize: 16
instanceName: testConsumerInstance
2)生产者和消费者
TestRocketMqProducer
package com.jpeony.core.mq;
import com.jpeony.rocketmq.spring.annotation.RocketMqProducer;
import com.jpeony.rocketmq.spring.producer.AbstractRocketMqProducer;
/**
* 消息发送
* 业务使用注入对象:@Autowired private TestRocketMqProducer producer;
* 调用发送方法:producer.sendMessage*
* topic发送时如果不显示传入,则用配置文件里面的,一般进行显示传入!
*
* @author yihonglei
*/
@RocketMqProducer(
namesrvAddr = "${rocketmq.producer.testProducer.namesrvAddr}",
groupName = "${rocketmq.producer.testProducer.groupName}",
topic = "${rocketmq.producer.testProducer.topic}")
public class TestRocketMqProducer extends AbstractRocketMqProducer {
}
TestRocketMqConsumer
package com.jpeony.core.mq;
import com.jpeony.rocketmq.spring.annotation.RocketMqConsumer;
import com.jpeony.rocketmq.spring.consumer.AbstractRocketMqConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.common.message.MessageExt;
import java.util.List;
/**
* 消息消费者
*
* @author yihonglei
*/
@RocketMqConsumer(
namesrvAddr = "${rocketmq.consumer.testConsumer.namesrvAddr}",
groupName = "${rocketmq.consumer.testConsumer.groupName}",
topic = "${rocketmq.consumer.testConsumer.topic}",
tag = "${rocketmq.consumer.testConsumer.tag}")
public class TestRocketMqConsumer extends AbstractRocketMqConsumer {
@Override
public ConsumeOrderlyStatus handleOrderlyMessage(List msgs, ConsumeOrderlyContext context) {
logger.info("TestRocketConsumer.ConsumeOrderlyStatus.handleOrderlyMessage:{}", context);
return ConsumeOrderlyStatus.SUCCESS;
}
@Override
public ConsumeConcurrentlyStatus handleMessage(List msgs, ConsumeConcurrentlyContext context) {
logger.info("TestRocketConsumer.ConsumeConcurrentlyStatus.handleMessage:{}", context);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
}
3)测试
RocketMqController
package com.jpeony.api.controller;
import com.jpeony.common.util.ApiResponse;
import com.jpeony.core.mq.TestRocketMqProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* RocketMq生产者消费者测试
*
* @author yihonglei
*/
@RestController
@RequestMapping("/rocketmq/test")
public class RocketMqController {
@Autowired
private TestRocketMqProducer producer;
@RequestMapping("/producer")
public ApiResponse producer() {
boolean b = false;
try {
b = producer.sendMessage("testTopic", "*", "This my rocketmq message!");
} catch (Exception e) {
e.printStackTrace();
}
return ApiResponse.success(b);
}
}
ApiApplication
package com.jpeony.api;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import com.jpeony.common.spring.TraceLogInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
import static com.jpeony.common.constant.InterceptorConstant.NGINX;
/**
* 启动类
*
* @author yihonglei
*/
@SpringBootApplication(scanBasePackages = "com.jpeony")
@MapperScan("com.jpeony.core.mapper")
public class ApiApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
/**
* 添加日志追踪拦截器
*
* @author yihonglei
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TraceLogInterceptor()).addPathPatterns("/**").excludePathPatterns(NGINX);
}
/**
* 不使用默认jackson框架解析json,重写configureMessageConverters,使用fastjson框架解析json
*
* @author yihonglei
*/
@Override
public void configureMessageConverters(List> converters) {
converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
List fastMediaTypes = Lists.newArrayList();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastMediaTypes.add(MediaType.APPLICATION_JSON);
fastMediaTypes.add(new MediaType("application", "*+json"));
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastJsonHttpMessageConverter);
}
}
访问controller即可看到消息发送和消费。
http://localhost:8765/rocketmq/test/producer