首页 > 解决方案 > Eureka 服务器集群出错(对等感知)

问题描述

我的问题是关于 Spring Cloud Eureka:我正在尝试运行 Eureka Cluster(2 个节点,localhost:8761,localhost:8762),但出现了一些错误:

以下是application.yml文件:

服务器 1:

spring:
    profiles: eureka-peer1

server:
    port: 8761

eureka:
    instance:
        hostname: eureka-peer1
    client:
        registerWithEureka: true
        fetchRegistry: true
        serviceUrl:
            defaultZone: http://eureka-peer2:8762/eureka/

服务器 2:

spring:
   profiles: eureka-peer2

server:
    port: 8762

eureka:
   instance:
       hostname: eureka-peer2
   client:
       registerWithEureka: true
       fetchRegistry: true
       serviceUrl:
           defaultZone: http://eureka-peer1:8761/eureka/ 

文件 /etc/hosts:

127.0.0.1   localhost
127.0.0.1   eureka-peer1
127.0.0.1   eureka-peer2

ErekaService1Application.javaErekaService2Application.java

package com.example.eurekaservice1;

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

@SpringBootApplication
@EnableEurekaServer
@EnableEurekaClient
public class EurekaService1Application {

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

}

删除 @EnableEurekaClient 并将“registerWithEureka”和“fetchRegistry”设置为“false”会得到相同的结果。

此外,具有这些属性的独立模式会导致相同的错误:

spring:
  profiles: eureka-peer1

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

这是pom.xml文件,使用 start.spring.io 自动生成:

<?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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-service-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-service-1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

标签: spring-bootnetflix-eurekaservice-discovery

解决方案


删除@EnableEurekaClient和依赖

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

推荐阅读