[fix]修改生产环境配置,修改消费者配置
This commit is contained in:
parent
fd5f3fc527
commit
895421fa8b
@ -140,7 +140,7 @@ rocketmq:
|
|||||||
# 生产者配置
|
# 生产者配置
|
||||||
producer:
|
producer:
|
||||||
# 生产者组名
|
# 生产者组名
|
||||||
group: wzj_test
|
group: wzj_prod
|
||||||
# 发送消息超时时间
|
# 发送消息超时时间
|
||||||
send-message-timeout: 30000
|
send-message-timeout: 30000
|
||||||
# 消息最大长度
|
# 消息最大长度
|
||||||
@ -154,7 +154,7 @@ rocketmq:
|
|||||||
# 拉取消息最大数量
|
# 拉取消息最大数量
|
||||||
pull-batch-size: 10
|
pull-batch-size: 10
|
||||||
# 消费者组 (系统模块)
|
# 消费者组 (系统模块)
|
||||||
group: wzj_test
|
group: wzj_prod
|
||||||
# 是否启动消费者
|
# 是否启动消费者
|
||||||
enabled: true
|
enabled: true
|
||||||
--- # mail 邮件发送
|
--- # mail 邮件发送
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package org.dromara.common.mq.utils;
|
package org.dromara.common.mq.utils;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
|
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
|
||||||
import org.apache.rocketmq.client.consumer.PullResult;
|
import org.apache.rocketmq.client.consumer.PullResult;
|
||||||
import org.apache.rocketmq.client.consumer.PullStatus;
|
import org.apache.rocketmq.client.consumer.PullStatus;
|
||||||
@ -17,23 +18,24 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.baomidou.mybatisplus.extension.ddl.DdlScriptErrorHandler.PrintlnLogErrorHandler.log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 带超时自动销毁功能的用户级RocketMQ消费者管理器
|
* 带超时自动销毁功能的用户级RocketMQ消费者管理器
|
||||||
* 支持按用户隔离消费者,并自动清理超时未活动的实例
|
* 支持按用户隔离消费者,并自动清理超时未活动的实例
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
|
@Slf4j
|
||||||
public class UserRocketMQConsumerManager {
|
public class UserRocketMQConsumerManager {
|
||||||
// 全局NameServer地址
|
// 全局NameServer地址
|
||||||
private String namesrvAddr="192.168.1.65:9876";
|
@Value("${rocketmq.name-server}")
|
||||||
|
private String namesrvAddr;
|
||||||
|
|
||||||
// 超时时间(毫秒),默认30分钟
|
// 超时时间(毫秒),默认30分钟
|
||||||
private long timeoutMillis = 30 * 60 * 1000;
|
private long timeoutMillis = 30 * 60 * 1000;
|
||||||
@ -101,36 +103,44 @@ public class UserRocketMQConsumerManager {
|
|||||||
* @param batchSize 每次拉取数量
|
* @param batchSize 每次拉取数量
|
||||||
* @return 拉取到的消息列表
|
* @return 拉取到的消息列表
|
||||||
*/
|
*/
|
||||||
public List<MessageExt> pullUserMessages(String userId , int batchSize)
|
public List<MessageExt> pullUserMessages(String userId , int batchSize){
|
||||||
throws MQClientException, InterruptedException, MQBrokerException, RemotingException {
|
|
||||||
validateUserId(userId);
|
|
||||||
UserConsumerContext context = getUserConsumerContext(userId);
|
|
||||||
|
|
||||||
// 更新最后活动时间
|
|
||||||
context.lastActiveTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
// 获取用户的消息队列
|
|
||||||
Set<MessageQueue> mqs = context.consumer.fetchSubscribeMessageQueues(context.topic);
|
|
||||||
List<MessageExt> allMessages = new ArrayList<>();
|
List<MessageExt> allMessages = new ArrayList<>();
|
||||||
|
|
||||||
int current=0;
|
try{
|
||||||
// 拉取所有队列的消息
|
validateUserId(userId);
|
||||||
for (MessageQueue mq : mqs) {
|
UserConsumerContext context = getUserConsumerContext(userId);
|
||||||
PullResult result = pullSingleQueueMessage(userId, context.consumer, mq , batchSize-current);
|
|
||||||
if (result.getPullStatus() == PullStatus.FOUND) {
|
|
||||||
allMessages.addAll(result.getMsgFoundList());
|
|
||||||
// 更新用户的偏移量
|
|
||||||
updateUserOffset(userId, mq, result.getNextBeginOffset());
|
|
||||||
redisCache.zSetAdd(GlobalConstants.GLOBAL_OFFSET+userId,mq.getQueueId()+"",result.getNextBeginOffset());
|
|
||||||
current+=result.getMsgFoundList().size();
|
|
||||||
if(current==batchSize){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 更新最后活动时间
|
||||||
|
context.lastActiveTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
// 获取用户的消息队列
|
||||||
|
Set<MessageQueue> mqs = context.consumer.fetchSubscribeMessageQueues(context.topic);
|
||||||
|
|
||||||
|
int current=0;
|
||||||
|
// 拉取所有队列的消息
|
||||||
|
for (MessageQueue mq : mqs) {
|
||||||
|
PullResult result = pullSingleQueueMessage(userId, context.consumer, mq , batchSize-current);
|
||||||
|
if (result.getPullStatus() == PullStatus.FOUND) {
|
||||||
|
allMessages.addAll(result.getMsgFoundList());
|
||||||
|
// 更新用户的偏移量
|
||||||
|
updateUserOffset(userId, mq, result.getNextBeginOffset());
|
||||||
|
redisCache.zSetAdd(GlobalConstants.GLOBAL_OFFSET+userId,mq.getQueueId()+"",result.getNextBeginOffset());
|
||||||
|
current+=result.getMsgFoundList().size();
|
||||||
|
if(current==batchSize){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return allMessages;
|
||||||
|
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error("用户[{}]拉取消息异常{}",userId,e.getMessage());
|
||||||
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return allMessages;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.wzj.soopin.content.service.impl;
|
package com.wzj.soopin.content.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||||
import com.tencentcloudapi.vod.v20180717.VodClient;
|
import com.tencentcloudapi.vod.v20180717.VodClient;
|
||||||
@ -56,13 +57,12 @@ public class VlogPullServiceImpl implements IVlogPullService {
|
|||||||
messageExts = mqConsumerManager.pullUserMessages(loginUser.getUserId() + "", (int) page.getSize());
|
messageExts = mqConsumerManager.pullUserMessages(loginUser.getUserId() + "", (int) page.getSize());
|
||||||
|
|
||||||
}
|
}
|
||||||
List<String> ids = messageExts.stream().map(messageExt -> {
|
if(CollectionUtil.isNotEmpty(messageExts)){
|
||||||
MQMessage mqMessage = JsonUtils.parseObject(messageExt.getBody(), MQMessage.class);
|
List<String> ids = messageExts.stream().map(messageExt -> {
|
||||||
return mqMessage.getData().toString();
|
MQMessage mqMessage = JsonUtils.parseObject(messageExt.getBody(), MQMessage.class);
|
||||||
}).collect(Collectors.toList());
|
return mqMessage.getData().toString();
|
||||||
VlogBO vlogBO = new VlogBO();
|
}).collect(Collectors.toList());
|
||||||
if (ids.size() > 0) {
|
VlogBO vlogBO = new VlogBO();
|
||||||
|
|
||||||
vlogBO.setIds(ids);
|
vlogBO.setIds(ids);
|
||||||
List<IndexVlogVO> vlogVOList = vlogService.getIndexVlogList(vlogBO);
|
List<IndexVlogVO> vlogVOList = vlogService.getIndexVlogList(vlogBO);
|
||||||
return page.setRecords(vlogVOList);
|
return page.setRecords(vlogVOList);
|
||||||
@ -75,7 +75,7 @@ public class VlogPullServiceImpl implements IVlogPullService {
|
|||||||
}
|
}
|
||||||
//发出事件
|
//发出事件
|
||||||
//先临时取10条数据
|
//先临时取10条数据
|
||||||
Page<IndexVlogVO> indexVlogVOPage = vlogService.getIndexVlogList(vlogBO, page);
|
Page<IndexVlogVO> indexVlogVOPage = vlogService.getIndexVlogList(null, page);
|
||||||
//直接获取数据库数据,并要求push类推送数据
|
//直接获取数据库数据,并要求push类推送数据
|
||||||
return indexVlogVOPage;
|
return indexVlogVOPage;
|
||||||
}
|
}
|
||||||
|
10
script/docker/rocketmq/prod/broker.conf
Normal file
10
script/docker/rocketmq/prod/broker.conf
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
brokerClusterName = DefaultCluster
|
||||||
|
brokerName = broker-a
|
||||||
|
brokerId = 0
|
||||||
|
# 单机模式设为异步主节点,满足基础消息收发需求
|
||||||
|
brokerRole = ASYNC_MASTER
|
||||||
|
# 异步刷盘,平衡性能与可靠性(单机场景适用)
|
||||||
|
flushDiskType = ASYNC_FLUSH
|
||||||
|
# 关键配置:外部客户端(如Java程序)访问Broker的IP,必须是宿主机可被外部访问的IP
|
||||||
|
brokerIP1 = 43.143.227.203
|
||||||
|
autoCreateNamespaceEnable = true
|
Loading…
x
Reference in New Issue
Block a user