Spring 使用 Tomcat 启动部署

Spring boot deployement with Tomcat

我目前正在部署一个应用程序,但由于某种原因,它似乎无法正常工作。

在编码时,我是 running/debugging 使用 Intellij Spring Boot 运行ner 的项目,易于配置。

因为我需要在 Tomcat 服务器上部署,所以我尝试先在 Intellij 中设置一个 Tomcat Server Runner,然后 运行 从我的 war Tomcat 直接服务器。

问题是:运行 似乎无法正常工作。事实上,即使部署没有 return 任何错误,应用程序也没有完全启动。

这是一个 Spring 引导 运行ner 输出:

2020-02-19 11:50:18.373 DEBUG 1192 --- [  restartedMain] com.myapp.Application              : Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
2020-02-19 11:50:18.374  INFO 1192 --- [  restartedMain] com.myapp.Application              : No active profile set, falling back to default profiles: default
2020-02-19 11:50:18.421  INFO 1192 --- [  restartedMain] o.s.b.devtools.restart.ChangeableUrls    : The Class-Path manifest attribute in C:\Users\user\.m2\repository\org\glassfish\jaxb\jaxb-runtime.3.2\jaxb-runtime-2.3.2.jar referenced one or more files that do not exist: file:/C:/Users/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.xml.bind-api-2.3.2.jar,file:/C:/Users/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/txw2-2.3.2.jar,file:/C:/Users/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/istack-commons-runtime-3.0.8.jar,file:/C:/Users/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/stax-ex-1.8.1.jar,file:/C:/Users/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/FastInfoset-1.2.16.jar,file:/C:/Users/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.activation-api-1.2.1.jar
2020-02-19 11:50:18.422  INFO 1192 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-02-19 11:50:18.422  INFO 1192 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-02-19 11:50:19.223  INFO 1192 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-02-19 11:50:19.288  INFO 1192 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 57ms. Found 1 JPA repository interfaces.
2020-02-19 11:50:19.807  INFO 1192 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-02-19 11:50:20.115  INFO 1192 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2020-02-19 11:50:20.125  INFO 1192 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-02-19 11:50:20.125  INFO 1192 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-02-19 11:50:20.233  INFO 1192 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
etc.
2020-02-19 11:50:24.396  INFO 1192 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-19 11:50:24.399  INFO 1192 --- [  restartedMain] com.myapp.Application              : Started Application in 6.531 seconds (JVM running for 7.815)

还有一个 Tomcat 运行 输出:

2020-02-19 12:14:15.753  INFO 4436 --- [on(3)-127.0.0.1] com.myapp.Application              : Starting Application on UO with PID 4436 (C:\App\apache-tomcat-8.0.47\webapps\server_war\WEB-INF\classes started by user in C:\App\apache-tomcat-8.0.47\bin)
2020-02-19 12:14:15.757 DEBUG 4436 --- [on(3)-127.0.0.1] com.myapp.Application              : Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
2020-02-19 12:14:15.760  INFO 4436 --- [on(3)-127.0.0.1] com.myapp.Application              : No active profile set, falling back to default profiles: default
2020-02-19 12:14:15.937  INFO 4436 --- [on(3)-127.0.0.1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 113 ms
2020-02-19 12:14:15.994  INFO 4436 --- [on(3)-127.0.0.1] com.myapp.Application              : Started Application in 1.045 seconds (JVM running for 7.228)
[2020-02-19 12:14:16,029] Artifact server:war: Artifact is deployed successfully
[2020-02-19 12:14:16,029] Artifact server:war: Deploy took 5,204 milliseconds

如您所见,Spring 已正确启动,但似乎并未执行所有启动任务。

我的 pom 中已经提供了 spring-boot-starter-tomcat,这是我的 Application.class:

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

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

我的问题是:我是否遗漏了配置中的某些内容?如何 运行 使用多个配置文件 Tomcat 正确地 运行 一切(我目前在 运行ner 中使用 VM 选项)?

Spring Boot Framework本身封装了一个集成的Tomcat方便实现而不需要像传统方式那样生成一个.war然后自己实现Tomcat ,例如,简单地使用 Maven 插件并执行它,您将不得不使用 mvn spring-boot : 运行.

但是,如果这是您想要的方式,您必须使用 Maven 从 Spring 引导项目生成 war,然后将 war 添加到 tomcat webapps 文件夹,现在您可以在本地启动 tomcat 进行测试。 按照文档中包含的步骤进行操作,您应该不会遇到问题。

https://www.baeldung.com/spring-boot-war-tomcat-deploy

要为部署配置不同的配置文件,您必须配置 Tomcat catalina.properties 文件,以免重复太多,请查看以下主题:

希望对您有所帮助!

你能展示一下你的 pom.xml 吗?尝试添加

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

要在启动时获得更详细的输出,请尝试设置日志记录属性 https://www.javadevjournal.com/spring-boot/logging-application-properties/

除此之外 Tomcat 运行s 是一个 "apache-tomcat-8.0.47" 和你的 servlet 引擎 运行s 在 Apache Tomcat/9.0.30

如果你的 parent-pom 在 2.2.4 RELEASE 上,你的 pom 将依赖于 Tomcat 9 的东西。 Spring Boot 也会为你设置 Tomcat(因此你的依赖项)至少如果你 运行 它在你的本地机器上作为 Spring Boot app.

我找到了问题的解决方案,不知何故(可能 copy/pasted 来自前面的其他依赖声明)我设置了 spring-boot-autoconfigure 依赖提供,但是我的 tomcat没有这种依赖。所以我只需要删除提供的范围,一切都已修复。