工程公共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工程

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、项目启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class EurkeaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurkeaServerApplication.class, args);
}
}

1.3、这里配置4个eureka server实例,路径:eureka-server\src\main\resources\,分4个zone,属于region-east、region-west两个区。

属于region-east的配置文件为:application-zone1.yml,application-zone2.yml

application-zone1.yml:

server:
port: 8761
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-west: http://localhost:8763/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-east: zone1,zone2
instance:
hostname: localhost
metadataMap.zone: zone1

application-zone2.yml:

server:
port: 8762
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-west: http://localhost:8763/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-east: zone1,zone2
instance:
hostname: localhost
metadataMap.zone: zone2

属于region-west的配置文件为:application-zone3-region-west.yml,application-zone4-region-west.yml

application-zone3-region-west.yml

server:
port: 8763
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-east: http://localhost:8761/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-west
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-west: zone3,zone4
instance:
hostname: localhost
metadataMap.zone: zone3

application-zone4-region-west.yml

server:
port: 8764
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-east: http://localhost:8761/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-west
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-west: zone3,zone4
instance:
hostname: localhost
metadataMap.zone: zone4

由于框架中,EurekaServerConfigBean的remoteRegionAppWhitelist默认值是null,而getRemoteRegionAppWhitelist(String regionName)方法又被直接调用,如果工程上不处理,就直接空指针异常了。

//框架源码
package org.springframework.cloud.netflix.eureka.server; import ...... @ConfigurationProperties("eureka.server")
public class EurekaServerConfigBean implements EurekaServerConfig {
public static final String PREFIX = "eureka.server";
private static final int MINUTES = 60000;
@Autowired(
required = false
)
PropertyResolver propertyResolver;
private String aWSAccessId;
//.....private String[] remoteRegionUrls;
private Map<String, Set<String>> remoteRegionAppWhitelist;
  //......
}

so,在eureka server工程中加入以下配置:

import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.EurekaServerConfig;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.HashMap; /**
* 配置类
*/
@Configuration
@AutoConfigureBefore(EurekaServerAutoConfiguration.class)//当前配置类EurekaServerAutoConfiguration加载完毕后的后续加载操作
public class RegionConfig { @Bean
@ConditionalOnMissingBean
public EurekaServerConfig eurekaServerConfig(EurekaClientConfig clientConfig) {
EurekaServerConfigBean server = new EurekaServerConfigBean();
if (clientConfig.shouldRegisterWithEureka()) {
server.setRegistrySyncRetries(5);
}
server.setRemoteRegionAppWhitelist(new HashMap<>());
return server;
}
}

1.4、启动实例,执行命令:

mvn spring-boot:run -Dspring.profiles.active=zone1
mvn spring-boot:run -Dspring.profiles.active=zone2
mvn spring-boot:run -Dspring.profiles.active=zone3-region-west
mvn spring-boot:run -Dspring.profiles.active=zone4-region-west

2、Eureka Client工程

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.2、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.3、eureka client工程配置文件

这里配置4个client实例,也是分4个zone,属于region-east、region-west两个区。

属于region-east的配置文件为:application-zone1.yml,application-zone2.yml

application-zone1.yml:

server:
port: 8071
spring:
application.name: demo-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-east: zone1,zone2
instance:
metadataMap.zone: zone1

application-zone2.yml

server:
port: 8072
spring:
application.name: demo-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-east
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-east: zone1,zone2
instance:
metadataMap.zone: zone2

属于region-west的配置文件为:application-zone3.yml,application-zone4.yml

application-zone3.yml:

server:
port: 8073
spring:
application.name: demo-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-west
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-west: zone3,zone4
instance:
metadataMap.zone: zone3

application-zone4.yml:

server:
port: 8074
spring:
application.name: demo-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-west
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-west: zone3,zone4
instance:
metadataMap.zone: zone4

application.yml:

management:
endpoints:
web:
exposure:
include: '*'

2.4、启动eureka client工程,执行命令:

mvn spring-boot:run -Dspring.profiles.active=zone1
mvn spring-boot:run -Dspring.profiles.active=zone2
mvn spring-boot:run -Dspring.profiles.active=zone3
mvn spring-boot:run -Dspring.profiles.active=zone4

3、Zuul Gateway工程

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工程启动类:

package cn.springcloud.book;

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
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulGatewayApplication { public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
}

3.3、zuul gateway工程配置文件,这里使用2个gateway实例来演示fallback到remote region的应用实例功能,配置文件一个属于region-east,一个属于region-west。

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

application-zone3-region-west.yml:

server:
port: 10002
eureka:
instance:
metadataMap.zone: zone3
client:
register-with-eureka: true
fetch-registry: true
region: region-west
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-west: zone3,zone4

3.4、启动gateway工程,执行命令:

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

访问:localhost:10001/demo-client/actuator/env,结果如下:

访问:localhost:10002/demo-client/actuator/env,结果如下:

