修改计数
This commit is contained in:
@@ -17,6 +17,8 @@ import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -63,23 +65,30 @@ public class AnalysisDifyMqConsumer implements RocketMQListener<MessageExt> {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Override
|
||||
public void onMessage(MessageExt messageExt) {
|
||||
log.info("analysisDifyMqConsumer 当前线程: {}, 线程ID: {}", Thread.currentThread().getName(), Thread.currentThread().getId());
|
||||
long startTime = System.currentTimeMillis();
|
||||
log.info("redis计数数量: " + redisCounterRateLimiter.getCurrentCount(ConstantStr.DIFY_COUNTERRATELIMIT));
|
||||
boolean flag = redisCounterRateLimiter.incrementAndCheck(ConstantStr.DIFY_COUNTERRATELIMIT, difyLimit, expire);
|
||||
if(!flag){
|
||||
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
|
||||
Long count = zSetOperations.zCard(ConstantStr.DIFY_COUNTERRATELIMIT);
|
||||
log.info("redis计数数量: " + count);
|
||||
if(count>=difyLimit){
|
||||
log.info("analysisDifyMqConsumer 请求dify超过基数 :{},稍后请求: " + redisCounterRateLimiter.getCurrentCount(ConstantStr.DIFY_COUNTERRATELIMIT));
|
||||
throw new RuntimeException("请求dify超过基数 " );
|
||||
}
|
||||
String message = new String(messageExt.getBody());
|
||||
log.info("analysisDifyMqConsumer message: " + message);
|
||||
DiFyReq difyReq = JSONObject.parseObject(message, DiFyReq.class);
|
||||
JSONObject difyRequest = JSONObject.parseObject(JSONObject.toJSONString(difyReq.getInputs()), JSONObject.class);
|
||||
String aiAnalysisRequestId = difyRequest.getString("aiAnalysisRequestId");
|
||||
|
||||
zSetOperations.add(ConstantStr.DIFY_COUNTERRATELIMIT, aiAnalysisRequestId, startTime);
|
||||
zSetOperations.removeRangeByScore(ConstantStr.DIFY_COUNTERRATELIMIT, 0, startTime - (expire * 1000));
|
||||
try {
|
||||
String message = new String(messageExt.getBody());
|
||||
log.info("analysisDifyMqConsumer message: " + message);
|
||||
DiFyReq difyReq = JSONObject.parseObject(message, DiFyReq.class);
|
||||
CompletableFuture<JSONObject> future = diFyService.asyncExecuteDifyFlow(difyReq);
|
||||
JSONObject difyRequest = JSONObject.parseObject(JSONObject.toJSONString(difyReq.getInputs()), JSONObject.class);
|
||||
String aiAnalysisRequestId = difyRequest.getString("aiAnalysisRequestId");
|
||||
CompletableFuture<JSONObject> future = diFyService.asyncExecuteDifyFlow(difyReq);
|
||||
// JSONObject json = future.get();
|
||||
future.thenAccept(result -> {
|
||||
log.info("异步处理asyncExecuteDifyFlow完成aiAnalysisRequestId: {},处理结果:{}", aiAnalysisRequestId, result);
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -60,14 +61,18 @@ public class AiAnalysisDifyServiceImpl implements AiAnalysisDifyService {
|
||||
|
||||
@Autowired
|
||||
private RedisCounterRateLimiter redisCounterRateLimiter;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
@Override
|
||||
public boolean updateAiDifyResult(String message) {
|
||||
|
||||
// 计数-1
|
||||
redisCounterRateLimiter.decrement(ConstantStr.DIFY_COUNTERRATELIMIT);
|
||||
log.info("redisdecrement计数数量: " + redisCounterRateLimiter.getCurrentCount(ConstantStr.DIFY_COUNTERRATELIMIT));
|
||||
|
||||
if(StringUtils.isNotEmpty(message)){
|
||||
AnalysisDifyResultDTO analysisResp = JSONObject.parseObject(message, AnalysisDifyResultDTO.class);
|
||||
// 计数-1
|
||||
Long removedCount = redisTemplate.opsForZSet().remove(ConstantStr.DIFY_COUNTERRATELIMIT, analysisResp.getAiAnalysisRequestId());
|
||||
log.info("redisdecrement计数数量: " + removedCount);
|
||||
|
||||
AiAnalysisRequestLogs oldAiAnalysisRequestLogs = Optional.ofNullable(aiAnalysisRequestLogsService.queryByAiAnalysisRequestId(analysisResp.getAiAnalysisRequestId()))
|
||||
.orElseThrow(() -> new IllegalArgumentException("AiAnalysisRequestId查询对象为空!"));
|
||||
|
||||
Reference in New Issue
Block a user