Spring Cloud -- Eureka 高可用集群

ゝ一纸荒年。 2021-09-11 02:42 452阅读 0赞

Spring Cloud-Eureka High-Availability Cluster

环境信息:
OS:Win10
JDK:Java8
SpringCloud:Greenwich.SR2

1 Eureka Server

1.1 核心依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>

1.2 开启注解

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

1.3 主要配置

新建三个配置文件,对应三个Eureka节点:

  1. application-node1.yml
  2. application-node2.yml
  3. application-node3.yml

三个配置文件配置内容一样,不同的是:

  1. 端口:server.port
    分别修改为三个不同的端口:18761 18762 18763
  2. 名字:spring.application.name
    分别修改为三个不同的name:node1 node2 node3
  3. 名字:eureka.instance.hostname
    分别修改为三个不同的域名:node1 node2 node3
  4. 注册地址:eureka.client.serviceUrl.defaultZone
    每个节点的注册地址要为其他节点的地址,如node1要配置成node2和node3的地址,已达到相互注册的目的;

    eureka:
    client:

    1. serviceUrl:
    2. defaultZone: http://node2:18762/eureka/,http://node3:18763/eureka/

node1节点服务端完整配置如下,其他节点按照上边的规则进行修改:

  1. server:
  2. port: 18761
  3. spring:
  4. application:
  5. name: cloud-eureka-node1
  6. eureka:
  7. instance:
  8. hostname: node1
  9. #不使用主机名来定义注册中心的地址, 而使用IP地址的形式,
  10. #如果设置了ipAddress 属性, 则使用该属性配置的IP, 否则自动获取除环路IP外的第一个IP地址
  11. preferIpAddress: true #自动将IP注册到Eureka Server上, 如果不配置就是机器的主机名
  12. #ipAddress: 192.168.1.128 # IP地址
  13. #将Instance ID设置成IP:端口的形式
  14. instance-id: ${ spring.application.name}:${ server.port}
  15. #注意:更改Eureka更新频率将打破服务器的自我保护功能, 生产环境下不建议自定义这些配置。
  16. lease-expiration-duration-in-seconds: 30 #续约到期时间(默认90秒)
  17. lease-renewal-interval-in-seconds: 10 #续约更新时间间隔(默认30秒)
  18. #prefer-ip-address: true
  19. client:
  20. #向注册中心注册本服务实例
  21. register-with-eureka: true
  22. #检索并维护注册中心服务实例
  23. fetch-registry: false
  24. serviceUrl:
  25. defaultZone: http://node2:18762/eureka/,http://node3:18763/eureka/
  26. server:
  27. enable-self-preservation: false
  28. eviction-interval-timer-in-ms: 5000
  29. waitTimeInMsWhenSyncEmpty: 0

在这里插入图片描述

1.4 Windows配置

修改host文件,在C:\Windows\System32\drivers\etc

  1. #Eureka DS
  2. 127.0.0.1 node1
  3. 127.0.0.1 node2
  4. 127.0.0.1 node3

1.5 打包启动

在项目下运行mvn package,生成工程jar包;
然后开启三个终端,分别启动三个节点:

  1. java -jar cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=node1
  2. java -jar cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=node2
  3. java -jar cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=node3

前两个节点会有报错,是因为有节点连接超时,都启起来就好了;
在这里插入图片描述

1.6 Eureka服务端控制台

分别打开:

  1. http://node1:18761
  2. http://node2:18762
  3. http://node3:18763

可以看到他们相互进行了注册,DS Replicas能够看到其他节点:

在这里插入图片描述

在这里插入图片描述

服务端代码:https://github.com/WeisonWei/school-service-cloud/tree/master/cloud-eureka

2 Eureka Client

2.1 核心依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>

2.2 开启注解

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

2.3 主要配置

跟单节点不同的是注册地址eureka.client.serviceUrl.defaultZone,配置多个,其他没有变化:

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: student-service
  6. management:
  7. endpoints:
  8. web:
  9. exposure:
  10. include: '*'
  11. endpoint:
  12. health:
  13. show-details: always
  14. shutdown:
  15. enabled: true
  16. eureka:
  17. client:
  18. registry-fetch-interval-seconds: 5
  19. healthcheck:
  20. enabled: true
  21. serviceUrl:
  22. defaultZone: http://node1:18761/eureka,http://node2:18762/eureka,http://node3:18763/eureka

2.4 启动服务

以不同端口启动多个服务:

2.5 查看控制台服务注册情况

  1. http://node1:18761
  2. http://node2:18762
  3. http://node3:18763

在这里插入图片描述

客户端代码:https://github.com/WeisonWei/school-service/tree/master/student-service

参考资料:
[ 1 ].Spring Cloud 微服务实战 翟永超 电子工业出版社

发表评论

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

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

相关阅读