构建Multi Zone Eureka Server实战

妖狐艹你老母 2021-07-25 02:52 426阅读 0赞

一 Eureka Server

1 application-zone1.yml

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

2 application-zone2.yml

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

3 application-zone3-region-west.yml

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

4 application-zone4-region-west.yml

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

5 cn.springcloud.book.config.RegionConfig

  1. package cn.springcloud.book.config;
  2. import com.netflix.discovery.EurekaClientConfig;
  3. import com.netflix.eureka.EurekaServerConfig;
  4. import org.springframework.boot.autoconfigure.AutoConfigureBefore;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  6. import org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration;
  7. import org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * Created by caibosi on 2018-06-25.
  14. */
  15. @Configuration
  16. @AutoConfigureBefore(EurekaServerAutoConfiguration.class)
  17. public class RegionConfig {
  18. @Bean
  19. @ConditionalOnMissingBean
  20. public EurekaServerConfig eurekaServerConfig(EurekaClientConfig clientConfig) {
  21. EurekaServerConfigBean server = new EurekaServerConfigBean();
  22. if (clientConfig.shouldRegisterWithEureka()) {
  23. // Set a sensible default if we are supposed to replicate
  24. server.setRegistrySyncRetries(5);
  25. }
  26. server.setRemoteRegionAppWhitelist(new HashMap<>());
  27. return server;
  28. }
  29. }

二 Eureka Client

1 application.yml

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

2 application-zone1.yml

  1. server:
  2. port: 8071
  3. spring:
  4. application.name: demo-client
  5. eureka:
  6. client:
  7. prefer-same-zone-eureka: true
  8. region: region-east
  9. service-url:
  10. zone1: http://localhost:8761/eureka/
  11. zone2: http://localhost:8762/eureka/
  12. availability-zones:
  13. region-east: zone1,zone2
  14. instance:
  15. metadataMap.zone: zone1

3 application-zone2.yml

  1. server:
  2. port: 8072
  3. spring:
  4. application.name: demo-client
  5. eureka:
  6. client:
  7. prefer-same-zone-eureka: true
  8. region: region-east
  9. service-url:
  10. zone1: http://localhost:8761/eureka/
  11. zone2: http://localhost:8762/eureka/
  12. availability-zones:
  13. region-east: zone1,zone2
  14. instance:
  15. metadataMap.zone: zone2

4 application-zone3.yml

  1. server:
  2. port: 8073
  3. spring:
  4. application.name: demo-client
  5. eureka:
  6. client:
  7. prefer-same-zone-eureka: true
  8. region: region-west
  9. service-url:
  10. zone3: http://localhost:8763/eureka/
  11. zone4: http://localhost:8764/eureka/
  12. availability-zones:
  13. region-west: zone3,zone4
  14. instance:
  15. metadataMap.zone: zone3

5 application-zone4.yml

  1. server:
  2. port: 8074
  3. spring:
  4. application.name: demo-client
  5. eureka:
  6. client:
  7. prefer-same-zone-eureka: true
  8. region: region-west
  9. service-url:
  10. zone3: http://localhost:8763/eureka/
  11. zone4: http://localhost:8764/eureka/
  12. availability-zones:
  13. region-west: zone3,zone4
  14. instance:
  15. metadataMap.zone: zone4

三 zuul-dateway

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/
  12. zone2: http://localhost:8762/eureka/
  13. availability-zones:
  14. region-east: zone1,zone2

3 application-zone3-region-west.yml

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

四 组网

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=zone1
  2. mvn spring-boot:run -Dspring.profiles.active=zone2
  3. mvn spring-boot:run -Dspring.profiles.active=zone3-region-west
  4. mvn spring-boot:run -Dspring.profiles.active=zone4-region-west

2 启动4个Eureka Client

  1. mvn spring-boot:run -Dspring.profiles.active=zone1
  2. mvn spring-boot:run -Dspring.profiles.active=zone2
  3. mvn spring-boot:run -Dspring.profiles.active=zone3
  4. mvn spring-boot:run -Dspring.profiles.active=zone4

3 启动2个Zuul Gateway

  1. mvn spring-boot:run -Dspring.profiles.active=zone1
  2. mvn spring-boot:run -Dspring.profiles.active=zone3-region-west

六 测试

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

从上面的测试结果可以看到zoneAffinity特性,zone1的gateway访问的是zone1的demo-client,zone3的gateway访问的是zone3的demo-client

接下来,关闭zone1和zone2的Eureka Client,再通过zone1中的gateway访问demo-client,将路径到region-west的demo-client

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 3

发表评论

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

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

相关阅读