QyproAdminApiFacade.java
4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.stdinterface.modules.util.ApiClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.context.utils.PropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 祺鲲 / qypro-admin 第三方接口门面:复用单一 {@link ApiClient},避免重复创建连接与 Token 状态分裂。
* <p>
* 配置项(建议由 Nacos 下发):{@code qypro.admin.systemKey}、{@code qypro.admin.privateKey}、{@code qypro.admin.baseUrl}
*/
@Slf4j
@Component
public class QyproAdminApiFacade implements DisposableBean {
public static final String PROP_SYSTEM_KEY = "sxj.admin.systemKey";
public static final String PROP_PRIVATE_KEY = "sxj.admin.privateKey";
public static final String PROP_BASE_URL = "sxj.admin.baseUrl";
private static final String API_DEVICE_WARNING_CONTENT = "/v1/auth/device/warning-content";
/** 含首次调用至多 3 次 HTTP 调用(重试 < 3 次) */
private static final int WARNING_CONTENT_MAX_ATTEMPTS = 3;
private static final long RATE_LIMIT_BACKOFF_BASE_MS = 1000L;
private final Object clientLock = new Object();
private ApiClient apiClient;
private volatile boolean destroyed;
private final AtomicBoolean missingConfigWarned = new AtomicBoolean(false);
/**
* 预警信息内容格式同步:POST {@value #API_DEVICE_WARNING_CONTENT},Token 模式(与 {@link ApiClient#main} 示例一致)。
*
* @param jsonBody application/json 请求体
* @throws ApiClient.ApiException 业务 code 非 200 或解析失败
*/
public void syncDeviceWarningContent(String jsonBody) {
ApiClient client = getOrCreateClient();
if (client == null) {
return;
}
for (int attempt = 1; attempt <= WARNING_CONTENT_MAX_ATTEMPTS; attempt++) {
log.info("Qypro warning-content 请求 attempt={}/{} api={} body={}",
attempt, WARNING_CONTENT_MAX_ATTEMPTS, API_DEVICE_WARNING_CONTENT, jsonBody);
String response = client.post(API_DEVICE_WARNING_CONTENT, jsonBody);
log.info("Qypro warning-content 响应 attempt={} body={}", attempt, response);
JSONObject o = JSON.parseObject(response);
Integer code = o.getInteger("code");
if (code != null && code == 200) {
return;
}
if (Integer.valueOf(429).equals(code) && attempt < WARNING_CONTENT_MAX_ATTEMPTS) {
long waitMs = RATE_LIMIT_BACKOFF_BASE_MS * (1L << (attempt - 1));
log.warn("Qypro warning-content 限流 code=429,{}ms 后进行第 {} 次重试, msg={}",
waitMs, attempt + 1, o.getString("msg"));
sleepQuietly(waitMs);
continue;
}
throw new ApiClient.ApiException(
"预警内容同步失败: code=" + code + ", msg=" + o.getString("msg"));
}
}
private static void sleepQuietly(long waitMs) {
try {
TimeUnit.MILLISECONDS.sleep(waitMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ApiClient.ApiException("预警内容同步等待重试被中断", e);
}
}
private ApiClient getOrCreateClient() {
if (destroyed) {
return null;
}
synchronized (clientLock) {
if (apiClient != null) {
return apiClient;
}
String systemKey = PropertiesUtils.getProperty(PROP_SYSTEM_KEY, "");
String privateKey = PropertiesUtils.getProperty(PROP_PRIVATE_KEY, "");
String baseUrl = PropertiesUtils.getProperty(PROP_BASE_URL, "");
if (StringUtils.isAnyBlank(systemKey, privateKey, baseUrl)) {
if (missingConfigWarned.compareAndSet(false, true)) {
log.warn("Qypro 管理端未配置完整({}、{}、{}),跳过第三方预警同步",
PROP_SYSTEM_KEY, PROP_PRIVATE_KEY, PROP_BASE_URL);
}
return null;
}
apiClient = new ApiClient(systemKey, privateKey, baseUrl);
return apiClient;
}
}
@Override
public void destroy() {
synchronized (clientLock) {
destroyed = true;
if (apiClient != null) {
try {
apiClient.close();
} catch (Exception e) {
log.warn("关闭 Qypro ApiClient 异常", e);
}
apiClient = null;
}
}
}
}