java - 带有 Netflix Eureka 的 Spring Boot 应用程序打包成 fat jar 后无法启动

标签 java spring spring-boot jar netflix-eureka

所以我一直在关注这个Microservices with Spring Boot指南,IDE(intelliJ IDEA)中的一切似乎都按预期工作,Eureka Server 启动并按预期运行。 但是,在我将 Eureka 服务器导出为 fat jar 后出现了问题。

为了将 Eureka 服务器打包到一个 fat jar 中,我在终端中使用了 mvn clean package,它一直对我有用。

之后,当我尝试使用 java -jar myjar.jar 运行 jar 时,出现以下异常

    org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.tushinov.ServiceRegistryStarter.main(ServiceRegistryStarter.java:11) [classes!/:1.0]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [service-registry-1.0.jar:1.0]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [service-registry-1.0.jar:1.0]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [service-registry-1.0.jar:1.0]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [service-registry-1.0.jar:1.0]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
        at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:125) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:417) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:176) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        ... 16 common frames omitted
Caused by: java.lang.IllegalStateException: StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[] failed to start
        at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.rethrowDeferredStartupExceptions(TomcatWebServer.java:171) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:110) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        ... 21 common frames omitted

在 Stackoverflow 中查看后,我发现了 this问题,但它没有帮助我解决问题。所以我决定自己问一个问题。

我已经使用mvn clean package来制作 fat jar 子很长一段时间了,从来没有遇到过问题。怎么这次又失败了呢?我猜这与 netflix eureka 服务器有关。也许缺少配置或类似的东西。

这是我目前拥有的一切:

pom.xml

<?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>org.tushinov</groupId>
    <artifactId>service-registry</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>

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

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

应用程序属性

spring.application.name=eureka-server

server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

和代码

package org.tushinov;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

如果我遗漏了任何内容,请在评论中询问以进行澄清。 欢迎任何帮助和/或建议,我们将不胜感激!

最佳答案

过了一段时间我找到了这个问题的答案。它不是一个配置或类似的东西。这是 intelliJ 和 maven 中的 JDK 不匹配。在 intelliJ 中,我使用 JDK 1.8 来编译和运行我的应用程序。

我没有意识到的是,mvn 使用 JAVA_HOME 参数来查找它应该使用哪个 JDK,而不是 intelliJ,您可以在 intelliJ 中指定您的 jdk。在我的例子中,JAVA_HOME 参数指向 JDK 1.9,显然,它可能需要更多依赖项和/或配置才能运行 Netflix Eureka 服务器。

这些resources帮助我解决了问题。

关于java - 带有 Netflix Eureka 的 Spring Boot 应用程序打包成 fat jar 后无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52951745/

相关文章:

java - 依赖项问题 - Eclipse Java 项目

java - 如何在 Spring Boot 中配置自定义 AccessDecisionManager 和自定义 AuthenticationProvider

java - Spring框架,@Transactional(isolation = Isolation.DEFAULT)

java - 忽略列表中对象的特定属性

java - 所下的订单包含不存在的产品

java - 如何使用 JButton 打开文件

Java自定义类加载器获取资源

java - 如何在 Java 中保持多个服务器之间的数据结构同步?

Tomcat集群中的Spring Websocket

java - Spring 启动 : get command line argument within @Bean annotated method