4

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

  1. 我的demo应用程序:一个运行在 上的 Spring Boot 应用程序localhost:9001,我希望它成为一个 Eureka Client,即,将自己注册为一个实例,同时能够发现其他实例(我使用了通用@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);  
    }
    }
    
  2. 应用程序.yml:

    server:
      port: 9001
    
    eureka:
      client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/
    

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/

有人在这里看到任何问题吗?

4

9 回答 9

8

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

这是赤裸裸的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>

这是大多数情况下需要的启动包依赖项:

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

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

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

于 2020-03-20T07:43:51.053 回答
4

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

于 2016-04-13T20:39:25.060 回答
2

在 pom.xml 中执行以下操作

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<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>

在 Spring 引导配置中执行以下操作:

@EnableDiscoveryClient @SpringBootApplication

公共类ServiceApplication

于 2019-07-02T11:42:55.060 回答
1
service.application.properties
spring.application.name=rating-data-service
server.port=8083
eureka.client.eureka-server-port=8761 // on which port server is running
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false

server.application.properties

server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
于 2020-04-05T05:16:57.487 回答
0
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

在您的 pom.xml 中添加上述内容,如果您使用的是 gradle,请使用以下代码:

compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: '1.4.6.RELEASE'
于 2018-11-05T07:02:53.823 回答
0

客户端 build.gradle 需要有点像下面的

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudVersion', "Hoxton.SR1")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

客户端的 application.properties 文件应包含以下内容 -

spring.application.name=movie-catalog-service // Your service name
server.port=8081 // on which port you are running
eureka.client.eureka-server-port=8761 // on which port server is running

Eureka Server build.gradle 需要有点像下面这样

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "Hoxton.SR3")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

Eureka Server 文件的 application.properties 应该有点像这样

server.port=8761 // port on which server is running
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

参考: https ://spring.io/guides/gs/service-registration-and-discovery/

于 2020-03-21T14:08:15.317 回答
0

如果我们同时包含starter一个和没有启动器,如依赖关系树中所见,它本身netflix-eureka-client就会丢失eureka-client

因此,添加缺少的依赖项是有意义的。我认为netflix-eureka-client应该正常依赖eureka-client,但不是出于某种原因:)

    <dependency>
        <groupId>com.netflix.eureka</groupId>
        <artifactId>eureka-client</artifactId>
    </dependency>

在此处输入图像描述

于 2020-08-26T10:42:09.513 回答
0

尝试@EnableEurekaServer为尤里卡应用程序使用这个依赖项:

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

@EnableEurekaClient用于具有依赖关系的 eureka 客户端应用程序:

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

并为客户端应用程序说尤里卡位于哪里

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

截屏

于 2021-11-17T06:31:02.517 回答
0

在里面添加 pom.xml spring-cloud-netflix-eureka-client

于 2019-05-25T19:22:54.370 回答