Spring Cloud分区发布实践(5)--定制ServiceInstanceListSupplier
现在我们简单地来定制二个 ServiceInstanceListSupplier, 都是zone-preference的变种.
为了方便, 我重新调整了一下项目的结构, 把一些公用的类移动到hello-pubtool 模块, 这样网关项目和Feign项目就能复用一样的类了.
A. main和beta互不相通, 绝对隔离 (资源相对充裕)
回到最开始的目的, 我们先实现这个A方案
package com.cnscud.betazone.pub.samezone;
import com.cnscud.betazone.pub.LogUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 只返回统一区域的实例. (和网上的代码略有不同)
* @see org.springframework.cloud.loadbalancer.core.ZonePreferenceServiceInstanceListSupplier
* @see org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplierBuilder
*
*/
public class SameZoneOnlyServiceInstanceListSupplier implements ServiceInstanceListSupplier {
private final String ZONE = "zone";
private final ServiceInstanceListSupplier delegate;
private final LoadBalancerZoneConfig zoneConfig;
private String zone;
public SameZoneOnlyServiceInstanceListSupplier(ServiceInstanceListSupplier delegate,
LoadBalancerZoneConfig zoneConfig) {
this.delegate = delegate;
this.zoneConfig = zoneConfig;
}
@Override
public String getServiceId() {
return delegate.getServiceId();
}
@Override
public Flux<List<ServiceInstance>> get() {
return delegate.get().map(this::filteredByZone);
}
private List<ServiceInstance> filteredByZone(List<ServiceInstance> serviceInstances) {
if (zone == null) {
zone = zoneConfig.getZone();
}
if (zone != null) {
List<ServiceInstance> filteredInstances = new ArrayList<>();
for (ServiceInstance serviceInstance : serviceInstances) {
String instanceZone = getZone(serviceInstance);
if (zone.equalsIgnoreCase(instanceZone)) {
filteredInstances.add(serviceInstance);
}
}
//如果没找到就返回空列表,绝不返回其他集群的实例
LogUtils.warn("find instances size: " + filteredInstances.size());
return filteredInstances;
}
//如果没有zone设置, 则返回所有实例
return serviceInstances;
}
private String getZone(ServiceInstance serviceInstance) {
Map<String, String> metadata = serviceInstance.getMetadata();
if (metadata != null) {
return metadata.get(ZONE);
}
return null;
}
}
很简单, 不过要注意这个实现如果没有zone设置, 则返回所有可用实例.
对应的配置声明:
/**
* 自定义 Instance List Supplier: 根据默认Zone划分, 并且zone互相隔离.
*/
public class SameZoneOnlyCustomLoadBalancerConfiguration {
@Bean
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
DiscoveryClient discoveryClient, Environment env,
LoadBalancerZoneConfig zoneConfig,
ApplicationContext context) {
ServiceInstanceListSupplier delegate = new SameZoneOnlyServiceInstanceListSupplier(
new DiscoveryClientServiceInstanceListSupplier(discoveryClient, env),
zoneConfig
);
ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider = context
.getBeanProvider(LoadBalancerCacheManager.class);
if (cacheManagerProvider.getIfAvailable() != null) {
return new CachingServiceInstanceListSupplier(
delegate,
cacheManagerProvider.getIfAvailable()
);
}
return delegate;
}
}
这里使用了缓存, 如果需要更多特性, 就去 LoadBalancerClientConfiguration 的源码里去参悟吧, 还有什么 health-check, same-instance-preference很多东西可以参考.
C. beta绝对隔离, main在发布过程中可以切换到beta (main区域只有一台机器, 资源比较紧张)
这个也比较简单, 仅有几行代码的差异
package com.cnscud.betazone.pub.samezone;
import com.cnscud.betazone.pub.LogUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Beta只返回统一区域的实例, 其他区域如果为空则返回所有实例.
* @see org.springframework.cloud.loadbalancer.core.ZonePreferenceServiceInstanceListSupplier
* @see org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplierBuilder
*
*/
public class SameZoneSpecialBetaServiceInstanceListSupplier implements ServiceInstanceListSupplier {
private final String ZONE = "zone";
private String ZONE_BETA = "beta";
private final ServiceInstanceListSupplier delegate;
private final LoadBalancerZoneConfig zoneConfig;
private String zone;
public SameZoneSpecialBetaServiceInstanceListSupplier(ServiceInstanceListSupplier delegate,
LoadBalancerZoneConfig zoneConfig) {
this.delegate = delegate;
this.zoneConfig = zoneConfig;
}
@Override
public String getServiceId() {
return delegate.getServiceId();
}
@Override
public Flux<List<ServiceInstance>> get() {
return delegate.get().map(this::filteredByZone);
}
private List<ServiceInstance> filteredByZone(List<ServiceInstance> serviceInstances) {
if (zone == null) {
zone = zoneConfig.getZone();
}
if (zone != null) {
List<ServiceInstance> filteredInstances = new ArrayList<>();
for (ServiceInstance serviceInstance : serviceInstances) {
String instanceZone = getZone(serviceInstance);
if (zone.equalsIgnoreCase(instanceZone)) {
filteredInstances.add(serviceInstance);
}
}
//如果没找到就返回空列表,绝不返回其他集群的实例
LogUtils.warn("find instances size: " + filteredInstances.size());
if(filteredInstances.size()>0) {
return filteredInstances;
}
else {
//如果是beta, 则返回空
if (zone.equalsIgnoreCase(ZONE_BETA)){
return filteredInstances;
}
}
}
//如果没有zone设置, 则返回所有实例
return serviceInstances;
}
private String getZone(ServiceInstance serviceInstance) {
Map<String, String> metadata = serviceInstance.getMetadata();
if (metadata != null) {
return metadata.get(ZONE);
}
return null;
}
}
这个也没啥特殊的.
D. 发布前标注一套区域为beta的服务, 测试通过后修改beta服务的区域为main, 多个实例都可上线服务 (资源非常紧张, 操作相对复杂)
这个就是个附加操作, 可以使用A,B,C任意一个方案, 搭配脚本就可以使用.
脚本已经准备好了:
本文源码: https://github.com/cnscud/javaroom/tree/main/betazone2/hello-pubtool{:target="_blank"}
比较
灰度发布肯定还有很多方案, 但是对于作者来说, 根据zone来做分区灰度发布, 可能这是最简单的一种方式了, 实现简单, 通过Nginx做简单的设置分流到2组网关上, 就可以实现2组实例了.
当然Nginx也可以根据header, cookie, URL来做分流,就看自己的需要了.
代码简单, 容易理解, 大道至简!
后面还会实践通过Header来分流的方法, 不过比较而言, zone-preference 还是最简单的, 后面的实践起来服务直接传递数据就是头疼...
Spring Cloud分区发布实践(5)--定制ServiceInstanceListSupplier的更多相关文章
- Spring Cloud分区发布实践(1) 环境准备
最近研究了一下Spring Cloud里面的灰度发布, 看到各种各样的使用方式, 真是纷繁复杂, 眼花缭乱, 不同的场景需要不同的解决思路. 那我们也来实践一下最简单的场景: 区域划分: 服务分为be ...
- Spring Cloud分区发布实践(6)--灰度服务-根据Header选择实例区域
此文是一个完整的例子, 包含可运行起来的源码. 此例子包含以下部分: 网关层实现自定义LoadBalancer, 根据Header选取实例 服务中的Feign使用拦截器, 读取Header Feign ...
- Spring Cloud分区发布实践(3) 网关和负载均衡
注意: 因为涉及到配置测试切换, 中间环节需按此文章操作体验, 代码仓库里面的只有最后一步的代码 准备好了微服务, 那我们就来看看网关+负载均衡如何一起工作 新建一个模块hello-gateway, ...
- Spring Cloud分区发布实践(4) FeignClient
上面看到直接通过网关访问微服务是可以实现按区域调用的, 那么微服务之间调用是否也能按区域划分哪? 下面我们使用FeignClient来调用微服务, 就可以配合LoadBalancer实现按区域调用. ...
- Spring Cloud分区发布实践(2) 微服务
我们准备一下用于查询姓名的微服务. 首先定义一下服务的接口, 新建一个空的Maven模块hello-remotename-core, 里面新建一个类: public interface RemoteN ...
- Spring Cloud Alibaba发布第二个版本,Spring 发来贺电
还是熟悉的面孔,还是熟悉的味道,不同的是,这次的配方升级了. 今年10月底,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cl ...
- spring cloud微服务实践二
在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...
- 厉害了,Spring Cloud Alibaba 发布 GA 版本!
? 小马哥 & Josh Long ? 喜欢写一首诗一般的代码,更喜欢和你共同 code review,英雄的相惜,犹如时间沉淀下来的对话,历久方弥新. 相见如故,@杭州. 4 月 18 日, ...
- Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级。
升级前 => 升级后 Spring Boot 1.5.x => Spring Boot 2.0.2 Spring Cloud Edgware SR4 => Spring Cloud ...
随机推荐
- SpringCloud Alibaba实战(8:使用OpenFeign服务调用)
源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一个章节,我们已经成功地将服务注册到了Nacos注册中心,实现了服务注册和服务发 ...
- 精尽Spring Boot源码分析 - SpringApplication 启动类的启动过程
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- 21、部署heartbeat
21.1.heartbeat部署规划: 本文的实验环境是虚拟机设备: 名称 接口 ip 用途 master-db(主) eth0 10.0.0.16/24 用于服务器之间的心跳连接(直连) eth1 ...
- layui--入门(helloWorld)
具体可参考官方文档:https://www.layui.com/doc/ 由于引入layui 需要用到node.js 安装过程可参考: https://www.cnblogs.com/liuchenx ...
- Docker:Docker的Run命令使用时报错
命令报错:WARNING: Your kernel does not support swap limit capabilities, memory limited without swap. 这是因 ...
- Java:Java实例化(new)过程
实例化过程(new) 1.首先去JVM 的方法区中区寻找类的class对象,如果能找到,则按照定义生成对象,找不到 >>如下2.所示 2.加载类定义:类加载器(classLoader)寻找 ...
- [心得笔记]Java多线程中的内存模型
一:现代计算机的高速缓存 在计算机组成原理中讲到,现代计算机为了匹配 计算机存储设备的读写速度 与 处理器运算速度,在CPU和内存设备之间加入了一个名为Cache的高速缓存设备来作为缓冲:将运算需要 ...
- 线程中的yield()
属于本地方法 /** * A hint to the scheduler that the current thread is willing to yield * its current use o ...
- WPF项目升级sqlite-net-pcl时遇到Library e_sqlite3 not found的问题解决办法记录
项目中为了方便的存储本地数据,使用了SQLite数据库作为数据存储,再设计时DB.models引用了sqlite-net-pcl,那么项目再升级sqlite-net-pc 1.7.335l版本后后,遇 ...
- Django基础-002 Models的属性与字段
1.models字段类型 AutoField():一个IntegerField,根据可用ID自动递增.如果没指定主键,就创建它自动设置为主键. IntegerField():一个整数: FloatFi ...