Spring Cloud 配置Eureka Server高可用集群之吊打拦路虎

朱雀 2022-03-26 08:20 285阅读 0赞

Spring Cloud 配置Eureka Server高可用集群之吊打拦路虎

源码地址:https://download.csdn.net/download/qq_33624284/10923226

案例:工具采用 idea + 项目结构采用maven多module的结构 + spring boot 版本为 2.1.2

  1. 创建空模板Maven项目 eureka
    pom.xml依赖:

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0

    com.ztw
    eureka
    1.0-SNAPSHOT

    pom


    org.springframework.boot
    spring-boot-starter-parent
    2.1.2.RELEASE





    eureka-server



    UTF-8
    UTF-8
    1.8

    Finchley.RELEASE





    org.springframework.cloud
    spring-cloud-dependencies
    ${spring-cloud.version}
    pom
    import




  2. 在eureka下创建module eureka-server,目录结构如下:
    eureka 目录结构
    pom.xml依赖:

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0

    com.ztw
    eureka-server
    0.0.1-SNAPSHOT
    jar
    eureka-server
    Demo project for Spring Boot



    com.ztw
    eureka
    1.0-SNAPSHOT



    1.8




    org.springframework.cloud

    spring-cloud-starter-netflix-eureka-server



    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.boot
    spring-boot-maven-plugin




application.yml配置:

  1. server:
  2. port: 4001
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. ###因为是单个服务,不需要注册
  8. #fetch-registry: false
  9. #register-with-eureka: false
  10. fetch-registry: false
  11. register-with-eureka: false
  12. serviceUrl:
  13. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

EurekaServerApplication.class:

  1. package com.ztw;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class EurekaServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaServerApplication.class, args);
  10. }
  11. }

http://localhost:4001/ 圈起来的为空,是因为我们还没有客户注册
在这里插入图片描述

  1. 在eureka下创建module eureka-client,目录结构如下:
    在这里插入图片描述
    pom.xml依赖:

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0
    com.ztw
    eureka-client
    0.0.1-SNAPSHOT
    jar
    eureka-client
    Demo project for Spring Boot



    com.ztw
    eureka
    1.0-SNAPSHOT



    1.8




    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.boot
    spring-boot-starter-web



    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.boot
    spring-boot-maven-plugin




application.yml配置:

  1. server:
  2. port: 4002
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. ###需要注册
  8. #fetch-registry: false
  9. #register-with-eureka: false
  10. serviceUrl:
  11. ###服务地址,注册
  12. defaultZone: http://${eureka.instance.hostname}:4001/eureka/
  13. spring:
  14. application:
  15. name: eureka-client

EurekaClientApplication.class:

  1. package com.ztw;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @SpringBootApplication
  6. @EnableEurekaClient
  7. public class EurekaClientApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaClientApplication.class, args);
  10. }
  11. }

http://localhost:4001/ 圈起来的地方已经有eureka-client 证明注册成功了
在这里插入图片描述
拓展:

  1. eureka:
  2. instance:
  3. ###续约心跳时间30秒
  4. # lease-renewal-interval-in-seconds: 30
  5. ###服务剔除时间90秒
  6. # lease-expiration-duration-in-seconds: 90
  7. server:
  8. ###响应缓存30秒,不会立即注册
  9. # response-cache-update-interval-ms: 30
  10. ###Eureka自我保护,
  11. #Eureka Serve接收到的服务续约低于为该值配置的百分比(默认为 15 分钟 内低于 85% ),
  12. #则服务器开启自我保护模式,即不再剔除注册列表的信息。
  13. # enable-self-preservation: false
  1. 配置eureka 高可用集群(两个为例)
    hosts文件配置:

    C:\Windows\System32\drivers\etc
    hosts
    127.0.0.1 peer7
    127.0.0.1 peer8

eureka-server application.yml文件配置:

  1. ---
  2. spring:
  3. profiles: peer7
  4. server:
  5. port: 4007
  6. eureka:
  7. instance:
  8. ###实际应用配服务地址,这里为本地开发环境,已"peer7"参数直观代替
  9. hostname: peer7
  10. client:
  11. ###因为是多个服务集群,需要注册
  12. #fetch-registry: false
  13. #register-with-eureka: false
  14. serviceUrl:
  15. defaultZone: http://peer8:4008/eureka/
  16. ---
  17. spring:
  18. profiles: peer8
  19. server:
  20. port: 4008
  21. eureka:
  22. instance:
  23. hostname: peer8
  24. client:
  25. serviceUrl:
  26. defaultZone: http://peer7:4007/eureka/

eureka-client application.yml文件配置:

  1. server:
  2. port: 4002
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. ###需要注册
  8. #fetch-registry: false
  9. #register-with-eureka: false
  10. serviceUrl:
  11. ###服务地址,向一个服务注册
  12. defaultZone: http://${eureka.instance.hostname}:4007/eureka/
  13. spring:
  14. application:
  15. name: eureka-client
  1. 分别启动对应服务(idea)两次服务启动,需要配置步骤如下:
    在这里插入图片描述
    eureka-client 选择main方法启动即可。
  2. 验证结果
    http://peer7:4007/
    在这里插入图片描述
    http://peer8:4008/
    在这里插入图片描述

通过访问两个eureka 服务,得到界面的结果来看,都有我们的eureka-client,而我们在client只向peer7:4007/这个服务中注册过,结论明显:两个服务共享了,实现了eureka server集群。

以上是详细的eureka 配置过程,(eclipse 的搭建未写,可以自己尝试)搭建项目过程中报错的过程都省略了,对应的解决方法都在上面,可以自己动手配置,一步一步走,遇到问题对照博文修改。

发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读