1 Introduction
Welcome Spring Boot 2 combat series . PID
For operation and maintenance system is not new, but for some developers, particularly novices, or to the brief. It is the Process ID for short, is the unique identifier assigned to a process system, is an identifier of each process, the system will automatically run a program assigned to a unique process PID
. After the termination of the process, PID
the system recovery may be new to the program to continue running. Commonly known 进程号
.
PID
Is an important parameter of our system process management, operation and maintenance is important to identify. For example, we used kill -9 <pid>
.
2. Spring Boot application process
jps
Is Java 's own view Java processes the command, you can view all currently running system through the command Java process, Java package names, JAR package name and JVM parameters. For details, please refer to the relevant official documents . Usually we will use jps
to check the Java process ID applications.
Spring Boot application as a Java after the application starts a natural PID
process ID. Normally we will start after the log to see it in:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)
2019-11-20 14:28:00.925 INFO 7828 --- [ main] c.f.s.s.SecurityLearningApplication : Starting SecurityLearningApplication on DESKTOP-L0IOI2S with PID 7828
When using multiple Spring Boot application, it is difficult to identify Spring Boot application PID, after persistent PID
easier to manage our Spring Boot application.
3. Spring Boot PID applications written to the file
Spring Boot provides a method when the application starts PID applications written to the file, the specific functions performed by ApplicationPidFileWriter
complete. Roughly logic: listening event starts when the application starts, will PID
write to the specified file, by default application.pid
; the default path is the current path. If the write to file fails, the PID
value is written to the system environment variable properties PID_FAIL_ON_WRITE_ERROR
(case insensitive), or write Spring environment variable properties spring.pid.fail-on-write-error
.
3.1 Configuring Spring Boot PID persistence function
By default ApplicationPidFileWriter
does not automatically configured, we need to configure itself. We can Spring Boot configure listener registered in accordance with the following entry template class ApplicationPidFileWriter
:
package cn.felord.spring.security;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.cache.annotation.EnableCaching;
/**
* @author Felordcn
*/
@SpringBootApplication
public class SecurityLearningApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SecurityLearningApplication.class);
springApplication.addListeners(new ApplicationPidFileWriter());
springApplication.run(args);
}
}
After doing the above configuration, it will start to generate application.pid
documents, which will be there PID
. Sometimes you may want to customize the name and path of the file. You can configure the properties of Spring Boot spring.pid.file
to customize:
spring:
pid:
# 将 PID 写入 /var/run 路径下的 myApp.pid 文件中
file: /var/run/myApp.pid
Restart, will /var/run
find the next myApp.pid
.
4. Summary
Today we how to persist Spring Boot PID
were explained. By programmatic configuration, you can Spring Boot application PID
persistence to a file, and you can customize according to the needs PID
of stored files. Here today, if you feel good, please forward a praise and support about it.
关注公众号:Felordcn获取更多资讯