Springboot 项目application.properties的基础配置
server.port=8091 # RocketMQ NameServer 地址 rocketmq.name-server=公网IP:9876 # 生产者组名称 rocketmq.producer.group=my-rocket-mq-group rocketmq.producer.access-key=rocketmq2 rocketmq.producer.secret-key=12345678 rocketmq.producer.send-message-timeout=30000 # 消费者订阅的主题 rocketmq.consumer.topic=my-rocket-mq-test-topic rocketmq.consumer.access-key=rocketmq2 rocketmq.consumer.secret-key=12345678 # 消费者最小线程数 rocketmq.consumer.consume-thread-min=1 # 消费者最大线程数 rocketmq.consumer.consume-thread-max=20 # 消息最大重试次数 rocketmq.consumer.max-reconsume-times=3
Springboot关于生产者的代码
@Service public class MyProducer { @Autowired private RocketMQTemplate rocketMQTemplate; public void send(String topic, String message) { rocketMQTemplate.convertAndSend(topic, message); } }
Springboot关于消费者的代码
@Service @RocketMQMessageListener(topic = "my-rocket-mq-test-topic", consumerGroup = "my-consumer-group") public class MyConsumer implements RocketMQListener<String> { @Override public void onMessage(String s) { System.out.println(s); } }
需要引入的依赖
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring.boot>2.7.18</spring.boot> <rocketmq.version>2.2.0</rocketmq.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot}</version> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>${rocketmq.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring.boot}</version> </dependency> </dependencies>
RocketMQ的broker.conf中基础配置
brokerClusterName = DefaultCluster brokerName = broker-a brokerId = 0 deleteWhen = 04 fileReservedTime = 48 brokerRole = ASYNC_MASTER flushDiskType = ASYNC_FLUSH namesrvAddr = 公网IP:9876 brokerIP1 = 公网IP
RocketMQ的plain_acl.yml中基础配置
globalWhiteRemoteAddresses: - 10.10.103.* - 192.168.0.* accounts: - accessKey: RocketMQ secretKey: 12345678 whiteRemoteAddress: admin: false defaultTopicPerm: DENY defaultGroupPerm: SUB topicPerms: - topicA=DENY - topicB=PUB|SUB - topicC=SUB groupPerms: # the group should convert to retry topic - groupA=DENY - groupB=PUB|SUB - groupC=SUB - accessKey: rocketmq2 secretKey: 12345678 whiteRemoteAddress: 192.168.1.* # if it is admin, it could access all resources admin: true
备注:
如果项目启动出现RemotingTooMuchRequestException: sendDefaultImpl call timeout的异常
你需要考虑关闭防火墙,或者放开对端口10911和9876两个端口的拦截
标签:SUB,--,spring,配置,boot,topic,consumer,rocketmq From: https://www.cnblogs.com/xylfjk/p/17981886