Spring Cloud Gateway + Nacos 灰度发布

前言

本文将会使用 SpringCloud Gateway 网关组件配合 Nacos 实现灰度发布(金丝雀发布)

环境搭建

创建子模块服务提供者 provider,网关模块 gateway

父项目

pom.xml 配置

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.example</groupId>  <artifactId>spring-gateway-demo</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <name>spring-gateway-demo</name>  <description>spring-gateway-demo</description>  <properties>  <java.version>11</java.version>  <maven.compiler.source>11</maven.compiler.source>  <maven.compiler.target>11</maven.compiler.target>  <maven.compiler.plugin>3.8.1</maven.compiler.plugin>  <spring-boot.version>2.3.7.RELEASE</spring-boot.version>  <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>  <spring-cloud.version>Hoxton.SR9</spring-cloud.version>  <spring-cloud-starter-alibaba-nacos-config>2.2.0.RELEASE</spring-cloud-starter-alibaba-nacos-config>  </properties>  <modules>  <module>provider</module>  <module>gateway</module>  </modules>  <dependencies>  <dependency>  <groupId>com.alibaba.cloud</groupId>  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>  </dependency>  <dependency>  <groupId>com.alibaba.cloud</groupId>  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>  </dependency>  </dependencies>  <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>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-dependencies</artifactId>  <version>${spring-boot.version}</version>  <type>pom</type>  <scope>import</scope>  </dependency>  <dependency>  <groupId>com.alibaba.cloud</groupId>  <artifactId>spring-cloud-alibaba-dependencies</artifactId>  <version>${spring-cloud-alibaba.version}</version>  <type>pom</type>  <scope>import</scope>  </dependency>  </dependencies>  </dependencyManagement>  
</project>

服务提供者 provider

这里我们计划引入 nacos, 所以先创建一个 nacos 配置文件 dataId 为 provider.properties, 这里用默认的命名空间 public, 默认分组 DEFAULT_GROUP

version=2

provider 的 pom 配置依赖

<dependencies>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId>  </dependency>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope>  <exclusions>  <exclusion>  <groupId>org.junit.vintage</groupId>  <artifactId>junit-vintage-engine</artifactId>  </exclusion>  </exclusions>  </dependency>  
</dependencies>

application.yml

server:  port: 9001  
spring:  application:  name: provider  cloud:  nacos:  config:  server-addr: 127.0.0.1:8848  discovery:  server-addr: 127.0.0.1:8848

启动类上添加 @EnableDiscoveryClient 注解

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}}

然后添加测试 controller

@RefreshScope
@RestController
@RequestMapping("/test")
public class TestController {@Autowiredprivate Environment env;@Value("${version:0}")private String version;/*** http://localhost:9001/test/port* @return*/@GetMapping("/port")public Object port() {return String.format("port=%s, version=%s", env.getProperty("local.server.port"), version);}
}

注意,这里配置 nacos 的时候需要配置下面两个文件 provider.propertiesprovider,然后实际配置最终 nacos 是采用的 provider 文件,否则后端控制台就会持续输出 400 错误,可能是新版本问题,其他版本暂时不清楚(后面网关配置也是同理)

image.png

后端控制台输出,也可以看出需要两个

[fixed-localhost_8848] [subscribe] provider.properties+DEFAULT_GROUP
[fixed-localhost_8848] [add-listener] ok, tenant=, dataId=provider.properties, group=DEFAULT_GROUP, cnt=1
[fixed-localhost_8848] [subscribe] provider+DEFAULT_GROUP
[fixed-localhost_8848] [add-listener] ok, tenant=, dataId=provider, group=DEFAULT_GROUP, cnt=1

Nacos 版本:2.3.0-BETA

网关 gateway

gateway 服务的 pom 依赖配置如下:

    <dependencies>  <dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-gateway</artifactId>  </dependency>  <dependency>  <groupId>org.projectlombok</groupId>  <artifactId>lombok</artifactId>  <optional>true</optional>  </dependency>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope>  <exclusions>  <exclusion>  <groupId>org.junit.vintage</groupId>  <artifactId>junit-vintage-engine</artifactId>  </exclusion>  </exclusions>  </dependency>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-actuator</artifactId>  </dependency>  </dependencies>  

application.yml

