Process Monitoring

spring-boot 模块中,你可以找到两个类来创建通常对进程监视有用的文件:

In the spring-boot module, you can find two classes to create files that are often useful for process monitoring:

  • ApplicationPidFileWriter creates a file that contains the application PID (by default, in the application directory with a file name of application.pid).

  • WebServerPortFileWriter creates a file (or files) that contain the ports of the running web server (by default, in the application directory with a file name of application.port).

默认情况下,这些写入器尚未激活,但你可以启用它们:

By default, these writers are not activated, but you can enable them:

Extending Configuration

META-INF/spring.factories 文件中,你可以激活写入 PID 文件的侦听器(或多个侦听器):

In the META-INF/spring.factories file, you can activate the listener (or listeners) that writes a PID file:

org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriter,\
org.springframework.boot.web.context.WebServerPortFileWriter

Programmatically Enabling Process Monitoring

还可以通过调用 SpringApplication.addListeners(…​) 方法并传递适当的 Writer 对象来激活侦听器。此方法还可让你自定义 Writer 构造函数中的文件名和路径。

You can also activate a listener by invoking the SpringApplication.addListeners(…​) method and passing the appropriate Writer object. This method also lets you customize the file name and path in the Writer constructor.