无法将服务注册为 Eureka Client

OD *_*eet 4 spring netflix-eureka spring-cloud-netflix

我正在尝试将微服务注册为 Eureka 客户端以发现其他微服务,但是,我按照教程进行操作,但 Eureka 服务器中没有任何显示。下面是代码片段:

  1. 我的demo应用程序:一个在 上运行的 spring 启动应用程序localhost:9001,我希望它成为一个 Eureka 客户端,即,将自己注册为一个实例,同时具有发现其他实例的能力(我使用了通用@EnableDiscoveryClient注释并且 Spring Netflix Eureka 在类路径上):

    @RestController
    @SpringBootApplication
    @EnableDiscoveryClient
    public class DemoApplication {
    
    @RequestMapping("/") 
    String home() {
        return "This is a Demo project";
    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);  
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 应用程序.yml:

    server:
      port: 9001
    
    eureka:
      client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/
    
    Run Code Online (Sandbox Code Playgroud)

Eureka 服务器应该没问题,因为我localhost:8080在服务器上成功注册了另一个正在运行的 Miscroservice 。以防万一,这里是 Eureka 服务器的 application.yml:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Run Code Online (Sandbox Code Playgroud)

有人看到这里有什么问题吗?

zer*_*iko 6

重要的是选择正确的导入。我失去了几个小时才意识到:

这是依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是大多数情况下所需的入门包依赖项:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如果您导入裸依赖项,则不会显示任何错误或日志,但该服务不会注册到 Eureka 服务器。

最重要的是,确保仔细检查依赖项:也许您需要 Spring 引导组件的“启动包”依赖项。


OD *_*eet 4

问题解决了,我没有在 build.gradle 中包含依赖项 'com.netflix.eureka:eureka-client:1.1.147'。