Spring Boot uses Tomcat as the embedded server by default. So, it will be very easy for us to build a Web project.

Embedded Tomcat, a Jar package to run

Remember the example from Spring Boot: Getting Started? So let’s go back. First, modify the POM file to add dependencies.

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

Creating Java code

@RestController
@EnableAutoConfiguration
public class RestfulApiWebDemo {
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) throws Exception { SpringApplication.run(RestfulApiWebDemo.class, args); }}Copy the code

Run the Java classes directly, or you can run the MVN spring-boot:run command to start the application. An embedded Tomcat server is started running on port 8080. Visit http://localhost:8080 and you can see Hello World! .

In addition, add plug-ins to POM files.

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>Copy the code

After the plug-in is added, when you run “MVN Package” to package it, it will be packaged into a JAR file that you can run directly using the “java-jar” command.

With the Tomcat server embedded in Spring Boot, we can run a Web project from a JAR package, which is also very suitable for the microservices scenario, we only need to deploy a JAR in the formal environment, of course, We can also deploy the JAR to run in a Docker container, wouldn’t it be convenient?

How to customize embedded Tomcat

Set the port for embedded Tomcat

The Tomcat server embedded in Spring Boot runs on port 8080 by default. If, we need to modify Tomcat port, we can in the SRC/main/resources/application. The properties in the Tomcat configuration information.

server.port=8089Copy the code

Now, you can rerun the above example to see if the Tomcat port has changed to 8089.

Set the maximum number of threads for embedding Tomcat

We can also change the maximum number of threads for the embedded Tomcat server.

server.tomcat.max-threads=1000Copy the code

Set the encoding for embedded Tomcat

In some scenarios, we may need to change the encoding of Tomcat, such as whether it is GBK or UTF-8.

server.tomcat.uri-encoding = UTF- 8 -Copy the code

Common configuration parameters provided by officials

In addition to the three scenarios mentioned above, you can also customize the path address, SSL, and other parameters.

This section lists some common configuration parameters provided by the government. You can customize the embedded Tomcat based on specific requirements.

server.tomcat.accesslog.enabled=false # Enable access log. server.tomcat.accesslog.pattern=common # Format pattern for access logs. server.tomcat.accesslog.prefix=access_log # Log file name prefix. server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time. server.tomcat.accesslog.suffix=.log # Log file name suffix. server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods. server.tomcat.basedir= # Tomcat base directory. If not Specified a temporary directory will be 2. Server. Tomcat. Internal - proxies = 10. \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} | \ \ 192 \ \ 168 \ \. \ \ d {1, 3} \ \ \ \ d {1, 3} | \ \ 169 \ \ 254 \ \. \ \ d {1, 3} \ \ \ \ d {1, 3} | \ \ 127 \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} | \ \ 172 \ \. 1 [6-9] {1} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} | \ \ 172 \ \. 2 [0-9] {1} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} | \ \ 172 \ \. 3 [0, 1] {1} \ \ \ \ d {1, 3} \ \. \ \ d {1, 3} # regular expression matching trusted IP addresses. server.tomcat.max-threads=0 # Maximum amount of worker threads. server.tomcat.min-spare-threads=0 # Minimum amount of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value. server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto". server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL. server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path. server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR` server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.Copy the code

Details of the use of War package deployment

If you still want to deploy to an external Tomcat server using a WAR package, Spring Boot is also supported, but some additional configuration is required.

First, change the final packaging form to a WAR package, so change the packaging value to WAR.

<packaging>war</packaging>Copy the code

Next, configure the dependencies appropriately, noting that the dependency on embedded Tomcat needs to be removed here so that the WAR package does not contain Tomcat related JARS in the lib directory.

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

In addition, to ensure correct compilation, you need to add dependencies on servlet-API, so add the following configuration.

< the dependency > < groupId > org, apache tomcat < / groupId > < artifactId > tomcat servlet - API < / artifactId > < version > 7.0.42 < / version > <scope>provided</scope> </dependency>Copy the code

Set, packaged project access name, add content to the node.

<build>
  <finalName>springboot-web-demo</finalName>
</bulid>Copy the code

If we want a WAR package to be deployed on an external Tomcat server, instead of relying on the main function of RestfulApiWebDemo, we need to start the Spring application context in a manner similar to the configuration of the web.xml file. At this point we need to declare a class, The purpose of this class is similar to that of configuring the listener in web.xml that initializes the Spring application context, except that there is no need to write additional XML files.

public class ServletInitializer extends SpringBootServletInitializer {   
    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        returnapplication.sources(RestfulApiWebDemo.class); }}Copy the code

Now that we’re done, we can package a WAR package using Maven’s “MVN Clean Package” and deploy it to an external Tomcat server to run. To access the “http://localhost:8080/springboot-web-demo/” can see page shows “Hello World!” .

conclusion

Spring Boot uses Tomcat as the embedded server by default. Therefore, it will be very easy for us to build a Web project and only need a JAR package to run. In addition, we can make some customizations to the embedded Tomcat, such as ports, maximum threads, encoding, SSL, and so on. If you still want to deploy to an external Tomcat server using a WAR package, Spring Boot also supports this, but some additional configuration is required, and this configuration process can be implemented in a few simple steps.

The source code

Related example full code: springboot-action

Call for articles on mining technology

Gold digging technology essay: juejin.cn/post/684490…

(after)

More wonderful articles, all in the “server-side thinking” wechat public account!