如何将 Spring-Boot-Webflux 应用程序部署到 Tomcat 独立服务器?
How to deploy a Spring-Boot-Webflux application to Tomcat standalone server?
一个普通的 spring-web 应用程序可以作为 war
文件独立部署到 tomcat
,如下所示:
@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
问题:迁移到 spring-webflux
到 tomcat 后如何部署这样的应用程序?
To deploy as a WAR to any Servlet 3.1+ container, you can extend and
include AbstractReactiveWebInitializer in the WAR. That class wraps an
HttpHandler with ServletHttpHandlerAdapter and registers that as a
Servlet.
所以,但是没有示例如何。
我试过如下,出现异常:
@SpringBootApplication
public class MyApplication extends AbstractReactiveWebInitializer {
@Override
protected Class<?>[] getConfigClasses() {
return new Class[] {MyApplication.class};
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
结果:
MyApplication.java:13:8
java: cannot access javax.servlet.ServletException
class file for javax.servlet.ServletException not found
您应该遵循 Spring Boot Guide on Traditional Deployment。这解释了您需要 spring-boot-starter-tomcat
(因为这是您选择的服务器)所提供的范围。否则它可能会开始添加您不需要的其他 jar,这可能(并且可能会)干扰部署。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
您还可以 运行 生成的 WAR 文件作为常规应用程序。所以你可以构建 WAR 并只做 java -jar your.war
它就会开始。或者你可以只 运行 @SpringBootApplication
注释 class (尽管根据我的经验,如果你使用 JSP 可能无法 100% 工作)。
Spring 引导团队不支持此用例,as explained in the reference documentation. Even if some features might work, you'll find many limitations and bugs to this approach - and it seems you've started to experience just this。
一个普通的 spring-web 应用程序可以作为 war
文件独立部署到 tomcat
,如下所示:
@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
问题:迁移到 spring-webflux
到 tomcat 后如何部署这样的应用程序?
To deploy as a WAR to any Servlet 3.1+ container, you can extend and include AbstractReactiveWebInitializer in the WAR. That class wraps an HttpHandler with ServletHttpHandlerAdapter and registers that as a Servlet.
所以,但是没有示例如何。
我试过如下,出现异常:
@SpringBootApplication
public class MyApplication extends AbstractReactiveWebInitializer {
@Override
protected Class<?>[] getConfigClasses() {
return new Class[] {MyApplication.class};
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
结果:
MyApplication.java:13:8
java: cannot access javax.servlet.ServletException
class file for javax.servlet.ServletException not found
您应该遵循 Spring Boot Guide on Traditional Deployment。这解释了您需要 spring-boot-starter-tomcat
(因为这是您选择的服务器)所提供的范围。否则它可能会开始添加您不需要的其他 jar,这可能(并且可能会)干扰部署。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
您还可以 运行 生成的 WAR 文件作为常规应用程序。所以你可以构建 WAR 并只做 java -jar your.war
它就会开始。或者你可以只 运行 @SpringBootApplication
注释 class (尽管根据我的经验,如果你使用 JSP 可能无法 100% 工作)。
Spring 引导团队不支持此用例,as explained in the reference documentation. Even if some features might work, you'll find many limitations and bugs to this approach - and it seems you've started to experience just this。