首页 > 解决方案 > Spring Boot 应用程序使用 intelliJ 运行,但在 docker 中崩溃

问题描述

我有一个弹簧启动应用程序。它主要是一个 Kafka 监听应用程序,它唯一的 Web 应用程序部分是通过spring-boot-starter-actuator. 在 IntelliJ 中本地运行时,应用程序运行完美,没有错误,也没有崩溃。但是,当尝试通过 docker 运行应用程序时,应用程序会启动(我可以看到 spring 徽标),然后它会立即崩溃并出现下一个错误:

[org.springframework.boot.SpringApplication] - Application run failed 
MyApp    | 2021-02-25T19:17:17.802342316Z 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.

我在文件中的依赖部分build.gradle如下所示:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.kafka:spring-kafka'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.4.2'

    /*logging*/
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
    testCompile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.30'
    testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    compile group: 'net.logstash.logback', name: 'logstash-logback-encoder', version: '6.4'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
    compile group: 'ch.qos.logback', name: 'logback-access', version: '1.2.3'

    /*lombok*/
    compileOnly 'org.projectlombok:lombok:1.18.16'
    annotationProcessor 'org.projectlombok:lombok:1.18.16'

    /*metrics*/
    compile group: 'io.prometheus', name: 'simpleclient', version: '0.9.0'
    compile group: 'io.prometheus', name: 'simpleclient_httpserver', version: '0.9.0'
    compile group: 'io.prometheus', name: 'simpleclient_servlet', version: '0.9.0'
    compile group: 'io.micrometer', name: 'micrometer-registry-prometheus', version: '1.5.1'
    compile group: 'io.micrometer', name: 'micrometer-core', version: '1.5.1'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.4.2'

    /*Jetty server*/
    compile group: 'org.eclipse.jetty', name: 'jetty-server', version: '9.4.29.v20200521'
    compile group: 'org.eclipse.jetty', name: 'jetty-servlet', version: '9.4.29.v20200521'

    /* mysql */
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'

    /*mongo*/
    compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.12.7'

    // generate pojo from avro schema
    compile "org.apache.avro:avro-compiler:1.10.0"

    //avro serde
    compile group: 'io.confluent', name: 'kafka-streams-avro-serde', version: '5.2.1'
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.12.0'
    compile group: 'io.projectreactor', name: 'reactor-core', version: '3.4.0'
    implementation('com.fasterxml.jackson.dataformat:jackson-dataformat-avro:2.11.2')

    /* cassandra*/
    implementation("io.dropwizard.metrics:metrics-core:3.2.2")
    compile group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version: '3.8.0'
    compile group: 'com.datastax.cassandra', name: 'cassandra-driver-mapping', version: '3.10.2'

    /*tests*/
    testImplementation 'org.springframework.kafka:spring-kafka-test'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testCompile("junit:junit:4.12")
} 

我的主要应用程序类如下所示:

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

任何人都可以阐明这个问题背后的原因以及如何解决它?为什么它在 IntelliJ 中运行没有问题但在 docker 中崩溃?任何帮助,将不胜感激。谢谢你。

标签: javaspringspring-bootdocker

解决方案


推荐阅读