0 x01. An overview

SpringBoot usually we use cool, cool to its own Tomcat integration, we can directly write SBT startup class, and then a key to open the built-in Tomcat container services, is really very easy to use. However, considering the actual situation, our Tomcat server is usually deployed separately and has a special maintenance mode. At this point, we need to strip the Tomcat server built into the SBT application, and then publish and deploy the application to an external Tomcat container. This article will do just that.

Note: This article was published on My public account CodeSheep. You can subscribe by holding down or scanning the heart below ↓ ↓ ↓


0x02. Change the packaging mode

Modify the pom.xml configuration of the project, we change its packaging method to WAR mode, such as:

< the groupId > com. Example < / groupId > < artifactId > demo < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <packaging>war</packaging>Copy the code

0x03. Remove embedded Tomcat from the SBT

Modify pom.xml to remove springBoot embedded Tomcat plug-in from Maven poM

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <! > <exclusions> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>Copy the code

0x04. Add a servlet-API dependency

Modify pom.xml to add servlet-API dependencies to Maven’s POM

< the dependency > < groupId > javax.mail. Servlet < / groupId > < artifactId > javax.mail. The servlet API - < / artifactId > < version > 3.1.0 < / version > <scope>provided</scope> </dependency>Copy the code

0x05. Modify the startup class and override the initialization method

In SpringBoot, we usually use the main method to boot, there is a SpringBootApplication startup class, similar to the following code:

@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code

And we need now is similar to the web. The XML configuration way to start a spring Application, therefore, we add a SpringBootStartApplication class at the same level in the Application class, the code is as follows:

// Modify the startup class, Inheritance SpringBootServletInitializer and rewrite the configure method public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {// Note that this must point to the Application launcher class that was originally executed with the main methodreturnbuilder.sources(Application.class); }}Copy the code

Deploy to an external Tomcat container and verify

  • In the project root directory (that is, containpom.xmlMaven package operation:
mvn clean package
Copy the code

After the package is complete, if [INFO] BUILD SUCCESS is displayed, the package is successful

  • And then we puttargetGenerated in the directorywarPackage into TomcatwebappsTomcat is automatically decompressed for deployment.

Finally verify in the browser:

http://YOUR_IP:[port number]/[Package project name]Copy the code

  • You can also name the project ROOT so that you can access the SpringBoot application in Tomcat by accessing the ROOT directory
http://YOUR_IP:[port number]Copy the code


Afterword.

  • The author’s more original articles are here, welcome to watch

  • My Personal Blog

The author has more SpringBt practice articles here:

  • Spring Boot application monitoring actual combat
  • The SpringBoot application is deployed in an external Tomcat container
  • ElasticSearch in SpringBt practice
  • A preliminary study on Kotlin+SpringBoot joint programming
  • Spring Boot Logging framework practices
  • SpringBoot elegant coding: Lombok plus

If you are interested, take some time to read some of the author’s articles on containerization and microservitization:

  • Use K8S technology stack to create personal private cloud serial articles
  • Nginx server configuration from a detailed configuration list
  • Docker container visual monitoring center was built
  • Use ELK to build Docker containerized application log center
  • RPC framework practice: Apache Thrift
  • RPC framework practice: Google gRPC
  • Establishment of microservice call chain tracking center
  • Docker containers communicate across hosts
  • Preliminary study on Docker Swarm cluster
  • Several guidelines for writing dockerFiles efficiently