From 235b54a745b9b98889e2c683ef84e5ced381c254 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Tue, 8 Apr 2025 11:38:55 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=88=B7=E6=96=B0config.properties=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=85=8D=E7=BD=AE=E5=8F=98=E9=87=8F=E5=80=BC=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1394812148744192]增加一个刷新config.properties文件配置变量值接口 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1394812148744192 --- .../tenant/api/util/DownloadLocalFileApi.java | 3 + .../tenant/api/util/GetServerInfoApi.java | 130 ++++++++++++++++++ .../tenant/api/util/RefreshConfigApi.java | 122 ++++++++++++++++ 3 files changed, 255 insertions(+) create mode 100644 src/main/java/neatlogic/module/tenant/api/util/GetServerInfoApi.java create mode 100644 src/main/java/neatlogic/module/tenant/api/util/RefreshConfigApi.java diff --git a/src/main/java/neatlogic/module/tenant/api/util/DownloadLocalFileApi.java b/src/main/java/neatlogic/module/tenant/api/util/DownloadLocalFileApi.java index 64efff72..d7f28ca9 100644 --- a/src/main/java/neatlogic/module/tenant/api/util/DownloadLocalFileApi.java +++ b/src/main/java/neatlogic/module/tenant/api/util/DownloadLocalFileApi.java @@ -19,6 +19,8 @@ package neatlogic.module.tenant.api.util; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.auth.label.ADMIN; import neatlogic.framework.common.config.Config; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.common.util.FileUtil; @@ -46,6 +48,7 @@ import java.io.InputStream; import java.util.Objects; @Service +@AuthAction(action = ADMIN.class) @OperationType(type = OperationTypeEnum.SEARCH) public class DownloadLocalFileApi extends PrivateBinaryStreamApiComponentBase { diff --git a/src/main/java/neatlogic/module/tenant/api/util/GetServerInfoApi.java b/src/main/java/neatlogic/module/tenant/api/util/GetServerInfoApi.java new file mode 100644 index 00000000..8fbb4ca1 --- /dev/null +++ b/src/main/java/neatlogic/module/tenant/api/util/GetServerInfoApi.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.tenant.api.util; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.asynchronization.threadlocal.RequestContext; +import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.auth.label.ADMIN; +import neatlogic.framework.common.config.Config; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.exception.core.ApiRuntimeException; +import neatlogic.framework.heartbeat.dao.mapper.ServerMapper; +import neatlogic.framework.heartbeat.dto.ServerClusterVo; +import neatlogic.framework.integration.authentication.enums.AuthenticateType; +import neatlogic.framework.restful.annotation.Description; +import neatlogic.framework.restful.annotation.Input; +import neatlogic.framework.restful.annotation.OperationType; +import neatlogic.framework.restful.annotation.Param; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.HttpRequestUtil; +import org.apache.catalina.util.ServerInfo; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.lang.management.ManagementFactory; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Objects; + +@Service +@AuthAction(action = ADMIN.class) +@OperationType(type = OperationTypeEnum.SEARCH) +public class GetServerInfoApi extends PrivateApiComponentBase { + + @Resource + private ServerMapper serverMapper; + + @Override + public String getName() { + return "获取服务器信息"; + } + + @Input({ + @Param(name = "serverId", type = ApiParamType.INTEGER, desc = "服务器ID") + }) + @Description(desc = "获取服务器信息") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + JSONObject resultObj = new JSONObject(new LinkedHashMap<>()); + Integer serverId = paramObj.getInteger("serverId"); + if (serverId == null) { + serverId = Config.SCHEDULE_SERVER_ID; + } + if (Objects.equals(serverId, Config.SCHEDULE_SERVER_ID)) { + resultObj.put("Server.服务器版本", ServerInfo.getServerInfo()); + resultObj.put("服务器构建", ServerInfo.getServerBuilt()); + resultObj.put("服务器版本号", ServerInfo.getServerNumber()); + resultObj.put("操作系统名称", System.getProperty("os.name")); + resultObj.put("OS.版本", System.getProperty("os.version")); + resultObj.put("架构", System.getProperty("os.arch")); + resultObj.put("Java 环境变量", System.getProperty("java.home")); + resultObj.put("Java虚拟机版本", System.getProperty("java.runtime.version")); + resultObj.put("JVM.供应商", System.getProperty("java.vm.vendor")); + resultObj.put("CATALINA_BASE", System.getProperty("catalina.base")); + resultObj.put("CATALINA_HOME", System.getProperty("catalina.home")); + List args = ManagementFactory.getRuntimeMXBean().getInputArguments(); + JSONArray array = new JSONArray(); + array.addAll(args); + resultObj.put("命令行参数", array); + resultObj.put("serverId", serverId); + } else { + String host = null; + TenantContext.get().setUseMasterDatabase(true); + ServerClusterVo serverClusterVo = serverMapper.getServerByServerId(serverId); + if (serverClusterVo != null) { + host = serverClusterVo.getHost(); + } + TenantContext.get().setUseMasterDatabase(false); + if (StringUtils.isNotBlank(host)) { + HttpServletRequest request = RequestContext.get().getRequest(); + String url = host + request.getRequestURI(); + HttpRequestUtil httpRequestUtil = HttpRequestUtil.post(url) + .setPayload(paramObj.toJSONString()) + .setAuthType(AuthenticateType.BUILDIN) + .setConnectTimeout(5000) + .setReadTimeout(5000) + .sendRequest(); + String error = httpRequestUtil.getError(); + if (StringUtils.isNotBlank(error)) { + throw new ApiRuntimeException(error); + } + JSONObject resultJson = httpRequestUtil.getResultJson(); + if (MapUtils.isNotEmpty(resultJson)) { + String status = resultJson.getString("Status"); + if (!"OK".equals(status)) { + throw new RuntimeException(resultJson.getString("Message")); + } + resultObj = resultJson.getJSONObject("Return"); + } + } + } + return resultObj; + } + + @Override + public String getToken() { + return "util/serverinfo/get"; + } +} diff --git a/src/main/java/neatlogic/module/tenant/api/util/RefreshConfigApi.java b/src/main/java/neatlogic/module/tenant/api/util/RefreshConfigApi.java new file mode 100644 index 00000000..d41b9aa0 --- /dev/null +++ b/src/main/java/neatlogic/module/tenant/api/util/RefreshConfigApi.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.module.tenant.api.util; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.asynchronization.threadlocal.RequestContext; +import neatlogic.framework.asynchronization.threadlocal.TenantContext; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.auth.label.ADMIN; +import neatlogic.framework.common.config.Config; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.exception.core.ApiRuntimeException; +import neatlogic.framework.heartbeat.dao.mapper.ServerMapper; +import neatlogic.framework.heartbeat.dto.ServerClusterVo; +import neatlogic.framework.integration.authentication.enums.AuthenticateType; +import neatlogic.framework.restful.annotation.Description; +import neatlogic.framework.restful.annotation.Input; +import neatlogic.framework.restful.annotation.OperationType; +import neatlogic.framework.restful.annotation.Param; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.framework.util.HttpRequestUtil; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.Objects; +import java.util.Properties; + +@Service +@AuthAction(action = ADMIN.class) +@OperationType(type = OperationTypeEnum.SEARCH) +public class RefreshConfigApi extends PrivateApiComponentBase { + + @Resource + private ServerMapper serverMapper; + + @Resource + private Config config; + @Override + public String getName() { + return "刷新config.properties文件配置变量值"; + } + + @Input({ + @Param(name = "serverId", type = ApiParamType.INTEGER, desc = "服务器ID") + }) + @Description(desc = "刷新config.properties文件配置变量值") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + JSONObject resultObj = new JSONObject(); + Integer serverId = paramObj.getInteger("serverId"); + if (serverId == null) { + serverId = Config.SCHEDULE_SERVER_ID; + } + if (Objects.equals(serverId, Config.SCHEDULE_SERVER_ID)) { + Properties prop = new Properties(); + boolean flag = config.readProperties(prop); + Config.loadNacosProperties(prop); + if (flag) { + resultObj.put("数据来源", "Nacos"); + } else { + resultObj.put("数据来源", "config.properties"); + } + resultObj.put("config", prop); + resultObj.put("serverId", serverId); + } else { + String host = null; + TenantContext.get().setUseMasterDatabase(true); + ServerClusterVo serverClusterVo = serverMapper.getServerByServerId(serverId); + if (serverClusterVo != null) { + host = serverClusterVo.getHost(); + } + TenantContext.get().setUseMasterDatabase(false); + if (StringUtils.isNotBlank(host)) { + HttpServletRequest request = RequestContext.get().getRequest(); + String url = host + request.getRequestURI(); + HttpRequestUtil httpRequestUtil = HttpRequestUtil.post(url) + .setPayload(paramObj.toJSONString()) + .setAuthType(AuthenticateType.BUILDIN) + .setConnectTimeout(5000) + .setReadTimeout(5000) + .sendRequest(); + String error = httpRequestUtil.getError(); + if (StringUtils.isNotBlank(error)) { + throw new ApiRuntimeException(error); + } + JSONObject resultJson = httpRequestUtil.getResultJson(); + if (MapUtils.isNotEmpty(resultJson)) { + String status = resultJson.getString("Status"); + if (!"OK".equals(status)) { + throw new RuntimeException(resultJson.getString("Message")); + } + resultObj = resultJson.getJSONObject("Return"); + } + } + } + return resultObj; + } + + @Override + public String getToken() { + return "util/config/refresh"; + } +} -- Gitee