初始化仓库
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package xtools.boot.elasticsearch;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.boot.elasticsearch.selector.BootElasticsearchImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : BootElasticsearchConfiguration</p>
|
||||
* <p>Description : BootElasticsearchConfiguration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(BootElasticsearchImportSelector.class)
|
||||
public class BootElasticsearchConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public BootElasticsearchConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(BootElasticsearchConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package xtools.boot.elasticsearch.enums;
|
||||
|
||||
import xtools.boot.api.enums.BaseEnum;
|
||||
|
||||
/**
|
||||
* <p>Title : ElasticsearchMonitorEnums</p>
|
||||
* <p>Description : ElasticsearchMonitorEnums</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/3 10:31
|
||||
*/
|
||||
public enum ElasticsearchMonitorEnums implements BaseEnum {
|
||||
|
||||
// 集群索引信息
|
||||
INDICES(1, "集群索引信息", "/_cat/indices?v"),
|
||||
// 集群健康信息
|
||||
HEALTH(2, "集群健康信息", "/_cat/health?v"),
|
||||
// 集群分片信息
|
||||
SHARDS(3, "集群分片信息", "/_cat/shards?v"),
|
||||
// 集群节点信息
|
||||
NODES(4, "集群节点信息", "/_cat/nodes?v"),
|
||||
// 集群任务
|
||||
TASKS(5, "集群任务", "/_cat/tasks?v"),
|
||||
// 集群段信息
|
||||
SEGMENTS(6, "集群段信息", "/_cat/segments?v"),
|
||||
// 集群文档总数
|
||||
COUNT(7, "集群文档总数", "/_cat/count?v"),
|
||||
// 集群shard的recovery过程
|
||||
RECOVERY(8, "集群shard的recovery过程", "/_cat/recovery?v"),
|
||||
// 集群别名组
|
||||
ALIASES(9, "集群别名组", "/_cat/aliases?v"),
|
||||
// 集群线程池任务
|
||||
THREAD_POOL(10, "集群线程池任务", "/_cat/thread_pool?v"),
|
||||
// 集群插件信息
|
||||
PLUGINS(11, "集群插件信息", "/_cat/plugins?v"),
|
||||
// 单节点的自定义属性
|
||||
NODEATTRS(12, "单节点的自定义属性", "/_cat/nodeattrs?v"),
|
||||
;
|
||||
|
||||
/**
|
||||
* 编码
|
||||
*/
|
||||
private final int code;
|
||||
|
||||
/**
|
||||
* 说明
|
||||
*/
|
||||
private final String desc;
|
||||
|
||||
/**
|
||||
* 命令
|
||||
*/
|
||||
private final String cmd;
|
||||
|
||||
/**
|
||||
* 初始化方法
|
||||
*
|
||||
* @param code Code
|
||||
* @param desc 说明
|
||||
* @param cmd 命令
|
||||
*/
|
||||
ElasticsearchMonitorEnums(int code, String desc, String cmd) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
this.cmd = cmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断枚举值类型
|
||||
*
|
||||
* @param code 枚举值
|
||||
* @return 枚举值类型
|
||||
*/
|
||||
public static ElasticsearchMonitorEnums valueOf(int code) {
|
||||
for (ElasticsearchMonitorEnums type : values()) {
|
||||
if (type.code == code) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("unknown code, code=" + code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有枚举
|
||||
*
|
||||
* @return 所有枚举
|
||||
*/
|
||||
@Override
|
||||
public BaseEnum[] all() {
|
||||
return values();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取枚举编码
|
||||
*
|
||||
* @return 枚举编码
|
||||
*/
|
||||
@Override
|
||||
public int code() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取枚举说明
|
||||
*
|
||||
* @return 枚举说明
|
||||
*/
|
||||
@Override
|
||||
public String desc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取命令
|
||||
*
|
||||
* @return 命令
|
||||
*/
|
||||
public String cmd() {
|
||||
return cmd;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package xtools.boot.elasticsearch.monitor;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.api.exection.BizError;
|
||||
import xtools.boot.elasticsearch.enums.ElasticsearchMonitorEnums;
|
||||
import xtools.boot.elasticsearch.utils.EsUtils;
|
||||
import xtools.core.ArrUtils;
|
||||
import xtools.core.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>Title : ElasticsearchMonitor</p>
|
||||
* <p>Description : ElasticsearchMonitor</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/14 17:58
|
||||
*/
|
||||
@Component
|
||||
public class ElasticsearchMonitor implements BaseParams {
|
||||
|
||||
/**
|
||||
* 获取Elasticsearch监控信息
|
||||
*
|
||||
* @param type 枚举
|
||||
* @return Elasticsearch监控信息
|
||||
*/
|
||||
public JSONObject info(ElasticsearchMonitorEnums type) {
|
||||
if (Objects.isNull(type)) {
|
||||
throw new BizError("类型参数不能为空");
|
||||
}
|
||||
// 查询
|
||||
String result;
|
||||
try {
|
||||
result = EsUtils.exec(type.cmd(), null, String.class, true);
|
||||
} catch (Exception e) {
|
||||
throw new BizError("查询异常");
|
||||
}
|
||||
// 处理数据
|
||||
result = result.replaceAll("\n", "###").replaceAll("\\s+", " ");
|
||||
List<String> arr = ArrUtils.toStringList(result, "###");
|
||||
if (CollectionUtils.isEmpty(arr)) {
|
||||
throw new BizError("类型参数不能为空");
|
||||
}
|
||||
// 封装表头
|
||||
List<String> titleList = ArrUtils.toStringList(arr.getFirst(), " ");
|
||||
// 处理主体数据
|
||||
List<JSONObject> dataList = new ArrayList<>();
|
||||
for (int i = CP_NUM1; i < arr.size(); i++) {
|
||||
String line = arr.get(i);
|
||||
List<String> data = ArrUtils.toStringList(line, " ");
|
||||
JSONObject lineData = new JSONObject();
|
||||
for (int j = CP_NUM0; j < data.size(); j++) {
|
||||
lineData.put(titleList.get(j), data.get(j));
|
||||
}
|
||||
dataList.add(lineData);
|
||||
}
|
||||
return JSONObject.of("title", titleList, "dataList", dataList);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xtools.boot.elasticsearch.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : BootElasticsearchImportSelector</p>
|
||||
* <p>Description : BootElasticsearchImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class BootElasticsearchImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.boot.elasticsearch");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package xtools.boot.elasticsearch.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.core.ArrUtils;
|
||||
import xtools.core.StringUtils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>Title : EsQueryUtils</p>
|
||||
* <p>Description : EsQueryUtils</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/14 08:58
|
||||
*/
|
||||
public class EsQueryUtils implements BaseParams {
|
||||
|
||||
/**
|
||||
* 设置精确查询
|
||||
*
|
||||
* @param query 查询条件
|
||||
* @param filedName 字段名称
|
||||
* @param params 参数
|
||||
*/
|
||||
public static void setMatchPhrase(JSONArray query, String filedName, Object params) {
|
||||
if (params instanceof String txt && StringUtils.isNotBlank(txt)) {
|
||||
query.add(JSONObject.of("match_phrase", JSONObject.of(filedName, txt)));
|
||||
} else if (Objects.nonNull(params)) {
|
||||
query.add(JSONObject.of("match_phrase", JSONObject.of(filedName, params)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊查询
|
||||
*
|
||||
* @param query 模糊查询
|
||||
* @param filedName 字段名称
|
||||
* @param params 参数
|
||||
*/
|
||||
public static void setWildcard(JSONArray query, String filedName, String params) {
|
||||
if (StringUtils.isBlank(params)) {
|
||||
return;
|
||||
}
|
||||
query.add(JSONObject.of("wildcard", JSONObject.of(filedName, "*" + params + "*")));
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间范围查询
|
||||
*
|
||||
* @param query 时间范围查询
|
||||
* @param filedName 字段名称
|
||||
* @param params 参数
|
||||
*/
|
||||
public static void setTimeRange(JSONArray query, String filedName, Instant[] params) {
|
||||
if (ArrUtils.isEmpty(params)) {
|
||||
return;
|
||||
}
|
||||
long start = Long.MIN_VALUE;
|
||||
long end = Long.MAX_VALUE;
|
||||
Instant startTime = params[CP_NUM0];
|
||||
if (Objects.nonNull(startTime)) {
|
||||
start = startTime.toEpochMilli();
|
||||
}
|
||||
Instant endTime = params[CP_NUM1];
|
||||
if (Objects.nonNull(endTime)) {
|
||||
end = endTime.toEpochMilli();
|
||||
}
|
||||
query.add(JSONObject.of("range", JSONObject.of(filedName, JSONObject.of("gte", start, "lte", end))));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package xtools.boot.elasticsearch.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xtools.boot.api.exection.BizError;
|
||||
import xtools.boot.api.exection.BizWarning;
|
||||
import xtools.boot.core.utils.SpringContextUtils;
|
||||
import xtools.boot.log.LogBus;
|
||||
import xtools.boot.log.enums.LogBusBaseType;
|
||||
import xtools.core.BytesUtils;
|
||||
import xtools.core.CollectionUtils;
|
||||
import xtools.core.StringUtils;
|
||||
import xtools.core.encrypt.Base64Utils;
|
||||
import xtools.core.enums.LogLevel;
|
||||
import xtools.core.extend.HttpUtils;
|
||||
import xtools.extend.JsonUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>Title : EsUtils</p>
|
||||
* <p>Description : EsUtils</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2025年8月17日 下午6:24:22
|
||||
*/
|
||||
@Component
|
||||
public class EsUtils {
|
||||
|
||||
/**
|
||||
* 超时时间
|
||||
*/
|
||||
private static final long TIMEOUT = 60 * 1000 * 5;
|
||||
|
||||
/**
|
||||
* 查询服务器地址
|
||||
*/
|
||||
private static String host;
|
||||
|
||||
/**
|
||||
* 授权信息
|
||||
*/
|
||||
private static String auth;
|
||||
|
||||
|
||||
@Value("${spring.elasticsearch.uris:#{null}}")
|
||||
private String uris;
|
||||
@Value("${spring.elasticsearch.username:#{null}}")
|
||||
private String username;
|
||||
@Value("${spring.elasticsearch.password:#{null}}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 执行
|
||||
*
|
||||
* @param uri 执行的uri
|
||||
* @return 执行结果
|
||||
*/
|
||||
public static String exec(String uri) {
|
||||
return exec(uri, null, String.class, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
*
|
||||
* @param uri 执行的uri
|
||||
* @param req 请求参数
|
||||
* @return 执行结果
|
||||
*/
|
||||
public static JSONObject exec(String uri, JSONObject req) {
|
||||
return exec(uri, req, JSONObject.class, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行
|
||||
*
|
||||
* @param <T> 结果类型
|
||||
* @param uri 执行的uri
|
||||
* @param req 请求参数
|
||||
* @param clazz 结果类型
|
||||
* @param isGet 是否为get请求
|
||||
* @return 结果
|
||||
*/
|
||||
public static <T> T exec(String uri, JSONObject req, Class<T> clazz, boolean isGet) {
|
||||
// 参数校验
|
||||
if (StringUtils.isBlank(uri)) {
|
||||
throw new BizWarning("uri不能为空");
|
||||
}
|
||||
if (StringUtils.isBlank(host)) {
|
||||
// 初始化
|
||||
EsUtils bean = SpringContextUtils.getBean(EsUtils.class);
|
||||
bean.init();
|
||||
}
|
||||
HttpUtils http = HttpUtils.init(host + uri).addHeader("Content-Type", "application/json").timeOut(TIMEOUT);
|
||||
if (StringUtils.isNotBlank(auth)) {
|
||||
http.addHeader("Authorization", "Basic " + auth);
|
||||
}
|
||||
http.setLogCallBack(log -> {
|
||||
byte[] resp = log.resp();
|
||||
String respData = BytesUtils.isEmpty(resp) ? null : new String(resp);
|
||||
JSONObject logData = JSONObject.of(
|
||||
"req", log.reqParam(),
|
||||
"resp", JsonUtils.isJson(respData) ? JSONObject.parseObject(respData) : respData,
|
||||
"execTime", log.execTime()
|
||||
);
|
||||
Throwable error = log.error();
|
||||
// 保存日志
|
||||
LogBus.init(Objects.isNull(error) ? LogLevel.INFO : LogLevel.ERROR, LogBusBaseType.ELASTICSEARCH)
|
||||
.data(logData)
|
||||
.error(error)
|
||||
.save();
|
||||
});
|
||||
// 查询结果
|
||||
String result;
|
||||
if (isGet) {
|
||||
result = http.get();
|
||||
} else {
|
||||
result = http.post(CollectionUtils.isEmpty(req) ? null : req.toString());
|
||||
}
|
||||
if (StringUtils.isBlank(result)) {
|
||||
throw new BizWarning("ES执行结果为空");
|
||||
}
|
||||
return JSON.to(clazz, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取授权信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param password 密码
|
||||
* @return 授权信息
|
||||
*/
|
||||
private static String getAuth(String username, String password) {
|
||||
if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
|
||||
return null;
|
||||
}
|
||||
return Base64Utils.encodeToStr((username + ":" + password).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public void init() {
|
||||
if (StringUtils.isNotBlank(host)) {
|
||||
return;
|
||||
}
|
||||
if (StringUtils.isBlank(uris)) {
|
||||
throw new BizError("Es工具类初始化失败, spring.elasticsearch.uris 不能为空");
|
||||
}
|
||||
EsUtils.host = uris;
|
||||
EsUtils.auth = getAuth(username, password);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.boot.elasticsearch.BootElasticsearchConfiguration
|
||||
Reference in New Issue
Block a user