可以看到ZoneAffinity特性(上一篇文章有讲),zone1的gateway访问的是zone1的demo-client,zone3的gateway访问的是zone3的demo-client。

接下来,关闭eureka-client的zone1实例,继续访问 localhost:10001/demo-client/actuator/env,可以看到在经过几个错误之后,自动fallback到了remote-region的zone4实例上,实现了类似异地多活自动转移请求的效果。

Eureka【支持Remote Region】的更多相关文章

  1. 支持remote write和exemplar的prometheus服务

    最近项目组在做Prometheus指标采集和告警,其中用到了Prometheus的exemplar特性,由于该特性比较新,当前支持该特性的存储有比较少.因此需要自行实现exemplar功能. 我在gi ...

  2. 【分享】让prometheus支持PUSH模式,可以使用remote write协议推送数据

    2021-10-21补充: 我通过修改源码来让prometheus支持remote-write,纯属多此一举: --enable-feature=remote-write-receiver这个命令行参 ...

  3. 深入理解Eureka - Eureka Client获取注册信息机制

    深入理解Eureka - Eureka Client获取注册信息机 Eureka Client提供了定时获取注册信息的机制.Eureka Client获取注册信息的所有逻辑都在DiscoveryCli ...

  4. 0401-服务注册与发现、Eureka简介

    一.硬编码问题 解决方案:nginx.或.服务注册与发现 二.服务发现 注册.心跳机制 三.服务发现组件的功能 1.服务注册表:是一个记录当前可用服务实例的网络信息的数据库,是服务发现机制的核心.服务 ...

  5. SpringCloud核心教程 | 第三篇:服务注册与发现 Eureka篇

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...

  6. springCloud值Eureka

    Spring Cloud特点 约定优于配置 开箱即用.快速启动 适用于各种环境      PC Server  云环境  容器(Docker) 轻量级的组件  服务发现Eureka 组件的支持很丰富, ...

  7. spring cloud Eureka 配置信息

    Eureka instance 一个服务,如:订单系统,会部署多台服务器,而每台服务器上提供的服务就是instance; 负载配置. Eureka service 指的是服务,提供一种特定功能的服务, ...

  8. 一小时搞定Eureka

    一.什么是Eureka Eureka是Netflix公司开源的产品,它是一种基于REST( Representational State Transfer )的服务,主要用于AWS云. Eureka提 ...

  9. SpringCloud 源码系列(2)—— 注册中心 Eureka(中)

    五.服务注册 1.实例信息注册器初始化 服务注册的代码位置不容易发现,我们看 DiscoveryClient 初始化调度任务的这个方法,这段代码会去初始化一个实例信息复制器 InstanceInfoR ...

随机推荐

  1. mysql报错码code=exited,status=2的解决方案

    由于电脑死机,导致MySQL无法重启. 解决方案看官方文档,设置完后重启失败,再把innodb_force_recovery = 1去掉就可以了 https://dev.mysql.com/doc/r ...

  2. html预加载之link标签

    我们之前提及过link rel 里面有preload和prefetch.modulepreload,都是用于预加载资源 <link rel="preload" href=&q ...

  3. 在AspNetCore3.0中使用Autofac

    1. 引入Nuget包 Autofac Autofac.Extensions.DependencyInjection 2. 修改Program.cs 将默认ServiceProviderFactory ...

  4. 记录下我用Jenkins打包碰到的坑

    使用Andorid Studio 打包都是正常的,但是使用Jenkins自动打包一直报错,尝试过网上的各种方案,依然都不行,报错如下. FAILURE: Build failed with an ex ...

  5. "数字经济"云安全共测大赛Web-Writeup

    gameapp 这题首先反编译apk,简单看了看代码,主要是有startgame和score两个api,然后用模拟器(手机登不上)安装apk抓了下包,数据经过了rsa加密,所以首先用python实现r ...

  6. v-on绑定特性命名带小横杠 ‘-’与props属性中变量怎么对应

    特性命名问题: 矛盾点一:html的特性不区分大小写 矛盾点二:Vue中除了模板命名,其他命名不允许出现小横杠 ‘-’ 在js文件内,命名为驼峰式,camerCase,进入html文件,自动转换成短横 ...

  7. git如何压栈某一个文件?

    答: 使用git stash -p进行交互式操作,y表示压栈,n表示不压栈

  8. 最新react-native(Expo)安装使用antd-mobile-rn组件库

    1\安装antd-mobile-rn 库 npm install antd-mobile-rn --save 2.按需加载 npm install babel-plugin-import --save ...

  9. 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化

    效果: /**  * Flutter  BottomNavigationBar 自定义底部导航条.以及实现页面切换:  * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...

  10. Linux云服务器磁盘不见了?解决方案在这里,云服务器磁盘挂载

    用过诸多种云以后,发现有个通病,就是新买的数据盘在机器中找不到.本篇总结一下此类问题的解决方法,望各位点赞,有问题评论区见 一.云服务和物理机一样,你买了云服务器的数据盘以后,就相当于把数据盘直接安装 ...