稳定版。没重构之前。稳定的限流。
This commit is contained in:
@@ -2,10 +2,11 @@ package cn.van.business.mq;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.van.business.util.WxtsUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.rocketmq.spring.annotation.ConsumeMode;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.slf4j.Logger;
|
||||
@@ -13,10 +14,8 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static cn.hutool.core.thread.ThreadUtil.sleep;
|
||||
import static cn.van.business.util.WXUtil.WX_BASE_URL;
|
||||
|
||||
/**
|
||||
@@ -26,14 +25,17 @@ import static cn.van.business.util.WXUtil.WX_BASE_URL;
|
||||
* @description:
|
||||
*/
|
||||
@Service
|
||||
@RocketMQMessageListener(topic = "wx-message", consumerGroup = "${rocketmq.consumer.group}", nameServer = "${rocketmq.name-server}")
|
||||
@RocketMQMessageListener(topic = "wx-message", consumerGroup = "${rocketmq.consumer.group}", nameServer = "${rocketmq.name-server}", consumeMode = ConsumeMode.ORDERLY // 顺序消费(单线程)
|
||||
)
|
||||
public class MessageConsumerService implements RocketMQListener<JSONObject> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageConsumerService.class);
|
||||
private static final RateLimiter rateLimiter = RateLimiter.create(0.5, // 1 QPS
|
||||
5, // 预热期 5 秒
|
||||
TimeUnit.SECONDS);
|
||||
|
||||
private final WxtsUtil wxtsUtil;
|
||||
// create a rate limiter of 1 qps
|
||||
RateLimiter rateLimiter = RateLimiter.create(0.5);
|
||||
|
||||
|
||||
@Autowired
|
||||
public MessageConsumerService(WxtsUtil wxtsUtil) {
|
||||
@@ -41,23 +43,23 @@ public class MessageConsumerService implements RocketMQListener<JSONObject> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(JSONObject jsonObject) {
|
||||
// 处理消息
|
||||
public void onMessage(JSONObject message) {
|
||||
try {
|
||||
rateLimiter.acquire(); // 请求许可。如果超过速率,则此方法会阻塞
|
||||
String body = jsonObject.getString("body");
|
||||
byte[] decodedBody = Base64.getDecoder().decode(body);
|
||||
String decodedBodyStr = new String(decodedBody, StandardCharsets.UTF_8);
|
||||
JSONObject decodedBodyJson = JSONObject.parseObject(decodedBodyStr);
|
||||
String jsonStr = JSONUtil.toJsonStr(decodedBodyJson);
|
||||
String responseStr = HttpRequest.post(WX_BASE_URL)
|
||||
.body(jsonStr)
|
||||
.execute()
|
||||
.body();
|
||||
logger.info("消费消息:{}", jsonStr);
|
||||
//logger.info("[RateLimiter] 开始处理消息,当前时间:{}", System.currentTimeMillis());
|
||||
rateLimiter.acquire();
|
||||
//logger.info("[RateLimiter] 获得令牌,当前时间:{}", System.currentTimeMillis());
|
||||
|
||||
|
||||
//logger.debug("构造完成的消息结构:{}", requestBody.toJSONString());
|
||||
|
||||
// 4. 发送请求(保持原有)
|
||||
String responseStr;
|
||||
responseStr = HttpRequest.post(WX_BASE_URL).body(message.toJSONString()).execute().body();
|
||||
|
||||
// ... [保持原有响应处理逻辑]
|
||||
if (ObjectUtil.isNotEmpty(responseStr)) {
|
||||
JSONObject response = JSONObject.parseObject(responseStr);
|
||||
logger.info("消息成功发送并得到响应:{}", response);
|
||||
//logger.info("消息成功发送并得到响应:{}", response);
|
||||
if (response.getInteger("code") != 200) {
|
||||
// TODO: 如果需要处理错误,您可以在这里添加逻辑
|
||||
wxtsUtil.sendNotify("消息发送失败: " + responseStr);
|
||||
@@ -69,9 +71,10 @@ public class MessageConsumerService implements RocketMQListener<JSONObject> {
|
||||
throw new RuntimeException("消息发送失败,没有收到响应");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//logger.error("处理消息时发生错误", e);
|
||||
throw e; // 重抛异常使得 RocketMQ 可以捕获到这个异常
|
||||
logger.error("消息处理失败,原始消息:{}", message, e);
|
||||
wxtsUtil.sendNotify("系统异常:" + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user