java 无法启动Bean“eurekaAutoServiceRegistration”

rkkpypqq  于 2023-04-28  发布在  Java
关注(0)|答案(3)|浏览(419)

在我的SpringBoot应用程序中启动ApplicationContext时出现以下错误:

org.springframework.context.ApplicationContextException: Failed to start bean 'eurekaAutoServiceRegistration'; nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:880) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at es.ual.acg.cos.abstractcomponents.AbstractcomponentsApplication.main(AbstractcomponentsApplication.java:12) [classes/:na]

    Caused by: java.lang.NullPointerException: null
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.maybeInitializeClient(EurekaServiceRegistry.java:56) ~[spring-cloud-netflix-eureka-client-1.4.3.RELEASE.jar:1.4.3.RELEASE]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.register(EurekaServiceRegistry.java:37) ~[spring-cloud-netflix-eureka-client-1.4.3.RELEASE.jar:1.4.3.RELEASE]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.start(EurekaAutoServiceRegistration.java:80) ~[spring-cloud-netflix-eureka-client-1.4.3.RELEASE.jar:1.4.3.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 14 common frames omitted

我已经在主类中启用了带有装饰器@EnableEurekaClient的Eureka 客户端。
我有configure。属性文件,包含:

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.preferIpAddress=true

我用途:

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.3.RELEASE</version>
</dependency>

首先,我已经在其他Sping Boot 项目中运行了Eureka Server(它工作正常)。
注意:这是主类:

package es.ual.acg.cos.abstractcomponents;

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

@SpringBootApplication(scanBasePackages={"es.ual.acg.cos.abstractcomponents", "es.ual.acg.cos.util"})
@EnableEurekaClient
public class AbstractcomponentsApplication {

    public static void main(String[] args) {
        SpringApplication.run(AbstractcomponentsApplication.class, args);
    }
}
nuypyhwy

nuypyhwy1#

我也遇到了同样的问题,对我来说原因是版本冲突。
我的springboot版本是1。5.9.RELEASE,我的项目maven依赖关系中止如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.2.1</version>
    </dependency>
<dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

原因是因为我引入了httpcomponents,它的版本是4。2.1,但与Eureka 推出的httpcomponents版本不同(版本为httpclient/4.5.3和httpcore/4。4.6,你可以使用mvn dependency:tree来检查),我认为是导致bean 'eurekaAutoServiceRegistration'失败的原因。
最后我把httpclient的版本改成了4。5.3和httpcore到4。4.6,那就管用了,希望对你有帮助^_^

5us2dqdw

5us2dqdw2#

如果应用程序用于Eureka 客户端,则删除Eureka服务器依赖项spring-cloud-starter-netflix-eureka-server
原因:
在Eureka 客户端应用程序中,如果存在不需要的服务器依赖项spring-cloud-starter-netflix-eureka-server,则可能会发生此错误。

org.springframework.context.ApplicationContextException: Failed to start bean 'eurekaAutoServiceRegistration'; nested exception is java.lang.NullPointerException
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.25.jar:5.3.25]
.
.
Caused by: java.lang.NullPointerException: null
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.maybeInitializeClient(EurekaServiceRegistry.java:54) ~[spring-cloud-netflix-eureka-client-3.1.3.jar:3.1.3]
.
.

然而,原因也可能是因为失败的bean创建(使用如下所示的stacktrace):

Error creating bean with name 'scopedTarget.eurekaClient' defined in class path resource 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
.
.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.netflix.discovery.EurekaClient]: Factory method 'eurekaClient' threw exception; nested exception is java.lang.RuntimeException: Failed to initialize DiscoveryClient!
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.25.jar:5.3.25]
.
.
Caused by: java.lang.RuntimeException: Failed to initialize DiscoveryClient!
    at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:436)
.
.
Caused by: java.lang.NoClassDefFoundError: com/sun/jersey/client/apache4/ApacheHttpClient4
    at com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories.newTransportClientFactory(Jersey1TransportClientFactories.java:59) ~[eureka-client-1.10.17.jar:1.10.17]
.
.
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.client.apache4.ApacheHttpClient4
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na]
.
.

删除与Eureka 服务器相关的依赖项,并仅具有客户端依赖项spring-cloud-starter-netflix-eureka-client可以潜在地解决此问题。
我在Sping Boot v2中遇到了这种情况。7.8(Spring Cloud。版本v2021。0.3)。

wqnecbli

wqnecbli3#

也许,您使用其他json反序列化依赖项。Jackson是Spring Cloud的默认序列化依赖项。
可能是

<dependency>
  <groupId>com.alibaba</groupId>
  <actifactId>fastjson</actifactId>
</dependency>

我通过删除这个依赖项来解决这个问题。

相关问题