Search in sources :

Example 1 with VxApiDeployInfos

use of com.szmirren.vxApi.core.entity.VxApiDeployInfos in project VX-API-Gateway by EliMirren.

the class DeploymentVerticle method startAPI.

/**
 * 启动API
 *
 * @param msg
 */
public void startAPI(Message<JsonObject> msg) {
    if (vertx.isClustered()) {
        if (thisVertxName.equals(msg.body().getString("thisVertxName"))) {
            return;
        }
    }
    String appName = msg.body().getString("appName");
    String apiName = msg.body().getString("apiName");
    VxApiDeployInfos deployInfos = applicationMaps.get(appName);
    if (deployInfos == null) {
        msg.reply(-1);
        return;
    }
    JsonObject body = msg.body().getJsonObject("api");
    vertx.<Integer>executeBlocking(future -> {
        // HTTP服务端口启动API
        Integer httpPort = deployInfos.getHttpPort();
        if (httpPort != null) {
            VxApiDeployInfos proxy = portProxyMap.get(httpPort);
            if (proxy != null && proxy.getHttpPort() != null && proxy.getHttpPort().equals(httpPort) && !deployInfos.getAppName().equals(proxy.getAppName())) {
                startApiService(body.copy(), proxy.getAppName(), true, 1, res -> {
                    if (res.succeeded()) {
                        Integer proxyPort = proxy.getHttpPort();
                        Set<String> set = portProxyApplicationMap.get(proxyPort) == null ? new HashSet<>() : portProxyApplicationMap.get(proxyPort);
                        set.add(proxyPort + appName);
                        portProxyApplicationMap.put(proxyPort, set);
                        LOG.info(proxy.getAppName() + "HTTP服务端口代理-->启动" + appName + "应用的API:" + apiName + "-->成功!");
                    } else {
                        LOG.error(proxy.getAppName() + "HTTP服务端口代理-->启动" + appName + "应用的API:" + apiName + "失败:", res.cause());
                        future.fail(res.cause());
                    }
                }, null);
            }
        }
        // HTTPS服务端口启动API
        Integer httpsPort = deployInfos.getHttpsPort();
        if (httpsPort != null) {
            VxApiDeployInfos proxy = portProxyMap.get(httpsPort);
            if (proxy != null && proxy.getHttpsPort() != null && proxy.getHttpsPort().equals(httpsPort) && !deployInfos.getAppName().equals(proxy.getAppName())) {
                startApiService(body.copy(), proxy.getAppName(), true, 2, res -> {
                    if (res.succeeded()) {
                        Integer proxyPort = proxy.getHttpsPort();
                        Set<String> set = portProxyApplicationMap.get(proxyPort) == null ? new HashSet<>() : portProxyApplicationMap.get(proxyPort);
                        set.add(proxyPort + appName);
                        portProxyApplicationMap.put(proxyPort, set);
                        LOG.info(proxy.getAppName() + "HTTP服务端口代理-->启动" + appName + "应用的API:" + apiName + "-->成功!");
                    } else {
                        LOG.error(proxy.getAppName() + "HTTPS服务端口代理-->启动" + appName + "应用的API:" + apiName + "失败:", res.cause());
                        future.fail(res.cause());
                    }
                }, null);
            }
        }
        startApiService(body.copy(), appName, null, null, res -> {
            if (res.succeeded()) {
                LOG.info("启动" + appName + "应用的API:" + apiName + "-->成功!");
                future.complete(res.result().body());
            } else {
                LOG.error("启动" + appName + "应用的API:" + apiName + "失败:", res.cause());
                future.fail(res.cause());
            }
        }, null);
    }, res -> {
        if (res.succeeded()) {
            LOG.info("启动" + appName + "应用的API:" + apiName + "-->成功!");
            Set<String> set = applicationApiMaps.get(appName) == null ? new HashSet<>() : applicationApiMaps.get(appName);
            set.add(apiName);
            applicationApiMaps.put(appName, set);
            msg.reply(1);
        } else {
            LOG.error("启动" + appName + "应用的API:" + apiName + "失败:", res.cause());
            msg.fail(500, res.cause().getMessage());
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) VxApiDeployInfos(com.szmirren.vxApi.core.entity.VxApiDeployInfos)

Example 2 with VxApiDeployInfos

use of com.szmirren.vxApi.core.entity.VxApiDeployInfos in project VX-API-Gateway by EliMirren.

the class DeploymentVerticle method stopAPI.

/**
 * 停止一个API
 *
 * @param msg
 */
public void stopAPI(Message<JsonObject> msg) {
    if (vertx.isClustered()) {
        if (thisVertxName.equals(msg.body().getString("thisVertxName"))) {
            return;
        }
    }
    String appName = msg.body().getString("appName");
    String apiName = msg.body().getString("apiName");
    VxApiDeployInfos deployInfos = applicationMaps.get(appName);
    if (deployInfos == null) {
        msg.reply(1);
        return;
    }
    vertx.<Integer>executeBlocking(futrue -> {
        Integer httpPort = deployInfos.getHttpPort();
        if (httpPort != null) {
            VxApiDeployInfos proxy = portProxyMap.get(httpPort);
            if (proxy != null && proxy.getHttpPort() != null && proxy.getHttpPort().equals(httpPort) && !deployInfos.getAppName().equals(proxy.getAppName())) {
                stopApiServiceSingle(proxy.getAppName(), apiName, res -> {
                    if (res.succeeded()) {
                        LOG.info(proxy.getAppName() + "HTTP服务端口代理-->" + appName + "暂停API: " + apiName + "-->成功");
                    } else {
                        LOG.error(proxy.getAppName() + "HTTP服务端口代理-->" + appName + "暂停API: " + apiName + "-->失败:", res.cause());
                        futrue.fail(res.cause());
                    }
                }, null);
            }
        }
        Integer httpsPort = deployInfos.getHttpsPort();
        if (httpsPort != null) {
            VxApiDeployInfos proxy = portProxyMap.get(httpsPort);
            if (proxy != null && proxy.getHttpsPort() != null && proxy.getHttpsPort().equals(httpsPort) && !deployInfos.getAppName().equals(proxy.getAppName())) {
                stopApiServiceSingle(proxy.getAppName(), apiName, res -> {
                    if (res.succeeded()) {
                        LOG.info(proxy.getAppName() + "HTTPS服务端口代理-->" + appName + "暂停API: " + apiName + "-->成功");
                    } else {
                        LOG.error(proxy.getAppName() + "HTTPS服务端口代理-->" + appName + "暂停API: " + apiName + "-->失败:", res.cause());
                        futrue.fail(res.cause());
                    }
                }, null);
            }
        }
        stopApiServiceSingle(appName, apiName, res -> {
            if (res.succeeded()) {
                LOG.info(appName + "暂停API: " + apiName + "-->成功");
                futrue.complete(res.result().body());
            } else {
                futrue.fail(res.cause());
            }
        }, null);
    }, res -> {
        if (res.succeeded()) {
            if (applicationApiMaps.get(appName) != null) {
                applicationApiMaps.get(appName).remove(apiName);
            }
            LOG.info(appName + "暂停启动API: " + apiName + "-->成功");
            msg.reply(1);
        } else {
            LOG.error(appName + "暂停API: " + apiName + "-->失败:", res.cause());
            msg.fail(500, res.cause().toString());
        }
    });
}
Also used : VxApiDeployInfos(com.szmirren.vxApi.core.entity.VxApiDeployInfos)

Example 3 with VxApiDeployInfos

use of com.szmirren.vxApi.core.entity.VxApiDeployInfos in project VX-API-Gateway by EliMirren.

the class DeploymentVerticle method deploymentAPP.

/**
 * 部署应用程序
 *
 * @param msg
 */
public void deploymentAPP(Message<JsonObject> msg) {
    JsonObject body = new JsonObject();
    if (vertx.isClustered()) {
        if (thisVertxName.equals(body.getString("thisVertxName"))) {
            return;
        }
    }
    String name = msg.body().getString("appName");
    JsonObject application = msg.body().getJsonObject("app");
    body.put("appConfig", application);
    // 获得全局黑名单并部署应用
    vertx.eventBus().<JsonArray>send(thisVertxName + VxApiEventBusAddressConstant.SYSTEM_BLACK_IP_FIND, null, iplist -> {
        if (iplist.succeeded()) {
            // 添加到加载配置
            body.put("blackIpSet", iplist.result().body());
            DeploymentOptions options = new DeploymentOptions(config());
            options.setIsolationGroup(name);
            options.setConfig(body);
            vertx.deployVerticle(VxApiApplication.class.getName(), options, res -> {
                if (res.succeeded()) {
                    LOG.info("启动应用程序:" + name + "-->成功!");
                    // 记录部署信息
                    VxApiServerOptions serverOptions = VxApiServerOptions.fromJson(application.getJsonObject("serverOptions"));
                    VxApiDeployInfos infos = new VxApiDeployInfos(name, res.result(), serverOptions);
                    applicationMaps.put(name, infos);
                    applicationApiMaps.put(name, new HashSet<>());
                    // 设置端口服务号代理
                    // http端口号
                    Integer httpPort = infos.getHttpPort();
                    if (httpPort != null) {
                        if (portProxyMap.get(httpPort) == null) {
                            portProxyMap.put(httpPort, infos);
                        } else {
                            List<VxApiDeployInfos> item = portStandbyProxyMap.get(httpPort) == null ? new ArrayList<VxApiDeployInfos>() : portStandbyProxyMap.get(httpPort);
                            item.add(infos);
                            portStandbyProxyMap.put(httpPort, item);
                        }
                    }
                    // https端口号
                    Integer httpsPort = infos.getHttpsPort();
                    if (httpsPort != null) {
                        if (portProxyMap.get(httpsPort) == null) {
                            portProxyMap.put(httpsPort, infos);
                        } else {
                            List<VxApiDeployInfos> item = portStandbyProxyMap.get(httpsPort) == null ? new ArrayList<VxApiDeployInfos>() : portStandbyProxyMap.get(httpsPort);
                            item.add(infos);
                            portStandbyProxyMap.put(httpsPort, item);
                        }
                    }
                    msg.reply("ok");
                } else {
                    LOG.error("启动应用程序:" + name + "-->失败:" + res.cause());
                    int code = 500;
                    if (res.cause() != null && res.cause().toString().indexOf("Address already in use: bind") > -1) {
                        code = 1111;
                    }
                    msg.fail(code, res.cause().toString());
                }
            });
        } else {
            msg.fail(500, iplist.cause().toString());
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) DeploymentOptions(io.vertx.core.DeploymentOptions) JsonObject(io.vertx.core.json.JsonObject) VxApiServerOptions(com.szmirren.vxApi.core.options.VxApiServerOptions) VxApiDeployInfos(com.szmirren.vxApi.core.entity.VxApiDeployInfos)

Example 4 with VxApiDeployInfos

use of com.szmirren.vxApi.core.entity.VxApiDeployInfos in project VX-API-Gateway by EliMirren.

the class DeploymentVerticle method unDeploymentAPP.

/**
 * 卸载应用程序
 *
 * @param msg
 */
public void unDeploymentAPP(Message<JsonObject> msg) {
    if (vertx.isClustered()) {
        if (thisVertxName.equals(msg.body().getString("thisVertxName"))) {
            return;
        }
    }
    String name = msg.body().getString("appName");
    VxApiDeployInfos deployInfos = applicationMaps.get(name);
    if (deployInfos == null) {
        msg.reply("ok");
        return;
    }
    String deployId = deployInfos.getDeployId();
    Future<Void> undeplyFuture = Future.future();
    undeplyFuture.setHandler(handle -> {
        vertx.undeploy(deployId, res -> {
            if (res.succeeded()) {
                vertx.executeBlocking(futrue -> {
                    LOG.info("暂停应用程序:" + name + "-->成功!");
                    // 设置备用端口服务代理为主端口服务代理
                    Integer httpPort = deployInfos.getHttpPort();
                    if (httpPort != null) {
                        // 代理类部署信息
                        VxApiDeployInfos proxy = portProxyMap.get(httpPort);
                        if (proxy != null && proxy.getHttpPort() != null) {
                            if (portProxyApplicationMap == null) {
                                portProxyApplicationMap = new HashMap<>();
                            }
                            if (portProxyApplicationApiMap == null) {
                                portProxyApplicationApiMap = new HashMap<>();
                            }
                            Integer proxyPort = proxy.getHttpPort();
                            // 查询代理类中是否有启动当前应用的API
                            Set<String> proxySet = portProxyApplicationMap.get(proxyPort);
                            String proxyKey = httpPort + deployInfos.getAppName();
                            if (proxySet != null && !deployInfos.getAppName().equals(proxy.getAppName()) && proxySet.contains(proxyKey)) {
                                proxySet.remove(proxyKey);
                                portProxyApplicationMap.put(proxyPort, proxySet);
                                portProxyApplicationApiMap.remove(proxyKey);
                                Set<String> apiNames = applicationApiMaps.get(deployInfos.getAppName());
                                stopApiRecursion(new ArrayList<>(apiNames), proxy.getAppName(), 0, 0, stopRes -> {
                                    JsonObject result = stopRes.result() == null ? new JsonObject() : stopRes.result();
                                    int success = result.getInteger("success", 0);
                                    int fail = result.getInteger("fail", 0);
                                    LOG.info(proxy.getAppName() + "HTTP服务端口代理暂停:" + deployInfos.getAppName() + "所有API结果: 成功数量:" + success + ",失败数量:" + fail);
                                });
                            }
                            // 设置备用代理为主代理并将代理的API移动转交给备用代理类,如果没有备用类则当前代理
                            if (deployInfos.getAppName().equals(proxy.getAppName())) {
                                List<VxApiDeployInfos> item = portStandbyProxyMap.get(proxyPort);
                                if (item != null && item.size() > 0) {
                                    VxApiDeployInfos standby = item.remove(0);
                                    Set<String> set = portProxyApplicationMap.get(proxyPort);
                                    if (set != null) {
                                        String standbyAppName = standby.getAppName();
                                        JsonArray body = new JsonArray();
                                        set.forEach(k -> {
                                            body.addAll(portProxyApplicationApiMap.get(k));
                                        });
                                        startAllAPIRecursion(body, standbyAppName, 0, null, 0, true, 1, startProxy -> {
                                            JsonObject result = startProxy.result() == null ? new JsonObject() : startProxy.result();
                                            int success = result.getInteger("success", 0);
                                            int fail = result.getInteger("fail", 0);
                                            LOG.info("将代理HTTP服务端口" + proxy.getAppName() + "的API迁移到" + standbyAppName + "启动API结果: 成功数量:" + success + ",失败数量:" + fail);
                                        });
                                    }
                                    portProxyMap.put(proxyPort, standby);
                                } else {
                                    portProxyMap.remove(proxyPort);
                                }
                            }
                        }
                    }
                    Integer httpsPort = deployInfos.getHttpsPort();
                    if (httpsPort != null) {
                        // 代理类部署信息
                        VxApiDeployInfos proxy = portProxyMap.get(httpsPort);
                        if (proxy != null && proxy.getHttpsPort() != null) {
                            if (portProxyApplicationMap == null) {
                                portProxyApplicationMap = new HashMap<>();
                            }
                            if (portProxyApplicationApiMap == null) {
                                portProxyApplicationApiMap = new HashMap<>();
                            }
                            Integer proxyPort = proxy.getHttpsPort();
                            // 查询代理类中是否有启动当前应用的API
                            Set<String> proxySet = portProxyApplicationMap.get(proxyPort);
                            String proxyKey = proxyPort + deployInfos.getAppName();
                            if (proxySet != null && !deployInfos.getAppName().equals(proxy.getAppName()) && proxySet.contains(proxyKey)) {
                                proxySet.remove(proxyKey);
                                portProxyApplicationMap.put(proxyPort, proxySet);
                                portProxyApplicationApiMap.remove(proxyKey);
                                Set<String> apiNames = applicationApiMaps.get(deployInfos.getAppName());
                                stopApiRecursion(new ArrayList<>(apiNames), proxy.getAppName(), 0, 0, stopRes -> {
                                    JsonObject result = stopRes.result() == null ? new JsonObject() : stopRes.result();
                                    int success = result.getInteger("success", 0);
                                    int fail = result.getInteger("fail", 0);
                                    LOG.info(proxy.getAppName() + "HTTPS服务端口代理暂停:" + deployInfos.getAppName() + "所有API结果: 成功数量:" + success + ",失败数量:" + fail);
                                });
                            }
                            // 设置备用代理为主代理并将代理的API移动转交给备用代理类,如果没有代理则删除自己的代理
                            if (deployInfos.getAppName().equals(proxy.getAppName())) {
                                List<VxApiDeployInfos> item = portStandbyProxyMap.get(proxyPort);
                                if (item != null && item.size() > 0) {
                                    VxApiDeployInfos standby = item.remove(0);
                                    Set<String> set = portProxyApplicationMap.get(proxyPort);
                                    if (set != null) {
                                        String standbyAppName = standby.getAppName();
                                        JsonArray body = new JsonArray();
                                        set.forEach(k -> {
                                            body.addAll(portProxyApplicationApiMap.get(k));
                                        });
                                        startAllAPIRecursion(body, standbyAppName, 0, null, 0, true, 1, startProxy -> {
                                            JsonObject result = startProxy.result() == null ? new JsonObject() : startProxy.result();
                                            int success = result.getInteger("success", 0);
                                            int fail = result.getInteger("fail", 0);
                                            LOG.info("将代理HTTP服务端口" + proxy.getAppName() + "的API迁移到" + standbyAppName + "启动API结果: 成功数量:" + success + ",失败数量:" + fail);
                                        });
                                    }
                                    portProxyMap.put(proxyPort, standby);
                                } else {
                                    portProxyMap.remove(proxyPort);
                                }
                            }
                        }
                    }
                    futrue.complete();
                }, futrueRes -> {
                    applicationMaps.remove(deployInfos.getAppName());
                    applicationApiMaps.remove(deployInfos.getAppName());
                    msg.reply("ok");
                });
            } else {
                LOG.error("暂停应用程序:" + name + "-->失败:" + res.cause());
                msg.fail(500, res.cause().toString());
            }
        });
    });
    // 暂停API后暂停应用
    vertx.executeBlocking(futrue -> {
        stopApiRecursion(new ArrayList<>(applicationApiMaps.get(deployInfos.getAppName())), deployInfos.getAppName(), 0, 0, res -> {
            JsonObject result = res.result() == null ? new JsonObject() : res.result();
            int success = result.getInteger("success", 0);
            int fail = result.getInteger("fail", 0);
            LOG.info("执行暂停应用" + deployInfos.getAppName() + "->暂停所有API-->结果:成功数量:" + success + ",失败数量:" + fail);
            futrue.complete();
        });
    }, undeplyFuture);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) VxApiDeployInfos(com.szmirren.vxApi.core.entity.VxApiDeployInfos)

Example 5 with VxApiDeployInfos

use of com.szmirren.vxApi.core.entity.VxApiDeployInfos in project VX-API-Gateway by EliMirren.

the class DeploymentVerticle method startAllAPI.

/**
 * 启动所有API
 *
 * @param msg
 */
public void startAllAPI(Message<JsonObject> msg) {
    if (vertx.isClustered()) {
        if (thisVertxName.equals(msg.body().getString("thisVertxName"))) {
            return;
        }
    }
    String appName = msg.body().getString("appName");
    VxApiDeployInfos deployInfos = applicationMaps.get(appName);
    if (deployInfos == null) {
        msg.reply(ResultFormat.format(HTTPStatusCodeMsgEnum.C1400, "应用尚未启动"));
        LOG.info("启动" + appName + "所有API-->失败:应用尚未启动");
        return;
    }
    JsonArray body = msg.body().getJsonArray("apis");
    if (body != null && body.size() > 0) {
        if (applicationApiMaps.get(appName) == null) {
            applicationApiMaps.put(appName, new HashSet<>());
        }
        // 启动所有API
        vertx.<String>executeBlocking(fut -> {
            // 代理是否启动http服务
            Integer httpPort = deployInfos.getHttpPort();
            if (httpPort != null) {
                VxApiDeployInfos proxy = portProxyMap.get(httpPort);
                if (proxy != null && proxy.getHttpPort() != null && proxy.getHttpPort().equals(httpPort) && !proxy.getAppName().equals(deployInfos.getAppName())) {
                    Integer proxyPort = proxy.getHttpPort();
                    startAllAPIRecursion(body.copy(), proxy.getAppName(), 0, null, 0, true, 1, res -> {
                        JsonObject result = res.result() == null ? new JsonObject() : res.result();
                        int success = result.getInteger("success", 0);
                        int fail = result.getInteger("fail", 0);
                        LOG.info(proxy.getAppName() + "代理HTTP服务端口-->启动" + appName + "所有API结果: 成功数量:" + success + ",失败数量:" + fail);
                        Set<String> item = portProxyApplicationMap.get(proxyPort) == null ? new HashSet<>() : portProxyApplicationMap.get(proxyPort);
                        item.add(proxyPort + appName);
                        portProxyApplicationMap.put(proxyPort, item);
                    });
                }
            }
            // 代理是否启动https服务
            Integer httpsPort = deployInfos.getHttpsPort();
            if (httpsPort != null) {
                VxApiDeployInfos proxy = portProxyMap.get(httpsPort);
                if (proxy != null && proxy.getHttpsPort() != null && proxy.getHttpsPort().equals(httpsPort) && !proxy.getAppName().equals(deployInfos.getAppName())) {
                    Integer proxyPort = proxy.getHttpsPort();
                    startAllAPIRecursion(body.copy(), proxy.getAppName(), 0, null, 0, true, 2, res -> {
                        JsonObject result = res.result() == null ? new JsonObject() : res.result();
                        int success = result.getInteger("success", 0);
                        int fail = result.getInteger("fail", 0);
                        LOG.info(proxy.getAppName() + "HTTPS服务端口代理-->启动" + appName + "所有API结果: 成功数量:" + success + ",失败数量:" + fail);
                        Set<String> item = portProxyApplicationMap.get(proxyPort) == null ? new HashSet<>() : portProxyApplicationMap.get(proxyPort);
                        item.add(proxyPort + appName);
                        portProxyApplicationMap.put(proxyPort, item);
                    });
                }
            }
            // 启动成功的API集合
            Set<String> successSet = new HashSet<>();
            startAllAPIRecursion(body.copy(), appName, 0, successSet, 0, res -> {
                JsonObject result = res.result() == null ? new JsonObject() : res.result();
                int success = result.getInteger("success", 0);
                int fail = result.getInteger("fail", 0);
                LOG.info("启动" + appName + "所有API结果: 成功数量:" + success + ",失败数量:" + fail);
                applicationApiMaps.put(appName, successSet);
                fut.complete(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, "启动" + appName + "所有API结果: 成功数量:" + success + ",失败数量:" + fail));
            });
        }, res -> {
            msg.reply(res.result());
        });
    } else {
        msg.reply(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, "API数量为: 0"));
        if (LOG.isDebugEnabled()) {
            LOG.debug("启动" + appName + "所有API结果:API数量为 0");
        }
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) VxApiDeployInfos(com.szmirren.vxApi.core.entity.VxApiDeployInfos) HashSet(java.util.HashSet)

Aggregations

VxApiDeployInfos (com.szmirren.vxApi.core.entity.VxApiDeployInfos)5 JsonObject (io.vertx.core.json.JsonObject)4 JsonArray (io.vertx.core.json.JsonArray)3 VxApiServerOptions (com.szmirren.vxApi.core.options.VxApiServerOptions)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 HashSet (java.util.HashSet)1