部署到Tomcat的Spring Boot War

部署到Tomcat的Spring Boot War,spring,maven,tomcat,spring-boot,Spring,Maven,Tomcat,Spring Boot,我正在尝试将Spring Boot应用程序部署到Tomcat,因为我想部署到AWS。 我创建了一个WAR文件,但它似乎没有在Tomcat上运行,即使它是可见的 详细信息: 0这是我的应用程序: @Configuration @ComponentScan @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(Samp

我正在尝试将Spring Boot应用程序部署到Tomcat,因为我想部署到AWS。 我创建了一个WAR文件,但它似乎没有在Tomcat上运行,即使它是可见的

详细信息:
0这是我的应用程序:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class App {
    public static void main(String[] args) {
        SpringApplication.run(SampleController.class, args);
    }
}

@Controller
@EnableAutoConfiguration
public class SampleController {
    @RequestMapping("/help")
    @ResponseBody
    String home() {
        String input = "Hi! Please use 'tag','check' and 'close' resources.";
        return input;
    }
}
application.properties具有以下特性:

server.port=${port:7777}
  • 在阅读了一些文章之后,我在POM中添加了以下内容:

    http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

    <groupId>com.niewlabs</groupId>
    <artifactId>highlighter</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <packaging>war</packaging>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.9.RELEASE</version>
    </parent>    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    com.niewlabs
    
    。不知道它有多有用。

    我想你被这里的不同范例弄糊涂了。首先,war文件和服务器部署——这些东西属于Java Enterprise Edition(Java EE)。这些概念在遵循不同模型的spring boot应用程序中没有实际的位置

    SpringBoot负责创建一个嵌入式容器,并直接从标准jar文件在其中运行您的服务(尽管它可以做更多)。我认为此模型的目的是支持微服务开发,其中每个服务都有自己的容器,并且完全独立。您也可以使用代码生成Java EE应用程序,但考虑到spring启动要容易得多(对于某些类型的应用程序/服务),这将是愚蠢的

    所以,根据这些信息,你现在必须决定你要遵循什么样的范式,你需要遵循那个范式,而且只需要遵循那个范式

    Spring boot是可执行的——您只需在App类中运行main方法,您可以从命令行或使用您最喜欢的IDE、maven或gradle(提示:maven是正确的答案)。这将启动tomcat服务器(默认情况下)您的服务将在其中可用。根据您在上面发布的配置,您的服务应在以下位置可用:
    http://localhost:7777/context/help
    ——上下文将被替换为您尚未共享的上下文名称

    您不应该创建war、运行tomcat或部署任何东西。这些在spring boot中都是不必要的。pom中的包装应该是
    jar
    ,而不是
    war
    ,并且
    spring boot starter tomcat
    范围应该被删除——当然没有提供

    当您运行main方法时,控制台输出应该告诉您已注册的上下文;使用它可以正确获取URL


    话虽如此,spring boot现在必须存在于JEE世界中(直到它被广泛采用)出于这个原因,spring人员已经记录了一种构建war而不是可执行jar的方法,用于部署到servlet或JEE容器。这使得许多spring引导技术可以用于除war(或EAR)之外的任何限制使用的环境中但是,这只是对这样一个事实的回应,即这种环境非常普遍,并且不被视为解决方案的必要部分,甚至不可取部分。

    本指南详细解释了如何在Tomcat上部署Spring Boot应用程序:

    基本上,我需要添加以下类:

    public class WebInitializer extends SpringBootServletInitializer {   
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(App.class);
        }    
    }
    
    我还向POM添加了以下属性:

    <properties>        
        <start-class>mypackage.App</start-class>
    </properties>
    
    
    mypackage.App
    
    如果您的目标是将Spring Boot应用程序部署到AWS,则为您提供了一个非常简单的解决方案

    您需要做的只是:

    boxfuse run my-spring-boot-app-1.0.jar -env=prod
    
    这将:

    • 融合为您的应用程序量身定制的最小操作系统映像(比典型的Linux发行版小约100倍)
    • 将其推送到安全的在线存储库
    • 在大约30秒内将其转换为AMI
    • 创建并配置新的弹性IP或ELB
    • 给它分配一个新域名
    • 基于新AMI启动一个或多个实例
    所有映像都是在几秒钟内生成的,并且是不可变的。它们可以在VirtualBox(dev)和AWS(test&prod)上不加更改地运行

    所有更新都以零停机蓝/绿部署方式执行,您也可以只需一个命令即可启用自动缩放

    Boxfuse还了解,Spring引导配置将根据应用程序的属性自动配置安全组和ELB运行状况检查

    以下是帮助您入门的教程:

    免责声明:我是Boxfuse的创始人和首席执行官。在遵循指南(或使用Spring初始值设定)后,我有一个WAR可以在本地计算机上运行,但不能远程运行(在Tomcat上运行)

    并没有错误消息,只是说“找到了SpringServlet初始值设定项”,但根本并没有做任何事情

    17-Aug-2016 16:58:13.552 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.4
    17-Aug-2016 16:58:13.593 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /opt/tomcat/webapps/ROOT.war
    17-Aug-2016 16:58:16.243 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    

    没有别的事发生,只是弹簧靴没跑

    显然,我用Java1.8编译服务器,而远程计算机使用Java1.7

    在使用Java1.7编译之后,它开始工作

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version> <!-- added this line -->
        <start-class>myapp.SpringApplication</start-class>
    </properties>
    
    
    UTF-8
    UTF-8
    1.7
    myapp.SpringApplication
    
    您的
    应用程序。java
    类应该扩展
    SpringBootServletilizer
    类 例:


    公共类应用程序扩展了SpringBootServletInitializer{}


    只需扩展SpringBootServletializer。它将在您的AWS/tomcat中工作。请确保对pom.xml进行此更改

    <packaging>war</packaging>
    
    您可以添加一个控制器来测试MyController.java

    package com.example;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/hi")
        public @ResponseBody String hiThere(){
            return "hello world!";
        }
    }
    
    然后您可以在Tomcat8版本中运行该项目,并像这样访问控制器

    如果由于某种原因无法将项目添加到tomcat,请在项目中单击鼠标右键,然后转到构建路径->配置构建路径->项目面

    确保仅选择了这3个选项

    动态web模块3.1 Java 1.8
    JavaScript1.0

    我也遇到了同样的问题,我通过以下步骤找到了解决方案

    清洁包装

    对于使用Gradle的人来说,这是一个有效的解决方案 将插件添加到
    build.gradle

    apply plugin: 'war'
    
    提供的依赖项添加到tomcat

    dependencies {
        // other dependencies
        providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    }
    

    使用弹簧靴1.5.8.0版本更新2018-02-03

    在pom.xml中,您需要告诉Spring插件w
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <start-class>com.example.Application</start-class>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>       
    
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>       
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    
    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {
    
    
        /**
         * Used when run as JAR
         */
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        /**
         * Used when run as WAR
         */
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(Application.class);
        }
    
    }
    
    package com.example;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/hi")
        public @ResponseBody String hiThere(){
            return "hello world!";
        }
    }
    
    apply plugin: 'war'
    
    dependencies {
        // other dependencies
        providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    }
    
    <packaging>war</packaging>
    
        <!-- to deploy as a war in tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    
    14-Apr-2021 12:38:01.996 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
    14-Apr-2021 12:38:01.996 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.5]
    14-Apr-2021 12:38:02.023 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\dev\apache-tomcat-10.0.5\webapps\server-test-1-0.0.1-SNAPSHOT.war]
    14-Apr-2021 12:38:05.349 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    14-Apr-2021 12:38:05.436 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\dev\apache-tomcat-10.0.5\webapps\server-test-1-0.0.1-SNAPSHOT.war] has finished in [3,413] ms