The correct posture for Spring Boot project deployment with external Tomcat should be like this

1. Configure three-step operation

 

1. Modify the packaging type of the project

<packaging>war</packaging>

2. Remove the embedded tomcat dependency (not required)

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<!-- scope设置为provided, 表明依赖由外部容器提供,在打包时会将该包排除 -->
	<scope>provided</scope>
</dependency>

After the packaged war package is decompressed, the scope provided dependency (here, embedded tomcat) is placed in the lib-provided directory instead of the lib directory

3. Inherit SpringBootServletInitializer and override the configure method

3.1. Method One

@RestController
@SpringBootApplication
public class TomcatDemoApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(TomcatDemoApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(TomcatDemoApplication.class);
	}

	@GetMapping("/hello")
	public String hello() {
		return "hello world";
	}
}

3.2. Method Two

@RestController
@SpringBootApplication
public class TomcatDemoApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(TomcatDemoApplication.class, args);
	}

	@GetMapping("/hello")
	public String hello() {
		return "hello world";
	}
}
public class ServletInitializer extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(TomcatDemoApplication.class);
	}
}

Two, package deployment steps

1. mvn package is packaged, and the package name defaults to:

<artifactId>-<version>.<packaging>

Configure for the following items:

<groupId>com.example</groupId>
<artifactId>tomcat-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

The package name after packaging is: tomcat-demo-0.0.1-SNAPSHOT.war

2. Put the finished war package in the webapp directory of the external tomcat and start bin/start.bat

3. Visit  http://localhost:8085/tomcat-demo-0.0.1-SNAPSHOT/hello , the page normally returns "hello world"

Three, matters needing attention

1. After deploying the project with external tomcat, some internal configurations of Spring Boot are invalid, for example:

server.port, server.servlet.context-path 等, 

因为这些配置都是针对内嵌tomcat容器的,要修改端口号和部署根路径的话,需要修改外部tomcat的相关配置

2. Change the default package name after packaging

<build>
	<finalName>tomcat-demo</finalName>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

After packaging like this, the package name is

<finalName>.war, 这里即为tomcat-demo.war

The access form is changed to: http://localhost:8085/tomcat-demo/hello

 

Guess you like

Origin blog.csdn.net/u014225733/article/details/97370817