# 应用服务 WEB 访问端口  
server:  port: 9000  
# 应用名称  
spring:  application:  name: gateway  cloud:  nacos:  config:  server-addr: 127.0.0.1:8848  discovery:  server-addr: 127.0.0.1:8848  gateway:  routes: # http://127.0.0.1:9000/actuator/gateway/routes  - id: provider  # 路由 ID,保持唯一  uri: lb://provider # uri指目标服务地址,lb代表从注册中心获取服务  predicates:  - Path=/provider/**  # http://127.0.0.1:9000/provider/port 会转发到 http://localhost:9001/provider/port, 和预期不符合, 需要StripPrefix来处理  filters:  - StripPrefix=1 # StripPrefix=1就代表截取路径的个数为1, 这样请求 http://127.0.0.1:9000/provider/test/port 会转发到 http://localhost:9001/test/port  
management:  endpoint:  gateway:  enabled: true  endpoints:  web:  exposure:  include: gateway

同样启动类上添加 @EnableDiscoveryClient 注解

查看所有路由:/actuator/gateway/routes
查看指定路由(GET):/actuator/gateway/routes/{id}
查看全局过滤器:/actuator/gateway/globalfilters
查看路由过滤器:/actuator/gateway/routefilters
POST 方式刷新路由缓存:/actuator/gateway/refresh

测试

curl http://127.0.0.1:9001/test/port
port=9001, version=2
curl http://127.0.0.1:9000/provider/test/port
port=9001, version=2

动态路由

实现动态路由有两种方式,一个是改写 RouteDefinitionRepository(实测失败),一个是基于 nacos 的监听器给 RouteDefinitionRepository 动态更新值。实现逻辑大同小异

Spring Cloud Gateway 中加载路由信息分别由以下几个类负责
1、PropertiesRouteDefinitionLocator:从配置文件中读取路由信息 (如 YML、Properties 等)
2、RouteDefinitionRepository:从存储器中读取路由信息 (如内存、配置中心、Redis、MySQL 等)
3、DiscoveryClientRouteDefinitionLocator:从注册中心中读取路由信息(如 Nacos、Eurka、Zookeeper 等)

下面使用 RouteDefinitionRepository 配置动态路由

gateway-router.json

[{"id": "provider","predicates": [{"name": "Path","args": {"_genkey_0": "/provider/**"}}],"filters": [{"name": "StripPrefix","args": {"_genkey_0": "1"}}],"uri": "lb://provider","order": 0
}]

NacosRouteDefinitionRepository 配置类

@Component  
public class NacosRouteDefinitionRepository implements RouteDefinitionRepository, ApplicationEventPublisherAware {  private static final Logger log = LoggerFactory.getLogger(NacosRouteDefinitionRepository.class);  @Autowired  private NacosConfigManager nacosConfigManager;  // 更新路由信息需要的  private ApplicationEventPublisher applicationEventPublisher;  private String dataId = "gateway-router.json";  private String group = "DEFAULT_GROUP";  @Value("${spring.cloud.nacos.config.server-addr}")  private String serverAddr;  private ObjectMapper objectMapper = new ObjectMapper();  @PostConstruct  public void dynamicRouteByNacosListener() {  try {  nacosConfigManager.getConfigService().addListener(dataId, group, new Listener() {  public void receiveConfigInfo(String configInfo) {  log.info("自动更新配置...\r\n{}", configInfo);  applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));  }  public Executor getExecutor() {  return null;  }  });  } catch (NacosException e) {  e.printStackTrace();  }  }  @Override  public Flux<RouteDefinition> getRouteDefinitions() {  try {  String configInfo = nacosConfigManager.getConfigService().getConfig(dataId, group, 5000);  List<RouteDefinition> gatewayRouteDefinitions = objectMapper.readValue(configInfo, new TypeReference<List<RouteDefinition>>() {  });  return Flux.fromIterable(gatewayRouteDefinitions);  } catch (NacosException e) {  e.printStackTrace();  } catch (JsonMappingException e) {  e.printStackTrace();  } catch (JsonProcessingException e) {  e.printStackTrace();  }  return Flux.fromIterable(Lists.newArrayList());  }  @Override  public Mono<Void> save(Mono<RouteDefinition> route) {  return null;  }  @Override  public Mono<Void> delete(Mono<String> routeId) {  return null;  }  @Override  public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {  this.applicationEventPublisher = applicationEventPublisher;  }  
}

