插件端自动注册到服务端(准备)

This commit is contained in:
bwcx_jzy 2019-08-06 23:57:35 +08:00
parent b5ea653b22
commit a4e3f10a95
5 changed files with 106 additions and 5 deletions

View File

@ -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";

View File

@ -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;
}
/**
* 单例
*

View File

@ -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);
}
}

View File

@ -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

View File

@ -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";
}