工程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。

Eureka实战-2【构建Multi Zone Eureka Server】的更多相关文章

  1. spring boot 2.0.3+spring cloud (Finchley)1、搭建服务注册和发现组件Eureka 以及构建高可用Eureka Server集群

    一 .搭建Eureka 编写Eureka Server 由于有多个spring boot项目,采用maven多module的结构,项目结构如下: 新建一个maven主工程,在主maven的pom文件中 ...

  2. 【SpringCloud Eureka源码】从Eureka Client发起注册请求到Eureka Server处理的整个服务注册过程(下)

    目录 一.Spring Cloud Eureka Server自动配置及初始化 @EnableEurekaServer EurekaServerAutoConfiguration - 注册服务自动配置 ...

  3. Eureka实战-4【开启http basic权限认证】

    在我们实际生产环境中,都需要考虑到一个安全问题,比如用户登录,又或者是eureka server,它对外暴露的有自己的rest API,如果没有安全认证,也就意味着别人可以通过rest API随意修改 ...

  4. SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端、Eureka 服务信息、Eureka 发现管理、Eureka 安全配置、Eureka-HA(高可用) 机制、Eureka 服务打包部署)

    1.概念:Eureka 服务发现框架 2.具体内容 对于服务发现框架可以简单的理解为服务的注册以及使用操作步骤,例如:在 ZooKeeper 组件,这个组件里面已经明确的描述了一个服务的注册以及发现操 ...

  5. SpringCloud02 Eureka知识点、Eureka服务端和客户端的创建、Eureka服务端集群、Eureka客户端向集群的Eureka服务端注册

    1 Eureka知识点 按照功能划分: Eureka由Eureka服务端和Eureka客户端组成 按照角色划分: Eureka由Eureka Server.Service Provider.Servi ...

  6. Storm 实战:构建大数据实时计算

    Storm 实战:构建大数据实时计算(阿里巴巴集团技术丛书,大数据丛书.大型互联网公司大数据实时处理干货分享!来自淘宝一线技术团队的丰富实践,快速掌握Storm技术精髓!) 阿里巴巴集团数据平台事业部 ...

  7. [原创].NET 分布式架构开发实战之四 构建从理想和实现之间的桥梁(前篇)

    原文:[原创].NET 分布式架构开发实战之四 构建从理想和实现之间的桥梁(前篇) .NET 分布式架构开发实战之四 构建从理想和实现之间的桥梁(前篇) 前言:上一篇文章讲述了一些实现DAL的理论,本 ...

  8. List多个字段标识过滤 IIS发布.net core mvc web站点 ASP.NET Core 实战:构建带有版本控制的 API 接口 ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目 Using AutoFac

    List多个字段标识过滤 class Program{  public static void Main(string[] args) { List<T> list = new List& ...

  9. [转] Akka实战:构建REST风格的微服务

    [From] http://www.yangbajing.me/2015/11/27/akka%E5%AE%9E%E6%88%98%EF%BC%9A%E6%9E%84%E5%BB%BArest%E9% ...

随机推荐

  1. Condition控制线程通信

    Condition控制线程通信 一.前言 java.util.concurrent.locks.Condition 接口描述了可能会与锁有关联的条件变量.这些变量在用法上与使用Object.wait ...

  2. matplotlib绘制矢量图像(svg),pdf and ps文件

    机器学习的过程中处理数据,会遇到数据可视化的问题. 大部分都是利用python的matplotlib库进行数据的可视化处理. plt.show() 默认都是输出.png文件,图片只要稍微放大一点,就糊 ...

  3. [python]文档字符串

    文档字符串可以在运行时访问,也可以用来自动生成文档. 输入: def foo(): print "This is a doc string" return True foo() 运 ...

  4. A-Graph Games_2019牛客暑期多校训练营(第三场)

    题意 给出一张无向图,定义S[x]表示与点x直接相连的点集,有两个操作 1 x y表示将第x到第y条边状态变化(若存在则删除,不存在则建立) 2 x y询问S[x]与S[y]是否相等 题解 有一个技巧 ...

  5. Codeforces 1058 D. Vasya and Triangle 分解因子

    传送门:http://codeforces.com/contest/1058/problem/D 题意: 在一个n*m的格点中,问能否找到三个点,使得这三个点围成的三角形面积是矩形的1/k. 思路: ...

  6. 刨死你系列——LinkedHashMap剖析(基于jdk1.8)

    一.概述 LinkedHashMap 继承自 HashMap,在 HashMap 基础上,通过维护一条双向链表,解决了 HashMap 不能随时保持遍历顺序和插入顺序一致的问题.除此之外,Linked ...

  7. React-router总结

    版本 v3和v4有一些差距: https://blog.csdn.net/qq_35484341/article/details/80500237 以下的总结,都是基于V4的 官方文档:https:/ ...

  8. sql 增删改列名

    添加列:alter table table_name add new_column data_type [interality_codition] ALTER TABLE dbo.tb newColu ...

  9. .NET Core 3.0 Preview 9 发布

    翻译自官方博客 今天,我们宣布推出.NET Core 3.0 Preview 9.就像预览版8一样,我们专注打磨最终版本的.NET Core 3.0,而不是添加新功能.如果这些最终版本看起来不像早期预 ...

  10. ASP.NET Core SignalR:集线器Hubs

    一.什么是集线器hubs 通过SignalR的集线器hubs中定义的方法,服务器可以调用连接中的客户端定义的方法,而客户端也可以调用服务器端集线器中定义的方法.SignalR负责实现了客户端和服务器之 ...