然后重启网关,访问 http://127.0.0.1:9000/actuator/gateway/routes ,查看是否生效

[{"predicate": "Paths: [/provider/**], match trailing slash: true","route_id": "provider","filters": ["[[StripPrefix parts = 1], order = 1]"],"uri": "lb://provider","order": 0}
]

灰度发布

首先需要明白灰度的场景, 因为有不同版本的服务需要共存, 所以新的节点升级的时候必然代码及配置会存在差别, 所以我们根据这种差别来判断服务版本是新版本还是线上稳定版本。这里我们用 prod 和 gray 来标识 2 个版本。

实现的整体思路:

  1. 编写带版本号的灰度路由(负载均衡策略)
  2. 编写自定义 filter
  3. nacos 服务配置需要灰度发布的服务的元数据信息以及权重(在服务 jar 中配置)

注意, 应该先修改 nacos 配置实现动态路由, 然后再升级灰度节点. 本案例只是简单示例灰度原理。

下面进行网关配置

依赖配置

首先排除掉默认的 ribbon 依赖

        <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><exclusions><exclusion><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></exclusion></exclusions></dependency>

引入官方新的负载均衡包

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency>

负载均衡策略

public class VersionGrayLoadBalancer implements ReactorServiceInstanceLoadBalancer {private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;private String serviceId;private final AtomicInteger position;public VersionGrayLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, String serviceId) {this(serviceInstanceListSupplierProvider, serviceId, new Random().nextInt(1000));}public VersionGrayLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, String serviceId, int seedPosition) {this.serviceId = serviceId;this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;this.position = new AtomicInteger(seedPosition);}@Overridepublic Mono<Response<ServiceInstance>> choose(Request request) {HttpHeaders headers = (HttpHeaders) request.getContext();ServiceInstanceListSupplier supplier = this.serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);return ((Flux) supplier.get()).next().map(list -> processInstanceResponse((List<ServiceInstance>) list, headers));}private Response<ServiceInstance> processInstanceResponse(List<ServiceInstance> instances, HttpHeaders headers) {if (instances.isEmpty()) {return new EmptyResponse();} else {String reqVersion = headers.getFirst("version");if (StringUtils.isEmpty(reqVersion)) {return processRibbonInstanceResponse(instances);}List<ServiceInstance> serviceInstances = instances.stream().filter(instance -> reqVersion.equals(instance.getMetadata().get("version"))).collect(Collectors.toList());if (serviceInstances.size() > 0) {return processRibbonInstanceResponse(serviceInstances);} else {return processRibbonInstanceResponse(instances);}}}/*** 负载均衡器* 参考 org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer#getInstanceResponse** @author javadaily*/private Response<ServiceInstance> processRibbonInstanceResponse(List<ServiceInstance> instances) {int pos = Math.abs(this.position.incrementAndGet());ServiceInstance instance = instances.get(pos % instances.size());return new DefaultResponse(instance);}
}

过滤器加载负载均衡

