Since SpringBoot is a microservice framework, its production deployment method needs to be as simple as possible, which is a huge difference from conventional web applications. It can embed a web container, such as Tomcat, Jetty, etc., and no longer need to package the application into a specific form specified by the container.
For SpringBoot, packaging it into a simple Jar package and directly using java -jar can be started. This is a very elegant way, but it also brings certain problems, such as: How to stop the application? In the past, applications were deployed in specific containers, and the scripts provided by the container could be elegantly discontinued, but now the container is embedded and the scripts are gone. What should I do? Direct killing is a way, but it seems too rude and can bring many unexpected problems.
Since we can think of problems, so will the frame makers, then have they prepared solutions for us? There are yes, and I will introduce some solutions I have learned below.
1. Use Endpoints
In Part 4 of SpringBoot's official documentation, various features prepared for application release production are introduced. Among them, through Actuator's HTTP Endpoint, developers can easily monitor and manage applications.
Introduce the specified starter package:
"org.springframework.boot:spring-boot-starter-actuator:${springbootVersion}"Open the following two configurations in application.yml to stop the application through Http request
management: security: enabled: falseendpoints: shutdown: enabled: true
The operation commands are as follows:
curl -X POST http://host:port/shutdown
But this method has a very serious problem, that is, anyone can control the stop of the application, which is undoubtedly unacceptable for a production application. Some people may think that the link address is too simple now, and non-maintainers can easily guess whether this problem can be avoided if a very complex address is used. Very good, this proposal is good, let's take a look at the relevant configurations provided by SpringBoot.
endpoints: shutdown: enabled: true path: /xxx
After the configuration is completed, the above command is no longer available. The command needs to be updated as:
curl -X POST http://host:port/xxx
Of course /xxx is just one I set casually, you can set any address. Although the security is a little bit higher, such a security level is still unavailable to be applied to the production environment. So are there other protective measures? Yes, in addition to modifying the shutdown path, we can also add a unified context to all management operations, configure independent ports, and restrict specified IP access (usually limited to native machine). The configuration is as follows:
management: security: enabled: false port: 9001 address: 127.0.0.1 context-path: /admin
The changed service discontinuation command is:
curl -X POST http://127.0.0.1:9001/admin/xxx
This is actually safe enough. In order to further ensure the security of the system, add a layer of HTTP Basic Auth.
Increase Security dependencies:
"org.springframework.boot:spring-boot-starter-security:${springbootVersion}"Modify the configuration file as follows:
endpoints: shutdown: enabled: true path: /xxxmanagement: security: enabled: true port: 9001 address: 127.0.0.1 context-path: /adminsecurity: basic: enabled: true path: /admin user: name: root password: 123456
After the configuration is completed, the final discontinuation command is:
curl -X POST -u root:123456 http://127.0.0.1:9001/admin/xxx
2. Register as a system service
In addition to running SpringBoot applications with java -jar, you can easily register as a Linux/Unix system service with init.d or systemd, which makes it very easy to install and manage SpringBoot applications in a production environment.
In the Maven project, in order to create a "fully executable" jar, the following plug-ins need to be introduced:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.2.RELEASE</version> <configuration> <executable>true</executable> </configuration></plugin>
In the Gradle project, the equivalent configuration is as follows:
plugins { id 'org.springframework.boot' version '1.5.2.RELEASE'}springBoot { execute = true}After the configuration is completed, you can run the built application through ./application-name.jar.
Finally, we need to install the packaged application into an init.d service, so that it can be easily managed using Unix/Linux. The operation method is very simple. You just need to simply link the application to init.d (where funda is my own application name, and you need to replace it according to the situation when experimenting).
ln -s /app/funda/funda.jar /etc/init.d/funda
Check if the link is successfully established
ls -l /etc/init.d/funda
Start the service, and the application log can view the file /var/log/funda.log
service funda start
Other common commands
# Check the application running status service funda status# Stop the application service funda stop
Summary of questions:
After the link is successful, the application cannot be successfully started. It is prompted to Unable to find Java. Use the following command to link the Jdk java command to /sbin/java.
ln -s /usr/local/jdk1.8.0_131/bin/java /sbin/java
Reference link:
Endpoints
Monitoring and management over HTTP
Unix/Linux services
Unable to find Java #5690
Project link:
Github: https://github.com/qchery/funda
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.