阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Spring Boot项目在外部Tomcat环境下部署

157次阅读
没有评论

共计 4206 个字符,预计需要花费 11 分钟才能阅读完成。

Spring Boot 默认提供内嵌的 Tomcat,所以打包直接生成 jar 包,用 Java -jar 命令就可以启动。但是,有时候我们更希望一个 Tomcat 来管理多个项目,这种情况下就需要项目是 war 格式的包而不是 jar 格式的包。Spring Boot 同样提供了解决方案,只需要简单的几步更改就可以了,这里提供 Maven 项目的解决方法:

1. 将项目的启动类 Application.java 继承 SpringBootServletInitializer 并重写 configure 方法

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);
    }

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

}

2. 在 pom.xml 文件中,project 下面增加 package 标签

<packaging>war</packaging>

3. 还是在 pom.xml 文件中,dependencies 下面添加

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
</dependency>

这样,只需要以上 3 步就可以打包成 war 包,并且部署到 tomcat 中了。需要注意的是这样部署的 request url 需要在端口后加上项目的名字才能正常访问。spring-boot 更加强大的一点就是:即便项目是以上配置,依然可以用内嵌的 tomcat 来调试,启动命令和以前没变,还是:mvn spring-boot:run。

如果需要在 springboot 中加上 request 前缀,需要在 application.properties 中添加 server.contextPath=/prefix/ 即可。其中 prefix 为前缀名。这个前缀会在 war 包中失效,取而代之的是 war 包名称,如果 war 包名称和 prefix 相同的话,那么调试环境和正式部署环境就是一个 request 地址了。

一般这样配置都不会出现问题,但有些时候,还是会报错,那是因为 tomcat 运行版本不同:

例如我在 tomcat8.0.30 版本下,配置如下:

<!– 打 war 包时加入此项,告诉 spring-boot tomcat 相关 jar 包用外部的,不要打进去 –>
  <!– https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.4.0.RELEASE</version>
<scope>provided</scope>
</dependency>

这样配置在外部 tomcat 是 8.0.30 版本下正常运行,但是我的服务器是 7.0.69 就会报错:

org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/zhaolixiang]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:4088)
    at org.apache.catalina.startup.HostConfig.reload(HostConfig.java:1539)
    at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1512)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1748)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:333)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1371)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1543)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1553)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1521)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
    at org.springframework.boot.context.web.SpringBootServletInitializer.createSpringApplicationBuilder(SpringBootServletInitializer.java:140)
    at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:104)
    at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:85)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5573)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    … 12 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
    … 18 more

解决版本,修改代码:

<!– 打 war 包时加入此项,告诉 spring-boot tomcat 相关 jar 包用外部的,不要打进去 –>
  <!– https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.3.0.RELEASE</version>
<scope>provided</scope>
</dependency>

就成功了!!

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-10/147868.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4206字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中