稳定版。没重构之前。稳定的限流。

This commit is contained in:
Leo
2025-03-04 14:29:48 +08:00
parent 72b7a125e0
commit 645b025172
16 changed files with 182 additions and 130 deletions

View File

@@ -6,7 +6,9 @@ import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 日期工具类

View File

@@ -16,11 +16,15 @@ import com.jd.open.api.sdk.request.kplunion.UnionOpenOrderRowQueryRequest;
import com.jd.open.api.sdk.request.kplunion.UnionOpenPromotionBysubunionidGetRequest;
import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse;
import com.jd.open.api.sdk.response.kplunion.UnionOpenPromotionBysubunionidGetResponse;
import io.github.resilience4j.core.functions.CheckedRunnable;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
@@ -85,8 +89,10 @@ public class JDUtil {
// 构造函数中注入StringRedisTemplate
@Autowired
public JDUtil(StringRedisTemplate redisTemplate,
ProductOrderRepository productOrderRepository,OrderRowRepository orderRowRepository, WXUtil wxUtil, OrderUtil orderUtil) {
this.redisTemplate = redisTemplate;
ProductOrderRepository productOrderRepository,
OrderRowRepository orderRowRepository, WXUtil wxUtil,
OrderUtil orderUtil) {
this.redisTemplate = redisTemplate;
this.orderRowRepository = orderRowRepository;
this.productOrderRepository = productOrderRepository;
this.wxUtil = wxUtil;
@@ -122,7 +128,7 @@ private static class UserInteractionState {
/**
* 将 响应参数转化为 OrderRow并返回
*/
private static OrderRow createOrderRow(OrderRowResp orderRowResp) {
private OrderRow createOrderRow(OrderRowResp orderRowResp) {
OrderRow orderRow = new OrderRow();
orderRow.setOrderId(orderRowResp.getOrderId());
orderRow.setSkuId(orderRowResp.getSkuId());
@@ -210,50 +216,47 @@ private static class UserInteractionState {
return todayOrders.stream().filter(orderRow -> orderRow.getValidCode() == 13 || orderRow.getValidCode() == 25 || orderRow.getValidCode() == 26 || orderRow.getValidCode() == 27 || orderRow.getValidCode() == 28 || orderRow.getValidCode() == 29);
}
/**
* 实时刷新最近10分钟的订单
*/
@Scheduled(cron = "0 * * * * ?")
public void fetchLatestOrder() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastMinute = now.minusMinutes(10).withSecond(0).withNano(0);
/**
* 实时刷新最近10分钟的订单Resilience4j限流集成
*/
@Scheduled(cron = "0 * * * * ?")
public void fetchLatestOrder() {
try {
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastMinute = now.minusMinutes(10).withSecond(0).withNano(0);
for (Map.Entry<String, WXUtil.SuperAdmin> entry : super_admins.entrySet()) {
WXUtil.SuperAdmin admin = entry.getValue();
String appKey = admin.getAppKey();
String secretKey = admin.getSecretKey();
/**临时代码*/
if (Util.isAnyEmpty(appKey, secretKey)) {
continue;
}
// 如果当前分钟数刚好是整10就打印
if (now.getMinute() % 10 == 0) {
logger.info("实时订单 {} ", appKey.substring(appKey.length() - 4));
}
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(
lastMinute, true, 1, true, appKey, secretKey
);
if (response != null && response.getQueryResult() != null
&& response.getQueryResult().getCode() == 200) {
OrderRowResp[] orderRowResps = response.getQueryResult().getData();
if (orderRowResps == null) continue;
/**下面是原先的代码*/
for (Map.Entry<String, WXUtil.SuperAdmin> entry : super_admins.entrySet()) {
//String wxid = entry.getKey();
WXUtil.SuperAdmin admin = entry.getValue();
String appKey = admin.getAppKey();
String secretKey = admin.getSecretKey();
if (Util.isAnyEmpty(appKey, secretKey)) {
continue;
}
logger.info("实时订单 {} ", appKey.substring(appKey.length() - 4));
UnionOpenOrderRowQueryResponse response = fetchOrdersForDateTime(lastMinute, true, 1, true, appKey, secretKey); // 真实代表实时订单
if (response != null) {
int code = response.getQueryResult().getCode();
if (code == 200) {
if (response.getQueryResult().getCode() == 200) {
OrderRowResp[] orderRowResps = response.getQueryResult().getData();
if (orderRowResps == null) {
continue;
}
for (OrderRowResp orderRowResp : orderRowResps) {
// 固化到数据库
OrderRow orderRow = createOrderRow(orderRowResp);
// 订单号不存在就保存,存在就更新订单状态
orderRowRepository.save(orderRow);
}
}
Arrays.stream(orderRowResps)
.map(this::createOrderRow)
.forEach(orderRowRepository::save);
}
}
}
} catch (Exception e) {
logger.error("调度任务异常", e);
}
}
public void test01() {
@@ -291,7 +294,7 @@ private static class UserInteractionState {
*/
@Scheduled(cron = "10 * * * * ?")
public void sendOrderToWx() {
long start = System.currentTimeMillis();
//long start = System.currentTimeMillis();
int[] validCodes = {-1};
// 只要三个月的,更多的也刷新不出来的
Date threeMonthsAgo = Date.from(LocalDateTime.now().minusMonths(3).atZone(ZoneId.systemDefault()).toInstant());
@@ -303,7 +306,7 @@ private static class UserInteractionState {
}
logger.info("扫描订单发送到微信耗时:{} ms, 订单数:{} ", System.currentTimeMillis() - start, orderRows.size());
//logger.info("扫描订单发送到微信耗时:{} ms, 订单数:{} ", System.currentTimeMillis() - start, orderRows.size());
}
@@ -620,7 +623,8 @@ private static class UserInteractionState {
}
// 打印方法调用和开始结束时间
if (isRealTime) {
if (isRealTime && (LocalDateTime.now().getMinute() % 10 == 0)) {
logger.info(" {} --- 拉取订单, 分钟还是秒 {} , 开始时间:{} --- 结束时间:{}", appKey.substring(appKey.length() - 4), hourMinuteTag, startTime.format(DATE_TIME_FORMATTER), endTime.format(DATE_TIME_FORMATTER));
}

View File

@@ -145,7 +145,7 @@ public class WXUtil {
//if (wxid.equals(super_admin_wxid) || fromwxid.equals(super_admin_wxid)) {
// content = "超管: 凡神 \r\n" + content;
//}
List<String> strings = splitStringByLength(content, 2048);
List<String> strings = splitStringByLength(content, 4096);
int count = 1;
for (String string : strings) {

View File

@@ -2,7 +2,6 @@ package cn.van.business.util;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,7 +32,7 @@ public class WxtsUtil {
content = common + content + "<br><br>";
paramMap.put("text", content);
HttpResponse execute = HttpRequest.post(url).header("vanToken", TOKEN).header("source", "XZJ_UBUNTU").body(JSON.toJSONString(paramMap)).execute();
logger.info("企业微信推送结果:{}", execute);
//logger.info("企业微信推送结果:{}", execute);
} catch (Exception e) {
logger.error("企业微信推送失败:{}", e.getMessage());
}
@@ -41,4 +40,13 @@ public class WxtsUtil {
}
// 添加分级告警方法
public void sendCriticalAlert(String title, String content) {
String formattedMsg = String.format("[CRITICAL] %s\n%s", title, content);
// 这里调用实际的通知渠道,例如:
// - 发送邮件
// - 调用企业微信机器人
// - 触发短信通知
sendNotify(formattedMsg); // 复用原有通知方法
}
}