Do you know how to automatically save Spring Boot application process number, please

1 Introduction

Welcome Spring Boot 2 combat series . PIDFor 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, PIDthe system recovery may be new to the program to continue running. Commonly known 进程号.
PIDIs 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

jpsIs 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 jpsto check the Java process ID applications.
Spring Boot application as a Java after the application starts a natural PIDprocess 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 PIDeasier 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 ApplicationPidFileWritercomplete. Roughly logic: listening event starts when the application starts, will PIDwrite to the specified file, by default application.pid; the default path is the current path. If the write to file fails, the PIDvalue 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 ApplicationPidFileWriterdoes 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.piddocuments, 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.fileto customize:

 spring:
   pid:
# 将 PID 写入 /var/run 路径下的 myApp.pid 文件中
    file: /var/run/myApp.pid

Restart, will /var/runfind the next myApp.pid.

4. Summary

Today we how to persist Spring Boot PID were explained. By programmatic configuration, you can Spring Boot application PIDpersistence to a file, and you can customize according to the needs PIDof stored files. Here today, if you feel good, please forward a praise and support about it.

关注公众号:Felordcn获取更多资讯

Personal blog: https: //felord.cn

Published 110 original articles · won praise 298 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_35067322/article/details/103175690