提交 54558415 编写于 作者: X xiongchun

优化启动过程监听

上级 71f792cc
......@@ -61,4 +61,15 @@ public final class Constants {
public final static String START_SUCCESS = "(ô‿ô) PanGu Dev Framework Started successfully ㊥ 盘古开发框架启动成功";
}
/**
* Nacos配置
*/
public final static class Nacos {
/**
* 缺省组
*/
public final static String DEFAULT_GROUP = "DEFAULT_GROUP";
}
}
......@@ -115,7 +115,7 @@ public class PanGuApplicationBuilder{
String nacosUrl = System.getProperty("nacos.url");
if (StrUtil.isEmpty(active) || StrUtil.isEmpty(nacosUrl)){
String cfgMsg = "-Dactive=dev -Dnacos.url=127.0.0.1:8848 -Dnacos.username=xxxx -Dnacos.password=****";
String errMsg = StrUtil.format("未配置启动参数。配置参考:[{}]", cfgMsg);
String errMsg = StrUtil.format("未配置启动参数。配置示例:[{}]", cfgMsg);
throw new IllegalArgumentException(errMsg);
}else {
String paramsMsg = StrUtil.format("启动参数:-Dactive={} -Dnacos.url={} -D...", active, nacosUrl);
......
package com.gitee.pulanos.pangu.framework.handler;
import java.security.spec.InvalidParameterSpecException;
import java.util.Properties;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.gitee.pulanos.pangu.framework.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;
/**
* 环境变量处理
* 应用环境上下文处理
*
* @author xiongchun
* @since 4.0.0
*/
@Slf4j
@Component
public class EnvironmentHandler {
@Autowired
private ConfigurableEnvironment configurableEnvironment;
@NacosInjected
private ConfigService configService;
@Value("${nacos.config.data-id}")
private String dataId;
/**
* 设置缺省参数
*/
public void setDefaultConfigurations() {
MutablePropertySources mps = configurableEnvironment.getPropertySources();
Properties p = new Properties();
p.put("pangu-powered-by", "PULANOS");
mps.addFirst(new PropertiesPropertySource("defaultProperties", p));
}
/**
* 启动后的环境确认检查点
*/
public void confirmChecks() throws NacosException {
String cfgText = configService.getConfig(dataId, Constants.Nacos.DEFAULT_GROUP, 10000);
if (StrUtil.isEmpty(cfgText)){
String errMsg = "连接配置中心失败或配置中心未对该应用创建配置信息。排查建议:1)确认连接信息;2)在配置中心为此应用创建配置信息。";
throw new IllegalStateException(errMsg);
}else {
log.info("ConfirmChecks PASSED.");
}
}
}
......@@ -5,6 +5,7 @@ import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.spring.util.ConfigParseUtils;
import com.gitee.pulanos.pangu.framework.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -59,7 +60,7 @@ public class LogLevelHandler {
public void initLogLevelWhenSartup() {
String newCfgText = null;
try {
newCfgText = configService.getConfig(dataId, "DEFAULT_GROUP", 5000);
newCfgText = configService.getConfig(dataId, Constants.Nacos.DEFAULT_GROUP, 5000);
if (StrUtil.isNotEmpty(newCfgText)){
refreshLogLevel(newCfgText);
}else {
......
package com.gitee.pulanos.pangu.framework.listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.gitee.pulanos.pangu.framework.Constants;
import com.gitee.pulanos.pangu.framework.handler.EnvironmentHandler;
import com.gitee.pulanos.pangu.framework.handler.LogLevelHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
......@@ -30,8 +32,10 @@ public class ApplicationStartedEventListener implements ApplicationListener<Appl
*
* @param event the event to respond to
*/
@SneakyThrows
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
public void onApplicationEvent(ApplicationStartedEvent event){
environmentHandler.confirmChecks();
environmentHandler.setDefaultConfigurations();
logLevelHandler.initLogLevelWhenSartup();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册