public class GrayReactiveLoadBalancerClientFilter implements GlobalFilter, Ordered {  private static final Log log = LogFactory.getLog(ReactiveLoadBalancerClientFilter.class);  private static final int LOAD_BALANCER_CLIENT_FILTER_ORDER = 10150;  private final LoadBalancerClientFactory clientFactory;  private LoadBalancerProperties properties;  public GrayReactiveLoadBalancerClientFilter(LoadBalancerClientFactory clientFactory, LoadBalancerProperties properties) {  this.clientFactory = clientFactory;  this.properties = properties;  }  @Override  public int getOrder() {  return LOAD_BALANCER_CLIENT_FILTER_ORDER;  }  @Override  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {  URI url = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR);  String schemePrefix = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR);  if (url != null && ("grayLb".equals(url.getScheme()) || "grayLb".equals(schemePrefix))) {  ServerWebExchangeUtils.addOriginalRequestUrl(exchange, url);  if (log.isTraceEnabled()) {  log.trace(ReactiveLoadBalancerClientFilter.class.getSimpleName() + " url before: " + url);  }  return this.choose(exchange).doOnNext((response) -> {  if (!response.hasServer()) {  throw NotFoundException.create(this.properties.isUse404(), "Unable to find instance for " + url.getHost());  } else {  URI uri = exchange.getRequest().getURI();  String overrideScheme = null;  if (schemePrefix != null) {  overrideScheme = url.getScheme();  }  DelegatingServiceInstance serviceInstance = new DelegatingServiceInstance((ServiceInstance) response.getServer(), overrideScheme);  URI requestUrl = this.reconstructURI(serviceInstance, uri);  if (log.isTraceEnabled()) {  log.trace("LoadBalancerClientFilter url chosen: " + requestUrl);  }  exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, requestUrl);  }  }).then(chain.filter(exchange));  } else {  return chain.filter(exchange);  }  }  protected URI reconstructURI(ServiceInstance serviceInstance, URI original) {  return LoadBalancerUriTools.reconstructURI(serviceInstance, original);  }  private Mono<Response<ServiceInstance>> choose(ServerWebExchange exchange) {  URI uri = (URI) exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR);  VersionGrayLoadBalancer loadBalancer = new VersionGrayLoadBalancer(clientFactory.getLazyProvider(uri.getHost(), ServiceInstanceListSupplier.class), uri.getHost());  if (loadBalancer == null) {  throw new NotFoundException("No loadbalancer available for " + uri.getHost());  } else {  return loadBalancer.choose(this.createRequest(exchange));  }  }  private Request createRequest(ServerWebExchange exchange) {  HttpHeaders headers = exchange.getRequest().getHeaders();  Request<HttpHeaders> request = new DefaultRequest<>(headers);  return request;  }  
}

注入过滤器

@Configuration
public class GrayGatewayReactiveLoadBalancerClientAutoConfiguration {@Bean@ConditionalOnMissingBean({GrayReactiveLoadBalancerClientFilter.class})public GrayReactiveLoadBalancerClientFilter grayReactiveLoadBalancerClientFilter(LoadBalancerClientFactory clientFactory, LoadBalancerProperties properties) {return new GrayReactiveLoadBalancerClientFilter(clientFactory, properties);}
}

发布灰度服务

生产环境配置文件 application-prod.yml

server:  port: 9002  
spring:  application:  name: provider  cloud:  nacos:  config:  server-addr: 127.0.0.1:8848  discovery:  metadata:  version: prod  server-addr: 127.0.0.1:8848

灰度环境配置文件 application-gray.yml

server:  port: 9003  
spring:  application:  name: provider  cloud:  nacos:  config:  server-addr: 127.0.0.1:8848  discovery:  metadata:  version: gray  server-addr: 127.0.0.1:8848

idea 启动参数指定配置文件

image.png

