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

  • 相关阅读:
    C——编译预处理
    基于Pytorch的猫狗图片分类【深度学习CNN】
    SpringCloud微服务实战——搭建企业级开发框架(五十一):微服务安全加固—自定义Gateway拦截器实现防止SQL注入/XSS攻击
    PyCharm连接远程Docker环境
    【Spring Boot 使用记录】kafka自动配置和自定义配置及消费者
    Spring事务问题,同一次请求中相同SQL查询结果不一致
    站库分离技术--反向代理技术-雷池云WAF-给自己搭建一个安全点的网站
    Python第二次作业(4)【矩形面积与周长】
    Linux:用户态与内核态
    【Sentinel】Sentinel配置zk持久化
  • 原文地址:https://blog.csdn.net/keeppractice/article/details/133842659