简体   繁体   English

使用 Spring Boot 2.4.1 启动 Eureka 客户端时出错

[英]Error starting up Eureka Client with Spring Boot 2.4.1

I am developing an application using Spring Boot 2.4.1 and Eureka.我正在使用 Spring Boot 2.4.1 和 Eureka 开发应用程序。 I achieved to start up the Eureka Server, but with the Eureka Client I am getting the following error:我成功启动了 Eureka Server,但是使用 Eureka Client 我收到以下错误:

Caused by: java.lang.ClassNotFoundException: com.sun.jersey.client.apache4.ApacheHttpClient4
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    ... 55 common frames omitted
...
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.cloud.netflix.eureka.CloudEurekaClient.getApplications()" because the return value of "org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration.getEurekaClient()" is null
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.maybeInitializeClient(EurekaServiceRegistry.java:54) ~[spring-cloud-netflix-eureka-client-3.0.0.jar:3.0.0]
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.register(EurekaServiceRegistry.java:38) ~[spring-cloud-netflix-eureka-client-3.0.0.jar:3.0.0]
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.start(EurekaAutoServiceRegistration.java:83) ~[spring-cloud-netflix-eureka-client-3.0.0.jar:3.0.0]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.2.jar:5.3.2]

The pom.xml is the following: 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>15</java.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

    <groupId>some.group</groupId>
    <artifactId>some-artefact</artifactId>
    <version>1.0</version>
    <name>some-name</name>
    <description>some-description</description>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

The following is the application.yml:以下是application.yml:

spring:
  application:
    name: @project.artifactId@

logging:
  file.name: ../logs/@project.artifactId@.log
  file.max-size: 10MB

server:
  port: 8080

And the main class:以及主要的class:

@SpringBootApplication
@ComponentScan("com.something")
@EnableDiscoveryClient
public class SpigaConnectorServiceApplication {

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

    @RestController
    class ServiceInstanceRestController {

        @Autowired
        private DiscoveryClient discoveryClient;

        @RequestMapping("/service-instances/{applicationName}")
        public List<ServiceInstance> serviceInstancesByApplicationName(
                @PathVariable String applicationName) {
            return this.discoveryClient.getInstances(applicationName);
        }
    }
}

The following is the server pom:以下是服务器pom:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>15</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <groupId>com.ptesa.cloud</groupId>
    <artifactId>pt-eureka-server</artifactId>
    <version>1.0</version>
    <name>pt-eureka-server</name>
    <description>Registry and discovery of services</description>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

The application.yml of the server:服务器的application.yml:

server:
  port: 8761
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

And the class that start the server:以及启动服务器的 class :

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(com.ptesa.cloud.EurekaServerApplication.class);
    }

}

When using Spring Boot version 2.3.7.RELEASE I don't have this problem.当使用 Spring 启动版本 2.3.7.RELEASE 我没有这个问题。 The application starts almost normally.应用程序几乎可以正常启动。 Just get the following warning:只需收到以下警告:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/Jucaalpa/.m2/repository/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Is there a way to run Eureka Client with Spring Boot 2.4.1.有没有办法使用 Spring Boot 2.4.1 运行 Eureka Client。 Do you think it's better to use Spring Boot 2.3.7 because of compatibilities issue like the one I am having?由于像我遇到的兼容性问题,您认为使用 Spring Boot 2.3.7 会更好吗?

Thanks you.谢谢。

I found that adding the following dependency, the problem doesn't occur.我发现添加以下依赖,问题不会出现。

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-apache-client4</artifactId>
    <version>1.19.4</version>
</dependency>

In client yaml file , add the below config客户端 yaml 文件中,添加以下配置

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost

May help: I changed the project SDK version from 16 to 1.8 and the problem disappeared!可能有帮助:我将项目 SDK 版本从 16 更改为 1.8,问题消失了! (in Intellij go to Project Structure) (在 Intellij go 到项目结构中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM