优化 Server 启动脚本日志输出. (#92)

pull/106/head
chen.ma 3 years ago
parent 77eb7e08fa
commit d32e1f4450

@ -0,0 +1,55 @@
package cn.hippo4j.config.toolkit;
import org.apache.commons.lang3.StringUtils;
import java.nio.file.Paths;
import java.util.Objects;
/**
* Env Util.
*
* @author chen.ma
* @date 2022/2/9 07:46
*/
public class EnvUtil {
public static final String HIPPO4J_HOME_KEY = "hippo4j.home";
public static final String STANDALONE_MODE_PROPERTY_NAME = "hippo4j.standalone";
private static String HIPPO4J_HOME_PATH = null;
private static Boolean IS_STANDALONE = null;
/**
* Get hippo4j home.
*
* @return
*/
public static String getHippo4JHome() {
if (StringUtils.isBlank(HIPPO4J_HOME_PATH)) {
String hippo4jHome = System.getProperty(HIPPO4J_HOME_KEY);
if (StringUtils.isBlank(hippo4jHome)) {
hippo4jHome = Paths.get(System.getProperty("user.home"), "hippo4j").toString();
}
return hippo4jHome;
}
return HIPPO4J_HOME_PATH;
}
/**
* Standalone mode or not.
*
* @return
*/
public static boolean getStandaloneMode() {
if (Objects.isNull(IS_STANDALONE)) {
IS_STANDALONE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);
}
return IS_STANDALONE;
}
}

@ -15,7 +15,7 @@ set CUSTOM_SEARCH_LOCATIONS=%BASE_DIR%/conf/application.properties
set SERVER=hippo4j-server
echo "hippo4j is starting with standalone"
set "HIPPO4J_JVM_OPTS=-Xms1024m -Xmx1024m -Xmn512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%BASE_DIR%\logs\java_heapdump.hprof -XX:-UseLargePages"
set "HIPPO4J_JVM_OPTS=-Xms1024m -Xmx1024m -Xmn512m -Dhippo4j.standalone=true -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%BASE_DIR%\logs\java_heapdump.hprof -XX:-UseLargePages"
set "HIPPO4J_OPTS=%HIPPO4J_OPTS% -Dhippo4j.home=%BASE_DIR%"
set "HIPPO4J_OPTS=%HIPPO4J_OPTS% -jar %BASE_DIR%\target\%SERVER%.jar"

@ -16,6 +16,7 @@ else
fi
JAVA_OPT="${JAVA_OPT} -Xms1024m -Xmx1024m -Xmn512m"
JAVA_OPT="${JAVA_OPT} -Dhippo4j.standalone=true"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
JAVA_OPT="${JAVA_OPT} -Dhippo4j.home=${BASE_DIR}"
JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/target/${SERVER}.jar"
@ -39,4 +40,4 @@ fi
echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA ${JAVA_OPT} hippo4j.hippo4j >> ${BASE_DIR}/logs/start.out 2>&1 &
echo "hippo4j is startingyou can check the ${BASE_DIR}/logs/start.out"
echo "hippo4j is startingyou can check the ${BASE_DIR}/logs/start.out"

@ -40,6 +40,7 @@ spring.datasource.hikari.maximumPoolSize=15
spring.datasource.hikari.connection-test-query=SELECT 1
### Mybatis-Plus Config
mybatis-plus.global-config.banner=false
# mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.logic-delete-field=delFlag
mybatis-plus.global-config.db-config.logic-delete-value=1

@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
<springProperty scope="context" name="appName" source="spring.application.name" defaultValue="hippo4j-server" />
<springProperty scope="context" name="logPath" source="hippo4j.logs.path" defaultValue="${hippo4j.home}/logs" />
<springProperty scope="context" name="logLevel" source="logging.level.root" defaultValue="INFO" />
<springProperty scope="context" name="appName" source="spring.application.name" defaultValue="hippo4j-server"/>
<springProperty scope="context" name="logPath" source="hippo4j.logs.path" defaultValue="${hippo4j.home}/logs"/>
<springProperty scope="context" name="logLevel" source="logging.level.root" defaultValue="INFO"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5level %msg%xEx%n%n" />
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5level %msg%xEx%n%n"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="file-all" class="ch.qos.logback.core.rolling.RollingFileAppender">
<appender name="FILE-ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/${appName}-info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logPath}/${appName}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
@ -26,7 +26,7 @@
</encoder>
</appender>
<appender name="file-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/${appName}-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logPath}/${appName}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
@ -41,8 +41,13 @@
</filter>
</appender>
<logger name="cn.hippo4j.server.listener.StartingApplicationListener">
<appender-ref ref="CONSOLE"/>
<level value="INFO"/>
</logger>
<root level="${logLevel}">
<appender-ref ref="file-all" />
<appender-ref ref="file-error" />
<appender-ref ref="FILE-ALL"/>
<appender-ref ref="FILE-ERROR"/>
</root>
</configuration>

@ -39,6 +39,21 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Implementation-Title>${project.artifactId}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Build-Time>${maven.build.timestamp}</Build-Time>
<Built-By>chen.ma</Built-By>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

@ -0,0 +1,59 @@
package cn.hippo4j.server.listener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import java.util.ArrayList;
import java.util.List;
/**
* Base spring application run listener.
*
* @author chen.ma
* @date 2022/2/9 04:29
*/
public class BaseSpringApplicationRunListener implements SpringApplicationRunListener, Ordered {
private final SpringApplication application;
private final String[] args;
private List<Hippo4JApplicationListener> hippo4JApplicationListeners = new ArrayList();
{
hippo4JApplicationListeners.add(new StartingApplicationListener());
}
public BaseSpringApplicationRunListener(SpringApplication application, String[] args) {
this.application = application;
this.args = args;
}
@Override
public void starting() {
hippo4JApplicationListeners.forEach(each -> each.starting());
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
hippo4JApplicationListeners.forEach(each -> each.contextPrepared(context));
}
@Override
public void started(ConfigurableApplicationContext context) {
hippo4JApplicationListeners.forEach(each -> each.started(context));
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
hippo4JApplicationListeners.forEach(each -> each.failed(context, exception));
}
@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
}
}

@ -0,0 +1,40 @@
package cn.hippo4j.server.listener;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Hippo4J application listener.
*
* @author chen.ma
* @date 2022/2/9 04:35
*/
public interface Hippo4JApplicationListener {
/**
* {@link BaseSpringApplicationRunListener#starting}
*/
void starting();
/**
* {@link BaseSpringApplicationRunListener#contextPrepared}
*
* @param context
*/
void contextPrepared(ConfigurableApplicationContext context);
/**
* {@link BaseSpringApplicationRunListener#started}
*
* @param context context
*/
void started(ConfigurableApplicationContext context);
/**
* {@link BaseSpringApplicationRunListener#failed}
*
* @param context
* @param exception
*/
void failed(ConfigurableApplicationContext context, Throwable exception);
}

@ -0,0 +1,78 @@
package cn.hippo4j.server.listener;
import cn.hippo4j.config.toolkit.EnvUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import java.nio.file.Paths;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Starting application listener.
*
* @author chen.ma
* @date 2022/2/9 04:39
*/
@Slf4j
public class StartingApplicationListener implements Hippo4JApplicationListener {
private volatile boolean starting;
private ScheduledExecutorService scheduledExecutorService;
@Override
public void starting() {
starting = true;
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
if (EnvUtil.getStandaloneMode()) {
scheduledExecutorService = new ScheduledThreadPoolExecutor(
1,
r -> {
Thread thread = new Thread(r);
thread.setName("server.hippo4j-starting");
return thread;
}
);
scheduledExecutorService.scheduleWithFixedDelay(() -> {
if (starting) {
log.info("Hippo4J is starting...");
}
}, 1, 1, TimeUnit.SECONDS);
}
}
@Override
public void started(ConfigurableApplicationContext context) {
starting = false;
closeExecutor();
if (EnvUtil.getStandaloneMode()) {
log.info("Hippo4J started successfully...");
}
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
log.error("Startup errors : {}", exception);
closeExecutor();
context.close();
log.error("Hippo4J failed to start, please see {} for more details.",
Paths.get(EnvUtil.getHippo4JHome(), "logs/hippo4j.log"));
}
private void closeExecutor() {
if (scheduledExecutorService != null) {
scheduledExecutorService.shutdownNow();
}
}
}

@ -0,0 +1 @@
org.springframework.boot.SpringApplicationRunListener=cn.hippo4j.server.listener.BaseSpringApplicationRunListener

@ -40,6 +40,7 @@ spring.datasource.hikari.maximumPoolSize=15
spring.datasource.hikari.connection-test-query=SELECT 1
### Mybatis-Plus Config
mybatis-plus.global-config.banner=false
# mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.logic-delete-field=delFlag
mybatis-plus.global-config.db-config.logic-delete-value=1

@ -0,0 +1,15 @@
,--, ,--, ,---._
,--.'| ,--.'| .-- -.' \ Hippo4J ${application.version}
,--, | : ,--, ,-.----. ,-.----. ,--, | : | | : Port: ${server.port}
,---.'| : ',--.'| \ / \ \ / \ ,---. ,---.'| : ' : ; | PID: ${pid}
| | : _' || |, | : || : | ' ,'\ ; : | | ; : | Console: http://127.0.0.1:${server.port}/index.html
: : |.' |`--'_ | | .\ :| | .\ : / / || | : _' | | : :
| ' ' ; :,' ,'| . : |: |. : |: |. ; ,. :: : |.' | : https://hippox.cn
' | .'. |' | | | | \ :| | \ :' | |: :| ' ' ; : | ; |
| | : | '| | : | : . || : . |' | .; :\ \ .'. | ___ l
' : | : ;' : |__ : |`-': |`-'| : | `---`: | ' / /\ J :
| | ' ,/ | | '.'|: : : : : : \ \ / ' ; |/ ../ `..- ,
; : ;--' ; : ;| | : | | : `----' | : ;\ \ ;
| ,/ | , / `---'.| `---'.| ' ,/ \ \ ,'
'---' ---`-' `---` `---` '--' "---....--'
Loading…
Cancel
Save