记录同步数据日志功能

This commit is contained in:
wuweihua
2026-09-11 10:55:14 +08:00
parent e6d57a9000
commit 06211adaac
17 changed files with 1066 additions and 5 deletions
@@ -0,0 +1,29 @@
package com.ruoyi.price.search;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Bill sync search-store beans.
*/
@Configuration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class BillSearchConfiguration
{
@Bean
@ConditionalOnProperty(prefix = "bms.elasticsearch", name = "enabled", havingValue = "true", matchIfMissing = true)
public IBillSearchStore esBillSearchStore(ElasticsearchProperties properties)
{
return new EsBillSearchStore(properties);
}
@Bean
@ConditionalOnMissingBean(IBillSearchStore.class)
public IBillSearchStore noopBillSearchStore()
{
return new NoopBillSearchStore();
}
}
@@ -0,0 +1,31 @@
package com.ruoyi.price.search;
import java.util.ArrayList;
import java.util.List;
/** Paginated ES search result. */
public class BillSearchPage
{
private long total;
private List<BillSyncSnapshot> rows = new ArrayList<BillSyncSnapshot>();
public long getTotal()
{
return total;
}
public void setTotal(long total)
{
this.total = total;
}
public List<BillSyncSnapshot> getRows()
{
return rows;
}
public void setRows(List<BillSyncSnapshot> rows)
{
this.rows = rows == null ? new ArrayList<BillSyncSnapshot>() : rows;
}
}
@@ -0,0 +1,72 @@
package com.ruoyi.price.search;
/** Generic Elasticsearch business log. Distinguished by logType + bizId. */
public class BillSyncSnapshot
{
private String docId;
private String logType;
private String bizId;
private String requestJson;
private String responseJson;
private String updatedAt;
public String getDocId()
{
return docId;
}
public void setDocId(String docId)
{
this.docId = docId;
}
public String getLogType()
{
return logType;
}
public void setLogType(String logType)
{
this.logType = logType;
}
public String getBizId()
{
return bizId;
}
public void setBizId(String bizId)
{
this.bizId = bizId;
}
public String getRequestJson()
{
return requestJson;
}
public void setRequestJson(String requestJson)
{
this.requestJson = requestJson;
}
public String getResponseJson()
{
return responseJson;
}
public void setResponseJson(String responseJson)
{
this.responseJson = responseJson;
}
public String getUpdatedAt()
{
return updatedAt;
}
public void setUpdatedAt(String updatedAt)
{
this.updatedAt = updatedAt;
}
}
@@ -0,0 +1,73 @@
package com.ruoyi.price.search;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Elasticsearch connection for bill sync search store.
*/
@ConfigurationProperties(prefix = "bms.elasticsearch")
public class ElasticsearchProperties
{
/** false = skip ES write/read */
private boolean enabled = true;
/** e.g. http://127.0.0.1:9200 */
private String uris = "http://127.0.0.1:9200";
private String username;
private String password;
/** index name */
private String index = "bms_biz_log";
public boolean isEnabled()
{
return enabled;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
public String getUris()
{
return uris;
}
public void setUris(String uris)
{
this.uris = uris;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getIndex()
{
return index;
}
public void setIndex(String index)
{
this.index = index;
}
}
@@ -0,0 +1,330 @@
package com.ruoyi.price.search;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
/**
* Persist / load generic business logs via Elasticsearch REST API.
*/
public class EsBillSearchStore implements IBillSearchStore
{
private static final Logger log = LoggerFactory.getLogger(EsBillSearchStore.class);
private final ElasticsearchProperties properties;
private final RestTemplate restTemplate;
public EsBillSearchStore(ElasticsearchProperties properties)
{
this.properties = properties;
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(3000);
factory.setReadTimeout(5000);
RestTemplate template = new RestTemplate(factory);
if (StringUtils.isNotEmpty(properties.getUsername()))
{
String user = properties.getUsername();
String pass = properties.getPassword() == null ? "" : properties.getPassword();
template.getInterceptors().add((request, body, execution) -> {
request.getHeaders().setBasicAuth(user, pass);
return execution.execute(request, body);
});
}
this.restTemplate = template;
}
@Override
public void save(String logType, String bizId, String requestJson, String responseJson)
{
if (StringUtils.isEmpty(logType) || StringUtils.isEmpty(bizId))
{
return;
}
String docId = EsLogType.docId(logType, bizId);
try
{
JSONObject doc = new JSONObject();
doc.put("logType", logType.trim());
doc.put("bizId", bizId.trim());
doc.put("request", parseOrRaw(requestJson));
doc.put("response", parseOrRaw(responseJson));
doc.put("updatedAt", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(doc.toJSONString(), headers);
restTemplate.exchange(docUrl(docId), HttpMethod.PUT, entity, String.class);
}
catch (Exception e)
{
log.warn("elasticsearch save {} / {} failed: {}", logType, bizId, e.getMessage());
}
}
@Override
public BillSyncSnapshot get(String logType, String bizId)
{
if (StringUtils.isEmpty(logType) || StringUtils.isEmpty(bizId))
{
return null;
}
return getByDocId(EsLogType.docId(logType, bizId));
}
@Override
public BillSyncSnapshot getByDocId(String docId)
{
if (StringUtils.isEmpty(docId))
{
return null;
}
try
{
ResponseEntity<String> resp = restTemplate.exchange(docUrl(docId), HttpMethod.GET, HttpEntity.EMPTY, String.class);
String body = resp.getBody();
if (StringUtils.isEmpty(body))
{
return null;
}
JSONObject root = JSON.parseObject(body);
if (root == null || !Boolean.TRUE.equals(root.getBoolean("found")))
{
return null;
}
return toSnapshot(root.getJSONObject("_source"), root.getString("_id"));
}
catch (HttpClientErrorException.NotFound e)
{
return null;
}
catch (Exception e)
{
log.warn("elasticsearch get {} failed: {}", docId, e.getMessage());
return null;
}
}
@Override
public BillSearchPage search(String logType, String bizId, int pageNum, int pageSize)
{
BillSearchPage page = new BillSearchPage();
if (pageNum < 1)
{
pageNum = 1;
}
if (pageSize < 1)
{
pageSize = 10;
}
try
{
JSONObject body = new JSONObject();
body.put("from", Integer.valueOf((pageNum - 1) * pageSize));
body.put("size", Integer.valueOf(pageSize));
JSONArray sort = new JSONArray();
JSONObject sortItem = new JSONObject();
JSONObject sortOpt = new JSONObject();
sortOpt.put("order", "desc");
sortOpt.put("unmapped_type", "keyword");
sortItem.put("updatedAt.keyword", sortOpt);
sort.add(sortItem);
body.put("sort", sort);
body.put("query", buildQuery(logType, bizId));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(body.toJSONString(), headers);
ResponseEntity<String> resp = restTemplate.exchange(searchUrl(), HttpMethod.POST, entity, String.class);
JSONObject root = JSON.parseObject(resp.getBody());
if (root == null)
{
return page;
}
JSONObject hits = root.getJSONObject("hits");
if (hits == null)
{
return page;
}
page.setTotal(readTotal(hits.get("total")));
JSONArray hitList = hits.getJSONArray("hits");
if (hitList == null)
{
return page;
}
for (int i = 0; i < hitList.size(); i++)
{
JSONObject hit = hitList.getJSONObject(i);
if (hit == null)
{
continue;
}
BillSyncSnapshot snap = toSnapshot(hit.getJSONObject("_source"), hit.getString("_id"));
if (snap != null)
{
page.getRows().add(snap);
}
}
}
catch (HttpClientErrorException.NotFound e)
{
return page;
}
catch (Exception e)
{
log.warn("elasticsearch search failed: {}", e.getMessage());
}
return page;
}
private BillSyncSnapshot toSnapshot(JSONObject source, String id)
{
if (source == null)
{
return null;
}
BillSyncSnapshot snap = new BillSyncSnapshot();
snap.setDocId(id);
snap.setLogType(source.getString("logType"));
snap.setBizId(source.getString("bizId"));
if (StringUtils.isEmpty(snap.getBizId()))
{
snap.setBizId(source.getString("orderNo"));
}
if (StringUtils.isEmpty(snap.getLogType()) && StringUtils.isNotEmpty(source.getString("billId")))
{
snap.setLogType(EsLogType.BILL);
}
snap.setRequestJson(toJsonString(source.get("request")));
snap.setResponseJson(toJsonString(source.get("response")));
snap.setUpdatedAt(source.getString("updatedAt"));
return snap;
}
private JSONObject buildQuery(String logType, String bizId)
{
JSONArray filters = new JSONArray();
addTerm(filters, "logType", logType);
addWildcard(filters, "bizId", bizId);
if (filters.isEmpty())
{
JSONObject matchAll = new JSONObject();
matchAll.put("match_all", new JSONObject());
return matchAll;
}
JSONObject bool = new JSONObject();
bool.put("must", filters);
JSONObject query = new JSONObject();
query.put("bool", bool);
return query;
}
private void addTerm(JSONArray filters, String field, String value)
{
if (StringUtils.isEmpty(value))
{
return;
}
JSONObject term = new JSONObject();
term.put(field + ".keyword", value.trim());
JSONObject wrap = new JSONObject();
wrap.put("term", term);
filters.add(wrap);
}
private void addWildcard(JSONArray filters, String field, String value)
{
if (StringUtils.isEmpty(value))
{
return;
}
JSONObject wildcard = new JSONObject();
JSONObject opts = new JSONObject();
opts.put("value", "*" + escapeWildcard(value.trim()) + "*");
opts.put("case_insensitive", Boolean.TRUE);
wildcard.put(field + ".keyword", opts);
JSONObject wrap = new JSONObject();
wrap.put("wildcard", wildcard);
filters.add(wrap);
}
private String escapeWildcard(String value)
{
return value.replace("\\", "\\\\").replace("*", "\\*").replace("?", "\\?");
}
private long readTotal(Object total)
{
if (total instanceof JSONObject)
{
return ((JSONObject) total).getLongValue("value");
}
if (total instanceof Number)
{
return ((Number) total).longValue();
}
return 0L;
}
private String searchUrl()
{
return baseUrl() + "/" + properties.getIndex() + "/_search";
}
private String docUrl(String docId)
{
String encodedId = URLEncoder.encode(docId, StandardCharsets.UTF_8);
return baseUrl() + "/" + properties.getIndex() + "/_doc/" + encodedId;
}
private String baseUrl()
{
String base = properties.getUris();
if (base.endsWith("/"))
{
base = base.substring(0, base.length() - 1);
}
return base;
}
private Object parseOrRaw(String json)
{
if (StringUtils.isEmpty(json))
{
return null;
}
try
{
return JSON.parse(json);
}
catch (Exception e)
{
return json;
}
}
private String toJsonString(Object value)
{
if (value == null)
{
return null;
}
if (value instanceof String)
{
return (String) value;
}
return JSON.toJSONString(value);
}
}
@@ -0,0 +1,19 @@
package com.ruoyi.price.search;
/** Common ES business-log types. */
public final class EsLogType
{
public static final String BILL = "BILL";
public static final String QUOTE = "QUOTE";
private EsLogType()
{
}
public static String docId(String logType, String bizId)
{
String type = logType == null ? "" : logType.trim();
String id = bizId == null ? "" : bizId.trim();
return type + "_" + id;
}
}
@@ -0,0 +1,15 @@
package com.ruoyi.price.search;
/**
* Generic Elasticsearch store for business request/response logs.
*/
public interface IBillSearchStore
{
void save(String logType, String bizId, String requestJson, String responseJson);
BillSyncSnapshot get(String logType, String bizId);
BillSyncSnapshot getByDocId(String docId);
BillSearchPage search(String logType, String bizId, int pageNum, int pageSize);
}
@@ -0,0 +1,30 @@
package com.ruoyi.price.search;
/**
* No-op when Elasticsearch is disabled.
*/
public class NoopBillSearchStore implements IBillSearchStore
{
@Override
public void save(String logType, String bizId, String requestJson, String responseJson)
{
}
@Override
public BillSyncSnapshot get(String logType, String bizId)
{
return null;
}
@Override
public BillSyncSnapshot getByDocId(String docId)
{
return null;
}
@Override
public BillSearchPage search(String logType, String bizId, int pageNum, int pageSize)
{
return new BillSearchPage();
}
}