Search in sources :

Example 1 with AgentExtConfigBean

use of io.jpom.system.AgentExtConfigBean in project Jpom by dromara.

the class AutoRegSeverNode method reg.

/**
 * 向服务端注册插件端
 */
@PreLoadMethod
private static void reg() {
    AgentExtConfigBean instance = AgentExtConfigBean.getInstance();
    String agentId = instance.getAgentId();
    String serverUrl = instance.getServerUrl();
    if (StrUtil.isEmpty(agentId) || StrUtil.isEmpty(serverUrl)) {
        // 如果二者缺一不注册
        return;
    }
    String oldInstallId = null;
    File file = FileUtil.file(ConfigBean.getInstance().getDataPath(), AgentConfigBean.SERVER_ID);
    JSONObject serverJson = null;
    if (file.exists()) {
        try {
            serverJson = (JSONObject) JsonFileUtil.readJson(file.getAbsolutePath());
        } catch (FileNotFoundException e) {
            serverJson = new JSONObject();
        }
        oldInstallId = serverJson.getString("installId");
    }
    HttpRequest installRequest = instance.createServerRequest(ServerOpenApi.INSTALL_ID);
    String body1 = installRequest.execute().body();
    JsonMessage jsonMessage = JSON.parseObject(body1, JsonMessage.class);
    if (jsonMessage.getCode() != HttpStatus.HTTP_OK) {
        DefaultSystemLog.getLog().error("获取Server 安装id失败:" + jsonMessage);
        return;
    }
    String installId = jsonMessage.dataToString();
    boolean eqInstall = StrUtil.equals(oldInstallId, installId);
    // 
    URL url = URLUtil.toUrlForHttp(instance.getAgentUrl());
    String protocol = url.getProtocol();
    HttpRequest serverRequest = instance.createServerRequest(ServerOpenApi.UPDATE_NODE_INFO);
    serverRequest.form("id", agentId);
    serverRequest.form("name", "节点:" + agentId);
    serverRequest.form("openStatus", 1);
    serverRequest.form("protocol", protocol);
    serverRequest.form("url", url.getHost() + CharPool.COLON + url.getPort());
    AgentAuthorize agentAuthorize = AgentAuthorize.getInstance();
    serverRequest.form("loginName", agentAuthorize.getAgentName());
    serverRequest.form("loginPwd", agentAuthorize.getAgentPwd());
    serverRequest.form("type", eqInstall ? "update" : "add");
    String body = serverRequest.execute().body();
    DefaultSystemLog.getLog().info("自动注册Server:" + body);
    JsonMessage regJsonMessage = JSON.parseObject(body, JsonMessage.class);
    if (regJsonMessage.getCode() == HttpStatus.HTTP_OK) {
        if (serverJson == null) {
            serverJson = new JSONObject();
        }
        if (!eqInstall) {
            serverJson.put("installId", installId);
            serverJson.put("regTime", DateTime.now().toString());
        } else {
            serverJson.put("updateTime", DateTime.now().toString());
        }
        JsonFileUtil.saveJson(file.getAbsolutePath(), serverJson);
    } else {
        DefaultSystemLog.getLog().error("自动注册插件端失败:{}", body);
    }
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject) AgentAuthorize(io.jpom.system.AgentAuthorize) JsonMessage(cn.jiangzeyin.common.JsonMessage) FileNotFoundException(java.io.FileNotFoundException) AgentExtConfigBean(io.jpom.system.AgentExtConfigBean) File(java.io.File) URL(java.net.URL) PreLoadMethod(cn.jiangzeyin.common.PreLoadMethod)

Aggregations

HttpRequest (cn.hutool.http.HttpRequest)1 JsonMessage (cn.jiangzeyin.common.JsonMessage)1 PreLoadMethod (cn.jiangzeyin.common.PreLoadMethod)1 JSONObject (com.alibaba.fastjson.JSONObject)1 AgentAuthorize (io.jpom.system.AgentAuthorize)1 AgentExtConfigBean (io.jpom.system.AgentExtConfigBean)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 URL (java.net.URL)1