Eureka实战-2【构建Multi Zone Eureka Server】
工程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.yml,application-zone1b.yml,application-zone2a.yml,application-zone2b.yml,application.yml
application-zone1a.yml:
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.yml
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.yml
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.yml
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.yml
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.yml,application-zone1.yml,application-zone2.yml
application.yml:
#这里暴露所有的endpoints,便于后面验证
management: endpoints: web: exposure: include: '*'
application-zone1.yml:
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.yml:
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.yml,application-zone1.yml,application-zone2.yml
application.yml:
spring: application: name: zuul-gateway management: endpoints: web: exposure: include: '*'
application-zone1.yml:
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.yml:
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。
上一篇: vue使用readAsDataURL实现选择图片文件后预览
下一篇: Android开发笔记