Preface: Previously used Java-jar run, but the deployment of the time to stop the service through the port kill, recently on the push cool found a security shutdown Springboot blog, so I tidy up (handling) a bit.
Mode one: Start shutdown via shell command 1.1 background run
Nohup java-jar file name. jar 2>&1 &
1.2 Closing the service
#!/bin/Bashpid=$(PS-ef |grepFile name. jar |grep-Vgrep|awk '{print $}')if[-Z"$PID" ] Then EchoService is downElse EchoClose the service $PIDKill$PIDfi
Mode two: Send shutdown signal via HTTP
This approach relies primarily on the endpoint features of the Spring Boot actuator, as follows:
2.1 Introduction of actuator dependency in Pom.xml
< Dependency > < groupId >org.springframework.boot</groupId> < Artifactid>spring-boot-starter-actuator</artifactid> </dependency>
2.2 Opening shutdown Endpoint
Spring Boot Actuator shutdown endpoin T is turned off by default, so shutdown endpoint is turned on in application.properties:
< Dependency > < groupId >org.springframework.boot</groupId> < Artifactid>spring-boot-starter-actuator</artifactid> </dependency>
2.3 Send shutdown signal
The default URL for shutdown is Host:port/shutdown, and when you need to stop the service, post the request to the server, such as:
Curl-x POST Host:port/shutdown
Will get the response of the form {"message": "Shutting down, bye ..."}
2.4 Security Settings
It can be seen that using this method can be very convenient for remote operation, but it is important to note that, when formally used, the request must be made necessary security settings, such as the use of spring-boot-starter-security for identity authentication:
Pom.xml Adding security dependencies
< Dependency > < groupId >org.springframework.boot</groupId> < Artifactid>spring-boot-starter-security</artifactid> </dependency>
Turn on security verification
Change the configuration in Application.properties, and
#开启shutdown的安全验证
Endpoints.shutdown.sensitive=true
#验证用户名
Security.user.name=admin
#验证密码
Security.user.password=secret
#角色
Management.security.role=superuser
Method Three: Deploy as Unix/linux Service
This approach is based primarily on the official spring-boot-maven-plugin to create the "Fully executable" jar, which contains a shell script that makes it easy to set the app as Unix/linux system service (INIT.D Service), which is officially tested on CentOS and Ubuntu, may need to be customized for OS X and FreeBSD. The steps are as follows:
3.1 Introduction of plugins in Pom.xml
<plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> <Configuration> <executable>True</executable> </Configuration></plugin>
3.2 Set As System service
Make your app into a jar package, deploy it to a server, assuming the deployment path is/var/app and the package name is App.jar, you should set it as a system service as follows:
sudo LN -s/var/app/app.jar/etc/init.d/app
3.3 Give executable permissions:
chmod u+x App.jar
3.4 Managed in a system service manner
Sudo
Next, you can use our familiar service Foo Start|stop|restart to manage the start and stop of your application.
Commands will be shaped like started| Results feedback of Stopped [PID]
Default PID file path:/var/run/appname/appname.pid default log file path:/var/log/appname.log
This is probably a more familiar and more common way of managing.
Custom parameters
In this way, we can also use the custom. conf file to change the default configuration, as follows:
Create a. conf file under the same path as the jar package, and the name should be the same as the name of the. jar, such as appname.conf
In which the related variables are configured, such as:
Java_home=/usr/local/jdk
java_opts=-xmx1024m
Log_folder=/custom/log
Security settings
As an application service, security is a problem that cannot be ignored, and some of the following actions can be referenced as part of the underlying settings:
Create a separate user for the service, and preferably bind the user's shell to/usr/sbin/nologin
Give minimum scope permission: chmod App.jar
Block modification: sudo chattr +i App.jar
Do similar things to. conf files: chmod app.conf, sudo chown root:root a
Two ways, I've got it all,
The first type of HTTP I found in Jenkins want to batch to stop the service and start a bit of trouble, if the direct execution of the curl-x POST host:port/shutdown command, security is problematic, so you have to integrate security, you need to verify the pass to execute. Manually stopping before each deployment feels like a superfluous, so I chose the second way.
The second way the words need to be set <configuration><executable>true</executable></configuration> In this link incredibly with my integrated mybatis (I just need to write a relative class name on the line, do not need to write the full path, easy to change the entity class after the package path), so it took a good freshman time to modify all the XML package path
Original link https://my.oschina.net/wangnian/blog/714719
Springboot deployment and MAVEN package switching environment