无法使用 netflix eureka 注册 spring 微服务

问题描述

我有主类的 maven spring 微服务。

@EnableEncryptableProperties
@SpringBootApplication
@EnablediscoveryClient
public class AccountService {

    private static ApplicationContext applicationContext;

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

}

在我的 pom.xml 中,我添加了以下用于注册应用程序的依赖项。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>

启动时出现以下错误

[WARN ] 2021-01-11 22:05:50 [main] AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDeFinitionStoreException: Failed to process import candidates for configuration class [com.microservices.accountservice.AccountService]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
[ERROR] 2021-01-11 22:05:50 [main] SpringApplication - Application run Failed
org.springframework.beans.factory.BeanDeFinitionStoreException: Failed to process import candidates for configuration class [com.microservices.accountservice.AccountService]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist

项目定义了以下spring版本。

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

此外,微服务在启动期间使用 bootstrap.properties 与 spring-cloud-config-server 进行通信以获取 application.properties。与此错误相关的问题是什么?

解决方法

您的 pom.xml 中的依赖项彼此不兼容。您应该使用 spring-cloud-starter-netflix-eureka-client:2.2.6.RELEASE

如果您不为每个依赖项指定版本,而是使用 BOM (spring-cloud-dependencies),那就更好了,请参阅 official guide