Skip to content

Commit

Permalink
优化 Server 启动脚本日志输出. (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Feb 9, 2022
1 parent 77eb7e0 commit d32e1f4
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
2 changes: 1 addition & 1 deletion hippo4j-server/bin/startup.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion hippo4j-server/bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 starting,you can check the ${BASE_DIR}/logs/start.out"
echo "hippo4j is starting,you can check the ${BASE_DIR}/logs/start.out"
1 change: 1 addition & 0 deletions hippo4j-server/conf/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions hippo4j-server/conf/hippo4j-logback.xml
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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>
Expand All @@ -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>
15 changes: 15 additions & 0 deletions hippo4j-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.SpringApplicationRunListener=cn.hippo4j.server.listener.BaseSpringApplicationRunListener
1 change: 1 addition & 0 deletions hippo4j-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions hippo4j-server/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -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 :
| | ' ,/ | | '.'|: : : : : : \ \ / ' ; |/ ../ `..- ,
; : ;--' ; : ;| | : | | : `----' | : ;\ \ ;
| ,/ | , / `---'.| `---'.| ' ,/ \ \ ,'
'---' ---`-' `---` `---` '--' "---....--'

0 comments on commit d32e1f4

Please sign in to comment.