Search in sources :

Example 1 with VxApiCluster

use of com.szmirren.vxApi.cluster.VxApiCluster in project VX-API-Gateway by EliMirren.

the class VxApiMain method getConfig.

/**
 * 获得配置文件
 */
public void getConfig(Handler<AsyncResult<JsonObject>> conf) {
    if (config() == null || config().isEmpty()) {
        // 获得系统配置文件
        vertx.fileSystem().readFile(PathUtil.getPathString("conf.json"), res -> {
            if (res.succeeded()) {
                try {
                    JsonObject config = res.result().toJsonObject();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("执行加载基本配置文件-->结果:" + config);
                    }
                    // 集群配置文件
                    JsonObject clusterc = config.getJsonObject("cluster", new JsonObject().put("clusterType", CLUSTER_TYPE));
                    String clusterType = clusterc.getString("clusterType");
                    boolean confFromCluster = clusterc.getBoolean("getVxApiConfFromCluster", false);
                    // 从集群环境中获取应用配置文件
                    if (!CLUSTER_TYPE.equals(clusterType) && confFromCluster) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("执行从集群环境中加载配置文件...");
                        }
                        // 获取集群环境中的配置文件
                        VxApiCluster clusterConfig = VxApiClusterFactory.getClusterConfig(clusterType, clusterc.getJsonObject("clusterConf", getDefaultClusterConfig()), vertx);
                        clusterConfig.getConfig(handler -> {
                            if (handler.succeeded()) {
                                JsonObject result = handler.result();
                                if (result != null && !result.isEmpty()) {
                                    if (LOG.isDebugEnabled()) {
                                        LOG.debug("执行从集群环境中加载配置文件-->结果:" + result);
                                    }
                                    conf.handle(Future.<JsonObject>succeededFuture(result));
                                }
                            } else {
                                LOG.error("执行从集群环境中加载配置文件-->失败:" + handler.cause());
                                conf.handle(Future.failedFuture(handler.cause()));
                            }
                        });
                    } else {
                        // 从配置中获取应用配置文件
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("执行从基本配置中加载配置文件...");
                        }
                        JsonObject nextConf = new JsonObject();
                        nextConf.put("verticleConfig", config.getJsonObject("verticleConfig", new JsonObject()));
                        nextConf.put("dataConfig", config.getJsonObject("dataConfig", getDefaultDataConfig()));
                        nextConf.put("clientConfig", config.getJsonObject("clientConfig", getDefaultClientConfig()));
                        nextConf.put("cliConfig", config.getJsonObject("cliConfig", getDefaultClientConfig()));
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("执行从基本配置中加载配置文件-->结果:" + nextConf);
                        }
                        conf.handle(Future.<JsonObject>succeededFuture(nextConf));
                    }
                } catch (Exception e) {
                    LOG.error("获取配置文件-->失败:", e);
                    conf.handle(Future.<JsonObject>failedFuture(e));
                }
            } else {
                LOG.error("获取配置文件-->失败:", res.cause());
                conf.handle(Future.<JsonObject>failedFuture(res.cause()));
            }
        });
    } else {
        conf.handle(Future.<JsonObject>succeededFuture(config()));
    }
}
Also used : VxApiCluster(com.szmirren.vxApi.cluster.VxApiCluster) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

VxApiCluster (com.szmirren.vxApi.cluster.VxApiCluster)1 JsonObject (io.vertx.core.json.JsonObject)1