Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to stop SpringBoot applications safely

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to stop SpringBoot applications safely". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to stop SpringBoot applications safely.

There are two main ways: to send shutdown signals through HTTP, or through service stop

Method 1: send shutdown signal through HTTP

This method mainly depends on the endpoint feature of Spring Boot Actuator. The specific steps are as follows:

1. Introducing actuator dependency into pom.xml

Org.springframework.boot

Spring-boot-starter-actuator

two。 Turn on shutdown endpoint

Shutdown endpoint for Spring Boot Actuator is off by default, so turn on shutdown endpoint in application.properties:

# enable shutdown

Endpoints.shutdown.enabled=true

# disable password authentication

Endpoints.shutdown.sensitive=false

3. Send shutdown signal

The default url of shutdown is host:port/shutdown. When you need to stop the service, you can post the request to the server. For example, curl-X POST host:port/shutdown will get a response like {"message": "Shutting down, bye..."}.

4. Security settin

As you can see, it is very convenient to operate remotely by using this method, but it should be noted that when it is officially used, necessary security settings must be made for the request, such as identity authentication with spring-boot-starter-security:

Pom.xml add security dependency

Org.springframework.boot spring-boot-starter-security

Turn on security verification to change the configuration in application.properties, and

# enable shutdown's security authentication endpoints.shutdown.sensitive=true # verify username security.user.name=admin # verify password security.user.password=secret # role management.security.role=SUPERUSER

Specify path, IP, port

# specify the path of shutdown endpoint endpoints.shutdown.path=/custompath # you can also specify the path of all endpoints uniformly-management.context-path=/manage-- # specify the management port and IP management.port=8081 management.address=127.0.0.1

Method 2: deploy as Unix/Linux Service

This method mainly uses the official spring-boot-maven-plugin to create a "Fully executable" jar, in which a shell script is included in the jar package, which makes it easy to set the application as Unix/Linux 's system service (init.d service). The official test of this function has been carried out in CentOS and Ubuntu. For OS X and FreeBSD, you may need to customize it. The specific steps are as follows:

1. Introduce plug-ins into pom.xml:

Org.springframework.boot

Spring-boot-maven-plugin

True

E >

two。 Set as system service

Package your application into a jar package and deploy it to the server. Assuming that the deployment path is / var/app and the package name is app.jar, it should be set as a system service in the following way: sudo ln-s / var/app/app.jar / etc/init.d/app

3. Grant executable permissions:

Chmod upright x app.jar

4. Manage as a system service

Next, we can use the familiar service foo start | stop | restart to start and stop the application and manage the sudo service app start | the stop command will get a result feedback such as Started | Stopped [PID]

Default PID file path: / var/run/appname/appname.pid default log file path: / var/log/appname.log

This may be a more familiar and commonly used way of management.

Custom parameter

In this way, we can also use a custom .conf file to change the default configuration as follows:

Create a .conf file under the same path as the jar package with the same name as .jar, such as appname.conf

Configure relevant variables, such as JAVA_HOME=/usr/local/jdk JAVA_OPTS=-Xmx1024M LOG_FOLDER=/custom/log

Security settin

As an application service, security is an issue that cannot be ignored. The following operations can be used as a reference for some basic settings:

Create a separate user for the service, and it is best to bind the user's shell to / usr/sbin/nologin

Grant minimum scope permissions: chmod 500app.jar

Block modification: sudo chattr + I app.jar

Do similar work with .conf files: chmod 400app.conf,sudo chown root:root app.conf

At this point, I believe you have a deeper understanding of "how to safely stop SpringBoot applications". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report