同时注意配置两个环境的 nacos 文件(prod version: 4,gray version: 5

image.png

测试

然后分别启动三个服务:9000 端口网关 gateway 服务,9002 端口生产环境 provider-prod 服务,9003 端口灰度环境 provider-gray 服务

E:\Nacos\nacos>curl http://127.0.0.1:9000/provider/test/port
port=9003, version=5
E:\Nacos\nacos>curl -X GET -H "version:prod" http://127.0.0.1:9000/provider/test/port
port=9003, version=5
E:\Nacos\nacos>curl -X GET -H "version:gray" http://127.0.0.1:9000/provider/test/port
port=9002, version=4

仓库地址

如果对于文章中代码有疑问,可以直接查看下方作者仓库

仓库地址:ReturnTmp/spring-gateway-demo: 网关配置 + 灰度发布 + 配置中心 示例仓库 (github.com)

参考链接

  • Spring Cloud Gateway+nacos灰度发布 - 掘金 (juejin.cn)
  • SpringCloud gateway Actuator - shigp1 - 博客园 (cnblogs.com)
  • Spring Cloud Gateway监控 周立的博客
  • 前端灰度发布落地方案-腾讯云开发者社区-腾讯云 (tencent.com)
  • 后端 - 灰度发布架构设计!值得品鉴

本文由博客一文多发平台 OpenWrite 发布!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/315839.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis:原理+项目实战——Redis实战3(Redis缓存最佳实践(问题解析+高级实现))

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位大四、研0学生&#xff0c;正在努力准备大四暑假的实习 &#x1f30c;上期文章&#xff1a;Redis&#xff1a;原理项目实战——Redis实战2&#xff08;Redis实现短信登录&#xff08;原理剖析代码优化&#xff09;&#x…

Spring Boot快速搭建一个简易商城项目【四,优化购物车篇】

在之前的基础上继续将购物车进行完善&#xff1a;全选&#xff0c;删除&#xff0c;加减购物车数量 效果&#xff1a; 全选&#xff1a; 计算价格&#xff1a; //计算总价function jisuan(){let total 0;$(".th").each((i,el)>{//each遍历 i下标 el指的是当前的…

跟着动感音乐一起运动起来,健身房的动感单车中文教学

一、教程描述 目前流行的动感单车教程&#xff0c;大多数都是来自国外的&#xff0c;可能听不懂在讲些什么&#xff0c;本套教程是国内的中文教程&#xff0c;现场教学的感觉很好&#xff0c;配上健身房专用的动感单车音乐&#xff0c;很快就会唤醒全身的运动细胞&#xff0c;…

Filezilla使用

服务端 点击安装包 点击我接受 点击下一步 点击下一步 点击下一步 点击安装即可 配置用户组&#xff0c;点击编辑&#xff0c;出现组点击 点击添加&#xff0c;点击确定即可 配置用户&#xff0c;点击编辑点击用户 点击添加&#xff0c;设置用户名&#xff…

linux中最常用的帮助命令

文章目录 linux中最常用的帮助命令此man非man的意思man 的格式man的操作类似于whatis命令类似于apropos命令使用man的小技巧 你是干什么的 whatis拓展 指定目录的定位 whereis使用语法实例单独查找文件 刚刚好合适的 apropos 命令更多信息 linux中最常用的帮助命令 仅个人想法&…

2023-12-27 LeetCode每日一题(保龄球游戏的获胜者)

2023-12-27每日一题 一、题目编号 2660. 保龄球游戏的获胜者二、题目链接 点击跳转到题目位置 三、题目描述 给你两个下标从 0 开始的整数数组 player1 和 player2 &#xff0c;分别表示玩家 1 和玩家 2 击中的瓶数。 保龄球比赛由 n 轮组成&#xff0c;每轮的瓶数恰好为…

IO作业2.0

思维导图 1> 使用fread、fwrite完成两个文件的拷贝 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, const char *argv[]) {if(argc ! 3) //判断外部参数 {printf("The terminal format is incorrect\n");r…

k8s-yaml格式

三种常见的项目发布方式&#xff1a; 蓝绿发布&#xff1a; 金丝雀发布&#xff08;灰度发布&#xff09;&#xff1a; 滚动发布&#xff1a; 应用程序升级&#xff0c;面临的最大的问题&#xff0c;就是新旧业务的更换&#xff0c;立项--定稿--需求发布--开发--测试--发布&…

纠删码ReedSolomon

随着大数据技术的发展&#xff0c;HDFS作为Hadoop的核心模块之一得到了广泛的应用。为了数据的可靠性&#xff0c;HDFS通过多副本机制来保证。在HDFS中的每一份数据都有两个副本&#xff0c;1TB的原始数据需要占用3TB的磁盘空间&#xff0c;存储利用率只有1/3。而且系统中大部分…

NGUI基础-图集制作(保姆级教程)

目录 图集是什么 如何打开图集制作工具 制作步骤 图集的三个关键配置 相关参数介绍 Atlas Material Texture Padding Tim Alpha PMA shader Unity Packer TrueColor Auto-upgrade Force Square Pre-processor 图集是什么 Unity图集&#xff08;Sprite Atlas&…

Jupyter Notebook的10个常用扩展介绍

Jupyter Notebook&#xff08;前身为IPython Notebook&#xff09;是一种开源的交互式计算和数据可视化的工具&#xff0c;广泛用于数据科学、机器学习、科学研究和教育等领域。它提供了一个基于Web的界面&#xff0c;允许用户创建和共享文档&#xff0c;这些文档包含实时代码、…

erp管理系统哪家好?有什么好用的ERP推荐吗?

“公司属于工业制造&#xff0c;想要实现产供销一体化&#xff0c;生产自动排产&#xff0c;减少缺料停工&#xff0c;降低制造成本&#xff0c;订单准时交付&#xff0c;质量全程追溯。大家有推荐的ERP系统嘛&#xff1f;” 不少企业在落地ERP的时候&#xff0c;出现了很多问…