一、Docker安装
1.创建docker-compose.yml文件
version: '3.5'
services:
rmqnamesrv:
image: foxiswho/rocketmq:server
container_name: rmqnamesrv
ports:
- 9876:9876
networks:
rmq:
aliases:
- rmqnamesrv
rmqbroker:
image: foxiswho/rocketmq:broker
container_name: rmqbroker
ports:
- 10909:10909
- 10911:10911
volumes:
- ./broker.conf:/etc/rocketmq/broker.conf
environment:
NAMESRV_ADDR: "rmqnamesrv:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
command: mqbroker -c /etc/rocketmq/broker.conf
depends_on:
- rmqnamesrv
networks:
rmq:
aliases:
- rmqbroker
rmqconsole:
image: styletang/rocketmq-console-ng
container_name: rmqconsole
ports:
- 8090:8080
environment:
JAVA_OPTS: "-Drocketmq.namesrv.addr=rmqnamesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false"
depends_on:
- rmqnamesrv
networks:
rmq:
aliases:
- rmqconsole
networks:
rmq:
name: rmq
driver: bridge
2.新建broker.conf文件
# 集群名称
brokerClusterName = DefaultCluster
# 节点名称
brokerName = broker-a
# broker id节点ID, 0 表示 master, 其他的正整数表示 slave,不能小于0
brokerId = 0
# 在每天的什么时间删除已经超过文件保留时间的 commit log,默认值04
deleteWhen = 04
# 以小时计算的文件保留时间 默认值72小时
fileReservedTime = 72
# Broker角色
brokerRole = ASYNC_MASTER
# 刷盘方式
flushDiskType = ASYNC_FLUSH
# Broker服务地址,内部使用填内网ip,如果是需要给外部使用填公网ip,自行更改
brokerIP1 = 192.168.11.99
3. 执行docker-compose up -d
即可成功启动
4.访问控制台
二、SpringBoot集成RockerMQ
1.引入依赖
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2.配置服务器地址
#Rocketmq配置
rocketmq.name-server=192.168.11.99:9876
# 必须指定生产者组
rocketmq.producer.group=group01
# 消息发送超时时长,默认3s
rocketmq.producer.send-message-timeout=3000
# 同步发送消息失败重试次数,默认2
rocketmq.producer.retry-times-when-send-failed=3
# 异步发送消息失败重试次数,默认2
rocketmq.producer.retry-times-when-send-async-failed=3
2.创建生产者
@Component
public class Producer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void sendMessage(String topic, String msg) {
Message<String> msg = MessageBuilder.withPayload(msg).build();
rocketMQTemplate.send("topic_01",msg);
}
}
3.创建消费者
@Component
@RocketMQMessageListener(topic = "topic_01", consumerGroup = "group_205")
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
}
三、rocketMQ的延迟消息和死信队列
延迟消息
RocketMQ支持指定级别的延迟消息,即只能设置预设的几个时间等级的延迟,而不是任意时间延迟。目前RocketMQ社区版并不支持任意时间的精确延迟,RocketMQ在4.x版本只能够支持18种内置的延迟消息(1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h),具体实现方式是在发送消息时设置消息的延迟等级。
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void sendMessage(String topic, String message) {
Message<String> msg = MessageBuilder.withPayload(message).build();
rocketMQTemplate.syncSend(topic,msg,3000,1);
log.info("发送成功");
}
死信队列
@Service
@Slf4j
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "topic_01")
public class Consumer implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
@Override
public void onMessage(String message) {
System.out.println("Received message: " + message);
log.info("Received message: " + message);;
throw new RuntimeException("test");
}
@Override
public void prepareStart(DefaultMQPushConsumer consumer) {
// 设置最大重试次数
consumer.setMaxReconsumeTimes(2);
// 如下,设置其它consumer相关属性
consumer.setPullBatchSize(16);
}
}