高可用eureka服务注册与发现代码例子

代码

Eureka server 1

pom.xml

XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

application.yml

XML 复制代码
server:
  port: 8761
eureka:
  instance:
    hostname: 192.168.31.168
#    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8762/eureka/

spring:
  application:
    name: eureka-server1

EurekaServer1Application

java 复制代码
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer1Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer1Application.class, args);
    }
}

Eureka server 2

pom.xml

XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

application.yml

java 复制代码
server:
  port: 8762
eureka:
  instance:
    hostname: 192.168.31.168
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/


spring:
  application:
    name: eureka-server2
  cloud:
    compatibility-verifier:
      enabled: false

EurekaServer2Application

java 复制代码
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer2Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer2Application.class, args);
    }
}

Eureka client

pom.xml

XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

application.yml

XML 复制代码
spring:
  application:
    name: eureka-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
复制代码
EurekaClientApplication
XML 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

依次运行Eureka server1 , Eureka server2, EurekaClient

你会发现两个server上都有了EurekaClient的注册信息。

Eureka server1

Eureka server2

相关问题与总结

  1. Eureka server 不能用localhost,否则不会复制注册信息到peer Eureka上。

相关代码PeerAwareInstanceRegistryImpl,ApplicationResource.addInstance(), PeerEurekaNodes.resolvePeerUrls()

相关推荐
刘梦凡呀20 分钟前
C#获取钉钉平台考勤记录
java·c#·钉钉
best_virtuoso38 分钟前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
yudiandian201438 分钟前
02 Oracle JDK 下载及配置(解压缩版)
java·开发语言
楚韵天工1 小时前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
helloworddm1 小时前
Orleans Stream SubscriptionId 生成机制详解
java·系统架构·c#
失散131 小时前
分布式专题——43 ElasticSearch概述
java·分布式·elasticsearch·架构
ajsbxi1 小时前
【Java 基础】核心知识点梳理
java·开发语言·笔记
聪明的笨猪猪2 小时前
Java JVM “调优” 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
重整旗鼓~2 小时前
28.redisson源码分析分布式锁
java·开发语言
Query*2 小时前
Java 设计模式——工厂模式:从原理到实战的系统指南
java·python·设计模式