diff --git a/modules/agent/src/main/java/cn/keepbx/jpom/controller/IndexController.java b/modules/agent/src/main/java/cn/keepbx/jpom/controller/IndexController.java index a5e108127..a25c64e1e 100644 --- a/modules/agent/src/main/java/cn/keepbx/jpom/controller/IndexController.java +++ b/modules/agent/src/main/java/cn/keepbx/jpom/controller/IndexController.java @@ -9,7 +9,6 @@ import cn.keepbx.jpom.model.system.JpomManifest; import cn.keepbx.jpom.service.WhitelistDirectoryService; import cn.keepbx.jpom.service.manage.ProjectInfoService; import cn.keepbx.util.JvmUtil; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; @@ -32,7 +31,7 @@ public class IndexController extends BaseAgentController { @Resource private ProjectInfoService projectInfoService; - @RequestMapping(value = {"index", "", "index.html", "/"}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = {"index", "", "index.html", "/"}, produces = MediaType.TEXT_PLAIN_VALUE) @NotAuthorize public String index() { return "Jpom-Agent"; diff --git a/modules/agent/src/main/java/cn/keepbx/jpom/system/AgentExtConfigBean.java b/modules/agent/src/main/java/cn/keepbx/jpom/system/AgentExtConfigBean.java index 3337df2f2..abce06556 100644 --- a/modules/agent/src/main/java/cn/keepbx/jpom/system/AgentExtConfigBean.java +++ b/modules/agent/src/main/java/cn/keepbx/jpom/system/AgentExtConfigBean.java @@ -1,5 +1,9 @@ package cn.keepbx.jpom.system; +import cn.hutool.core.net.NetUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpUtil; import cn.jiangzeyin.common.spring.SpringUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @@ -12,6 +16,8 @@ import org.springframework.context.annotation.Configuration; */ @Configuration public class AgentExtConfigBean { + + private static AgentExtConfigBean agentExtConfigBean; /** * 白名单路径是否判断包含关系 */ @@ -36,8 +42,65 @@ public class AgentExtConfigBean { @Value("${log.saveDays:7}") private int logSaveDays; + @Value("${jpom.agent.id:}") + private String agentId; - private static AgentExtConfigBean agentExtConfigBean; + @Value("${jpom.agent.url:}") + private String agentUrl; + + @Value("${jpom.server.url:}") + private String serverUrl; + + @Value("${jpom.server.token:}") + private String serverToken; + + public String getAgentId() { + return agentId; + } + + public String getServerUrl() { + return serverUrl; + } + + public String getServerToken() { + return serverToken; + } + + /** + * 获取当前的url + * + * @return 如果没有配置将自动生成:http://+本地IP+端口 + */ + public String getAgentUrl() { + if (StrUtil.isEmpty(agentUrl)) { + String localhostStr = NetUtil.getLocalhostStr(); + int port = ConfigBean.getInstance().getPort(); + agentUrl = String.format("http://%s:%s", localhostStr, port); + } + if (StrUtil.isEmpty(agentUrl)) { + throw new JpomRuntimeException("获取Agent url失败"); + } + return agentUrl; + } + + /** + * 创建请求对象 + * + * @param openApi url + * @return HttpRequest + * @see cn.keepbx.jpom.common.ServerOpenApi + */ + public HttpRequest createServerRequest(String openApi) { + if (StrUtil.isEmpty(serverUrl)) { + throw new JpomRuntimeException("请先配置server端url"); + } + if (StrUtil.isEmpty(serverToken)) { + throw new JpomRuntimeException("请先配置server端Token"); + } + HttpRequest httpRequest = HttpUtil.createPost(String.format("%s%s", serverUrl, openApi)); + httpRequest.header("JPOM-TOKEN", serverToken); + return httpRequest; + } /** * 配置错误或者没有,默认是7天 @@ -51,7 +114,6 @@ public class AgentExtConfigBean { return logSaveDays; } - /** * 单例 * diff --git a/modules/agent/src/main/java/cn/keepbx/jpom/system/init/AutoRegSeverNode.java b/modules/agent/src/main/java/cn/keepbx/jpom/system/init/AutoRegSeverNode.java new file mode 100644 index 000000000..f0498e27e --- /dev/null +++ b/modules/agent/src/main/java/cn/keepbx/jpom/system/init/AutoRegSeverNode.java @@ -0,0 +1,30 @@ +package cn.keepbx.jpom.system.init; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpRequest; +import cn.jiangzeyin.common.PreLoadClass; +import cn.jiangzeyin.common.PreLoadMethod; +import cn.keepbx.jpom.common.ServerOpenApi; +import cn.keepbx.jpom.system.AgentExtConfigBean; + +/** + * 自动注册server 节点 + * + * @author bwcx_jzy + * @date 2019/8/6 + */ +@PreLoadClass +public class AutoRegSeverNode { + + @PreLoadMethod + private static void reg() { + System.out.println(AgentExtConfigBean.getInstance().getAgentUrl()); + String agentId = AgentExtConfigBean.getInstance().getAgentId(); + String serverUrl = AgentExtConfigBean.getInstance().getServerUrl(); + if (StrUtil.isEmpty(agentId) || StrUtil.isEmpty(serverUrl)) { + // 如果二者缺一不注册 + return; + } + HttpRequest serverRequest = AgentExtConfigBean.getInstance().createServerRequest(ServerOpenApi.UPDATE_NODE_INFO); + } +} diff --git a/modules/agent/src/main/resources/bin/extConfig.yml b/modules/agent/src/main/resources/bin/extConfig.yml index 5797f5f80..a9781a598 100644 --- a/modules/agent/src/main/resources/bin/extConfig.yml +++ b/modules/agent/src/main/resources/bin/extConfig.yml @@ -6,6 +6,16 @@ jpom: agentName: jpomAgent # agent 端管理密码,非必填,如果为空Jpom 会自动生成一串随机字符串当密码 agentPwd: + agent: + # 设置插件端id,配置自动注册服务端需要 + id: + # 当前节点插件端可以访问的url ,如果不设置将使用http://+本地IP+端口 + url: + server: + # 设置服务端的url + url: + # 服务器接口请求token + token: whitelistDirectory: # 白名单目录是否验证包含关系 checkStartsWith: true diff --git a/modules/common/src/main/java/cn/keepbx/jpom/common/ServerOpenApi.java b/modules/common/src/main/java/cn/keepbx/jpom/common/ServerOpenApi.java index bb34cd7a3..d551ebfeb 100644 --- a/modules/common/src/main/java/cn/keepbx/jpom/common/ServerOpenApi.java +++ b/modules/common/src/main/java/cn/keepbx/jpom/common/ServerOpenApi.java @@ -8,5 +8,5 @@ package cn.keepbx.jpom.common; */ public class ServerOpenApi { - public static final String UPDATE_NODE_INFO = "api/node/update"; + public static final String UPDATE_NODE_INFO = "/api/node/update"; }