0

相关项目在这里(实际代码来自 fixThisSpring 分支): https ://github.com/sekassel/CoronaTrackerEsp32/tree/master/server

类似的问题,但没有为我找到解决方案: ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

简而言之,主要是:

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

我用 Springboot 构建了一个小型 Vaadin 应用程序,它可以在本地运行。如果我尝试构建 Dockerfile 并运行它,它会崩溃:

19.2.2021 13:14:30 __  __          ____                  _              _
19.2.2021 13:14:30|  \/  | _   _  |  _ \  _ __   ___    (_)  ___   ___ | |_
19.2.2021 13:14:30| |\/| || | | | | |_) || '__| / _ \   | | / _ \ / __|| __|
19.2.2021 13:14:30| |  | || |_| | |  __/ | |   | (_) |  | ||  __/| (__ | |_
19.2.2021 13:14:30|_|  |_| \__, | |_|    |_|    \___/  _/ | \___| \___| \__|
19.2.2021 13:14:30         |___/                      |__/
19.2.2021 13:14:30
19.2.2021 13:14:30[pool-2-thread-1] INFO org.springframework.boot.SpringApplication - Starting application using Java 12-ea on accda21c508b with PID 1 (started by root in /server)
19.2.2021 13:14:30[pool-2-thread-1] INFO org.springframework.boot.SpringApplication - No active profile set, falling back to default profiles: default
19.2.2021 13:14:30[pool-2-thread-1] WARN org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
19.2.2021 13:14:30[pool-2-thread-1] ERROR org.springframework.boot.SpringApplication - Application run failed
19.2.2021 13:14:30org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

我猜这将是依赖相关的东西?所以这里的build.gradle:

plugins {
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'com.vaadin' version '0.14.3.7'
    id 'java'
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

group 'projekt'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

ext {
    set('vaadinVersion', "14.4.6")
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile "com.sparkjava:spark-core:2.8.0"
    compile 'org.slf4j:slf4j-simple:1.7.21'
    compile 'org.json:json:20200518'
    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.8'
    compile 'com.fasterxml.jackson.core:jackson-core:2.11.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
    compile 'com.google.protobuf:protobuf-java:3.12.2'
    compile 'at.favre.lib:hkdf:1.1.0'
    compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.32.3.2'

    // Vaadin
    implementation 'com.vaadin:vaadin-spring-boot-starter'
    implementation 'com.vaadin:vaadin-avatar-flow:1.0.1'
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
    }
}


dependencyManagement {
    imports {
        mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
    }
}

test {
    useJUnitPlatform()
}

bootJar {
    mainClassName = 'Main'
}

jar {
    manifest {
        attributes 'Main-Class': 'Main'
    }
}

和 Dockerfile:

FROM gradle:5.6.4-jdk11 as builder
COPY --chown=gradle:gradle . /server
WORKDIR /server
RUN gradle shadowJar

FROM openjdk:8-jdk-alpine
WORKDIR /server
COPY --from=builder /server/build/libs .
EXPOSE 4567 8080
CMD ["java", "-jar", "server-1.0-SNAPSHOT-all.jar"]

更新: 我将 Dockerfile 更改为:RUN gradle bootJar 现在 Spring 启动,但 vaadin 似乎在查找 .vaadin/node/node 时遇到问题

[pool-2-thread-1] INFO NodeInstaller - Extracting NPM
[pool-2-thread-1] INFO NodeInstaller - Local node installation successful.
[pool-2-thread-1] ERROR dev-updater - Error when running `npm install`
java.io.IOException: Cannot run program "/root/.vaadin/node/node" (in directory "/server"): error=2, No such file or directory
4

1 回答 1

0

首先你必须运行 bootJar 而不是 shadowJar。其次,您必须在生产模式下构建

RUN gradle bootJar -Pvaadin.productionMode

请阅读 Vaadin 文档中有关生产模式的更多信息 https://vaadin.com/docs/flow/production/tutorial-production-mode-basic.html

于 2021-02-23T15:33:55.220 回答