将springboot微服务部署在tomcat-8.5.23版本上。以下为修改配置信息。
1、引包pom.xml
<dependencies>
<!--修改包引用不包含tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--网上有说需要排除spring-boot-starter-tomcat,但我在tomcat-8.5.23下不排除也可以-->
<!--<exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-tomcat</artifactId>-->
<!-- </exclusion>-->
<!--</exclusions>-->
</dependency>
<!--war包部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<version>1.5.1.RELEASE</version>-->
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
<!--修改打包设置-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<skipTests>true</skipTests>
<!-- 打成war包时名子 -->
<warName>ROOT</warName>
<!-- mave时启用的main路径(因为其他包里面测试时也加了main函数) -->
<mainClass>${start-class}</mainClass>
<skip>true</skip><!-- 跳过测试 -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
2、修改启动类
package com.pengei;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
# 第一步:extends SpringBootServletInitializer
@SpringBootApplication
@EnableCaching
public class MyAPIApplication extends SpringBootServletInitializer {
# 第二步override configure方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyAPIApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(MyAPIApplication.class, args);
}
}
3、增加/webapp/WEB-INF/web.xml,内容如下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.pengei.MyAPIApplication</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>