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


    代码

    Eureka server 1

    pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starterartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.cloudgroupId>
    8. <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    9. dependency>
    10. dependencies>

    application.yml

    1. server:
    2. port: 8761
    3. eureka:
    4. instance:
    5. hostname: 192.168.31.168
    6. # hostname: localhost
    7. client:
    8. register-with-eureka: false
    9. fetch-registry: true
    10. service-url:
    11. defaultZone: http://localhost:8762/eureka/
    12. spring:
    13. application:
    14. name: eureka-server1

    EurekaServer1Application

    1. @SpringBootApplication
    2. @EnableEurekaServer
    3. public class EurekaServer1Application {
    4. public static void main(String[] args) {
    5. SpringApplication.run(EurekaServer1Application.class, args);
    6. }
    7. }

    Eureka server 2

    pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starterartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.cloudgroupId>
    8. <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    9. dependency>
    10. dependencies>

    application.yml

    1. server:
    2. port: 8762
    3. eureka:
    4. instance:
    5. hostname: 192.168.31.168
    6. client:
    7. register-with-eureka: false
    8. fetch-registry: true
    9. service-url:
    10. defaultZone: http://localhost:8761/eureka/
    11. spring:
    12. application:
    13. name: eureka-server2
    14. cloud:
    15. compatibility-verifier:
    16. enabled: false

    EurekaServer2Application

    1. @SpringBootApplication
    2. @EnableEurekaServer
    3. public class EurekaServer2Application {
    4. public static void main(String[] args) {
    5. SpringApplication.run(EurekaServer2Application.class, args);
    6. }
    7. }

    Eureka client

    pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starterartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-webartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.cloudgroupId>
    12. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    13. dependency>
    14. dependencies>

    application.yml

    1. spring:
    2. application:
    3. name: eureka-client
    4. eureka:
    5. client:
    6. service-url:
    7. defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
    EurekaClientApplication
    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class EurekaClientApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(EurekaClientApplication.class, args);
    6. }
    7. }

    依次运行Eureka server1 , Eureka server2, EurekaClient

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

    Eureka server1 

    Eureka server2

    相关问题与总结

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

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

  • 相关阅读:
    QVariant与Json的各种纠葛——Qt
    Luogu U238811 小 P 的数学题
    Java基于SSM+Vue的平时成绩管理系统
    CentOS下安装MongoDB
    IMS各网元的主要功能
    大数据Kubernetes(K8S)命令指南 超级详细!
    spark性能调优 | 默认并行度
    10分钟部署一个别人可以访问的在线网站
    .NET7 gRPC JSON转码+OpenAPI
    Cyanine5tetrazine(CAS号:1427705-31-4)结构式原理
  • 原文地址:https://blog.csdn.net/keeppractice/article/details/133842659