高可用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()

相关推荐
架构师沉默4 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
Java中文社群5 小时前
重要:Java25正式发布(长期支持版)!
java·后端·面试
每天进步一点_JL6 小时前
JVM 类加载:双亲委派机制
java·后端
阿里云云原生7 小时前
【云栖大会】AI原生、AI可观测、AI Serverless、AI中间件,4场论坛20+议题公布!
云原生
用户298698530147 小时前
Java HTML 转 Word 完整指南
java·后端
渣哥7 小时前
原来公平锁和非公平锁差别这么大
java
容器魔方7 小时前
Bloomberg 正式加入 Karmada 用户组!
云原生·容器·云计算
渣哥7 小时前
99% 的人没搞懂:Semaphore 到底是干啥的?
java
J2K7 小时前
JDK都25了,你还没用过ZGC?那真得补补课了
java·jvm·后端
kfyty7257 小时前
不依赖第三方,不销毁重建,loveqq 框架如何原生实现动态线程池?
java·架构