工程 pom 中公共依赖
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
1,Eureka Server 工程
启动 4 个实例, 配置两个 zone, 即 zone1,zone2, 每个 zone 都要 2 个 eureka server 实例, 这个 2 个 zone 配置在同一个 region 上, 即 region-east.
1.1,eureka-server 工程 pom 文件:
- <!-- 加上文章头部的公共依赖 -->
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
1.2,eureka-server 工程启动类
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaServerApplication.class, args);
- }
- }
1.3,eureka-server 工程配置文件, 路径: eureka-server\src\main\resources\, 分别有 5 个文件: application-zone1a.YAML,application-zone1b.YAML,application-zone2a.YAML,application-zone2b.YAML,application.YAML
- application-zone1a.YAML:
- server:
- port: 8761
- spring:
- application:
- name: eureka-server
- eureka:
- instance:
- hostname: localhost
- preferIpAddress: true
- metadataMap.zone: zone1
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
- server:
- waitTimeInMsWhenSyncEmpty: 0
- enableSelfPreservation: false
- application-zone1b.YAML
- server:
- port: 8762
- spring:
- application:
- name: eureka-server
- eureka:
- instance:
- hostname: localhost
- preferIpAddress: true
- metadataMap.zone: zone1
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
- server:
- waitTimeInMsWhenSyncEmpty: 0
- enableSelfPreservation: false
- application-zone2a.YAML
- server:
- port: 8763
- spring:
- application:
- name: eureka-server
- eureka:
- instance:
- hostname: localhost
- preferIpAddress: true
- metadataMap.zone: zone2
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
- server:
- waitTimeInMsWhenSyncEmpty: 0
- enableSelfPreservation: false
- application-zone2b.YAML
- server:
- port: 8764
- spring:
- application:
- name: eureka-server
- eureka:
- instance:
- hostname: localhost
- preferIpAddress: true
- metadataMap.zone: zone2
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
- server:
- waitTimeInMsWhenSyncEmpty: 0
- enableSelfPreservation: false
application.YAML
- eureka:
- server:
- use-read-only-response-cache: false
- response-cache-auto-expiration-in-seconds: 10
- management:
- endpoints:
- web:
- exposure:
- include: '*'
从上面的 4 个配置文件可以看出, 我们通过 eureka.instance.metadataMap.zone 设置了每个实例所属的 zone, 接下来使用这 4 个配置启动 4 个 eureka server 实例:
- // 启动命令
- mvn spring-boot:run -Dspring.profiles.active=zone1a
- mvn spring-boot:run -Dspring.profiles.active=zone1b
- mvn spring-boot:run -Dspring.profiles.active=zone2a
- mvn spring-boot:run -Dspring.profiles.active=zone2b
2,Eureka Client 工程
这里配置 2 个 eureka clent, 分别属于 zone1 和 zone2.
2.1,eureka-client 工程 pom 文件
- <!-- 加上文章头部公共配置 -->
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
2.1,eureka-client 工程启动类
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- @SpringBootApplication
- @EnableDiscoveryClient
- public class EurekaClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaClientApplication.class, args);
- }
- }
2.2,eureka-client 工程配置文件, 路径: eureka-client\src\main\resources\, 共 3 个文件: application.YAML,application-zone1.YAML,application-zone2.YAML
- application.YAML:
- # 这里暴露所有的 endpoints, 便于后面验证
- management:
- endpoints:
- Web:
- exposure:
- include: '*'
- application-zone1.YAML:
- server:
- port: 8081
- spring:
- application:
- name: client
- eureka:
- instance:
- metadataMap.zone: zone1
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
- application-zone2.YAML:
- server:
- port: 8082
- spring:
- application:
- name: client
- eureka:
- instance:
- metadataMap.zone: zone2
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
2.3, 启动 eureka client, 执行命令, 启动 2 个实例:
- mvn spring-boot:run -Dspring.profiles.active=zone1
- mvn spring-boot:run -Dspring.profiles.active=zone2
3,Zuul Gateway 工程
这里新建一个 zuul 网关工程, 来演示 metadataMap 的 zone 属性中 ZoneAffinity 特性.
3.1,zuul gateway 工程, pom 文件:
- <!-- 加上文章头部的公共依赖 -->
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
3.2,zuul gateway 工程启动类:
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableZuulProxy
- public class ZuulGatewayApplication {
- public static void main(String[] args) {
- SpringApplication.run(ZuulGatewayApplication.class, args);
- }
- }
3.3,zuul gateway 工程配置文件, 路径: zuul-gateway\src\main\resources\, 一共 3 个文件: application.YAML,application-zone1.YAML,application-zone2.YAML
- application.YAML:
- spring:
- application:
- name: zuul-gateway
- management:
- endpoints:
- Web:
- exposure:
- include: '*'
- application-zone1.YAML:
- server:
- port: 10001
- eureka:
- instance:
- metadataMap.zone: zone1
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
- application-zone2.YAML:
- server:
- port: 10002
- eureka:
- instance:
- metadataMap.zone: zone2
- client:
- register-with-eureka: true
- fetch-registry: true
- region: region-east
- service-url:
- zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
- availability-zones:
- region-east: zone1,zone2
3.4, 启动 zuul gateway 工程, 一共 2 个实例, 执行命令:
- mvn spring-boot:run -Dspring.profiles.active=zone1
- mvn spring-boot:run -Dspring.profiles.active=zone2
验证 ZoneAffinity, 访问: localhost:10001/client/actuator/env, 结果:
访问: localhost:10002/client/actuator/env, 结果:
可以看出请求网关 / client/actuator/env, 访问的是 eureka client 实例的 / actuator/env 接口, 处于 zone1 的 Gateway 返回的 activeProfiles 为 zone1, 处于 zone2 的 Gateway 返回的 activeProfiles 是 zone2.
来源: https://www.cnblogs.com/idoljames/p/11620616.html