构建Multi Zone Eureka Server实战

野性酷女 2021-07-24 20:05 409阅读 0赞

一 Eureka Server

1 application.yml

  1. eureka:
  2. server:
  3. use-read-only-response-cache: false
  4. response-cache-auto-expiration-in-seconds: 10
  5. management:
  6. endpoints:
  7. web:
  8. exposure:
  9. include: '*'

2 application-zone1a.yml

  1. server:
  2. port: 8761
  3. spring:
  4. application:
  5. name: eureka-server
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. preferIpAddress: true
  10. metadataMap.zone: zone1
  11. client:
  12. register-with-eureka: true
  13. fetch-registry: true
  14. region: region-east
  15. service-url:
  16. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  17. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  18. availability-zones:
  19. region-east: zone1,zone2
  20. server:
  21. waitTimeInMsWhenSyncEmpty: 0
  22. enableSelfPreservation: false

3 application-zone1b.yml

  1. server:
  2. port: 8762
  3. spring:
  4. application:
  5. name: eureka-server
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. preferIpAddress: true
  10. metadataMap.zone: zone1
  11. client:
  12. register-with-eureka: true
  13. fetch-registry: true
  14. region: region-east
  15. service-url:
  16. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  17. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  18. availability-zones:
  19. region-east: zone1,zone2
  20. server:
  21. waitTimeInMsWhenSyncEmpty: 0
  22. enableSelfPreservation: false

4 application-zone2a.yml

  1. server:
  2. port: 8763
  3. spring:
  4. application:
  5. name: eureka-server
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. preferIpAddress: true
  10. metadataMap.zone: zone2
  11. client:
  12. register-with-eureka: true
  13. fetch-registry: true
  14. region: region-east
  15. service-url:
  16. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  17. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  18. availability-zones:
  19. region-east: zone1,zone2
  20. server:
  21. waitTimeInMsWhenSyncEmpty: 0
  22. enableSelfPreservation: false

5 application-zone2b.yml

  1. server:
  2. port: 8764
  3. spring:
  4. application:
  5. name: eureka-server
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. preferIpAddress: true
  10. metadataMap.zone: zone2
  11. client:
  12. register-with-eureka: true
  13. fetch-registry: true
  14. region: region-east
  15. service-url:
  16. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  17. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  18. availability-zones:
  19. region-east: zone1,zone2
  20. server:
  21. waitTimeInMsWhenSyncEmpty: 0
  22. enableSelfPreservation: false

二 Eureka Client

1 application.yml

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: '*'

2 application-zone1.yml

  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: client
  6. eureka:
  7. instance:
  8. metadataMap.zone: zone1
  9. client:
  10. register-with-eureka: true
  11. fetch-registry: true
  12. region: region-east
  13. service-url:
  14. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  15. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  16. availability-zones:
  17. region-east: zone1,zone2

3 application-zone2.yml

  1. server:
  2. port: 8082
  3. spring:
  4. application:
  5. name: client
  6. eureka:
  7. instance:
  8. metadataMap.zone: zone2
  9. client:
  10. register-with-eureka: true
  11. fetch-registry: true
  12. region: region-east
  13. service-url:
  14. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  15. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  16. availability-zones:
  17. region-east: zone1,zone2

三 Zuul-gateway

1 application.yml

  1. spring:
  2. application:
  3. name: zuul-gateway
  4. management:
  5. endpoints:
  6. web:
  7. exposure:
  8. include: '*'

2 application-zone1.yml

  1. server:
  2. port: 10001
  3. eureka:
  4. instance:
  5. metadataMap.zone: zone1
  6. client:
  7. register-with-eureka: true
  8. fetch-registry: true
  9. region: region-east
  10. service-url:
  11. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  12. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  13. availability-zones:
  14. region-east: zone1,zone2

3 application-zone2.yml

  1. server:
  2. port: 10002
  3. eureka:
  4. instance:
  5. metadataMap.zone: zone2
  6. client:
  7. register-with-eureka: true
  8. fetch-registry: true
  9. region: region-east
  10. service-url:
  11. zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  12. zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
  13. availability-zones:
  14. region-east: zone1,zone2

四 组网

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70

五 启动

1 启动4个Eureka Server

  1. mvn spring-boot:run -Dspring.profiles.active=zone1a
  2. mvn spring-boot:run -Dspring.profiles.active=zone1b
  3. mvn spring-boot:run -Dspring.profiles.active=zone2a
  4. mvn spring-boot:run -Dspring.profiles.active=zone2b

2 启动2个Eureka Client

  1. mvn spring-boot:run -Dspring.profiles.active=zone1
  2. mvn spring-boot:run -Dspring.profiles.active=zone2

3 启动2个Zuul Gateway

  1. mvn spring-boot:run -Dspring.profiles.active=zone1
  2. mvn spring-boot:run -Dspring.profiles.active=zone2

六 测试

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 2

从测试结果来看访问gateway的/client/actuator/env,访问的是Eureka Client实例的/actuator/env接口,处于zone1的gateway返回的activeProfiles为zone1。

从这个表象来看gateway路由时对client实例是ZoneAffinity的。

发表评论

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

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

相关阅读