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

  • 相关阅读:
    MCU主频 服务器台式机主频 处理器主频那些事
    I2C通信协议
    3年功能测试拿8K,被刚入职的应届生反超,其实你在假装努力
    出差学小白知识No6:LD_PRELOAD变量路径不对找不到库文件
    CSDN 僵尸粉 机器人
    【C++】线程池(有点乱待补充)
    JAVA内部类
    随机数发生器设计(四)
    【问题解决】Android Studio 无法连接手机(荣耀90)无法识别手机usb
    tlaplus-vscode插件使用记录
  • 原文地址:https://blog.csdn.net/keeppractice/article/details/133842659