Spring Cloud Gateway(七):路由谓词工厂WeightRoutePredicateFactory
本文基于 spring cloud gateway 2.0.1 接上文
5、基于路由权重(weigth)的谓词工厂
Spring Cloud Gateway 提供了基于路由权重的断言工厂,配置时指定分组和权重值 即可。WeightRoutePredicateFactory 实现了路由权重的功能,按照路由权重选择 同一个分组中的路由。
spring:
cloud:
gateway:
locator:
enabled: true
routes:
-id: weight_route1
uri: http://www.baidu1.com
order: 6000
predicates:
-Weight= group3, 1
-Path=/ weight/**
filters:
-StripPrefix= 2
-id: weight_route2
uri: http://www.baidu2.com
order: 6000
predicates:
-Path=/ weight/**
-Weight= group3, 9
filters:
-StripPrefix=1
如上配置了两个对于 / weight/** 路径转发的路由定义,这两个路由是同一个权重分组,且 weight_ route1 权重为 1, weight_ route2 权重为9。 对于10个访问/ weight/** 路径的请求来说,将会有9个路由到 weight_ route2,1个路由到 weight_ route1。
5.1、WeightRoutePredicateFactory 权重算法实现过程
weight_ route1: group3, 1
weight_ route2: group3, 9
实现过程为:
1)构造 weights(group3)数组:weights=[ 1, 9]
2)normalize: weights= weights/ sum( weights)=[ 0. 1, 0. 9]
3)计算区间范围: ranges= weights. collect( 0,( s, w)-> s+ w)=[ 0, 0. 1, 1. 0]
4)生成 随机数: r= random()
5)搜索随机数所在的区间: i= integer s. t. r>= ranges[ i]&& r< ranges[ i+ 1]
6)选择相应的路由: routes[ i]
网关应用服务在启动时会发布 WeightDefinedEvent,在 WeightCalculatorWebFilter 过滤器中定义了该时间监听器,当接收到时间 WeightDefinedEvent 时,会自动添加 WeightConfig 到权重配置中。请求在经过 WeightCalculatorWebFilter 时会 生成 一个 随机数, 根据随机数所在的区间选择对应分组的路由。
// WeightRoutePredicateFactory.java
@Override
public Predicate<ServerWebExchange> apply(Config config) {
List<IpSubnetFilterRule> sources = convert(config.sources);
return exchange -> {
InetSocketAddress remoteAddress = config.remoteAddressResolver.resolve(exchange);
if (remoteAddress != null) {
String hostAddress = remoteAddress.getAddress().getHostAddress();
String host = exchange.getRequest().getURI().getHost();
if (log.isDebugEnabled() && !hostAddress.equals(host)) {
log.debug("Remote addresses didn't match " + hostAddress + " != " + host);
}
for (IpSubnetFilterRule source : sources) {
if (source.matches(remoteAddress)) {
return true;
}
}
}
return false;
};
}
当应用到配置的路由断言 WeightRoutePredicate 时,会根据 ServerWebExchange 中的 WEIGHT_ ATTR 值,判断当前的 routeId 与对应分组的 routeId 是否一致。
Spring Cloud Gateway(七):路由谓词工厂WeightRoutePredicateFactory的更多相关文章
- Spring Cloud Alibaba学习笔记(17) - Spring Cloud Gateway 自定义路由谓词工厂
在前文中,我们介绍了Spring Cloud Gateway内置了一系列的路由谓词工厂,但是如果这些内置的路由谓词工厂不能满足业务需求的话,我们可以自定义路由谓词工厂来实现特定的需求. 例如有某个服务 ...
- 通过Nacos动态刷新Spring Cloud Gateway的路由
通过Nacos动态刷新Spring Cloud Gateway的路由 一.背景 二.解决方案 三.实现功能 四.实现步骤 1.网关服务的实现 1.pom文件 2.bootstrap.yml配置文件 3 ...
- springcloud3(五) spring cloud gateway动态路由的四类实现方式
写这篇博客主要是为了汇总下动态路由的多种实现方式,没有好坏之分,任何的方案都是依赖业务场景需求的,现在网上实现方式主要有: 基于Nacos, 基于数据库(PosgreSQL/Redis), 基于Mem ...
- Spring Cloud gateway 七 Sentinel 注解方式使用
Sentinel 注解支持 @SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项. @SentinelResource 注解包含以下属性: value:资 ...
- spring cloud gateway网关路由分配
1, 基于父工程,新建一个模块 2,pom文件添加依赖 <dependencies> <dependency> <groupId>org.springframewo ...
- Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂
Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...
- Spring Cloud Alibaba学习笔记(19) - Spring Cloud Gateway 自定义过滤器工厂
在前文中,我们介绍了Spring Cloud Gateway内置了一系列的内置过滤器工厂,若Spring Cloud Gateway内置的过滤器工厂无法满足我们的业务需求,那么此时就需要自定义自己的过 ...
- Spring cloud gateway 如何在路由时进行负载均衡
本文为博主原创,转载请注明出处: 1.spring cloud gateway 配置路由 在网关模块的配置文件中配置路由: spring: cloud: gateway: routes: - id: ...
- Spring Cloud Gateway(一):认识Spring Cloud Gateway
1.Spring Cloud Gateway 简介 Spring Cloud Gateway 系列目录 Spring Cloud Gateway(一):认识Spring Cloud Gateway S ...
随机推荐
- SVN_04建库
示范加入一个代码库[Repository] [1]点击Repository右键,创建一个新库 (常规FSFS存储库) [2]在下面所看到的文本框中输入库名称 只创建空的库 创建完库后,没有任何内容在里 ...
- c++中配置多个环境的格式
例:环境1=$(QTDIR)\bin 环境2=$(PATH) 环境3=E:\软件\办公\Vimba_2.1\VimbaCPP\Bin\Win64 具体配置为 PATH=$(QTDIR)\bin%3b$ ...
- shell 实战 -- 基于一个服务启动,关闭,状态检查的脚本
功能说明: check:检查服务状态,在开启,关闭,状态检查时都会用到这个函数,所以封装起来放到最前面 start:开启服务 stop:关闭服务 fstop:强制关闭 status:检查服务状态 ru ...
- HTML标签认识一
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- vue-cli脚手架——2.0版本项目案例
一.[准备工作] Node.js 安装包及源码下载地址为:https://nodejs.org/en/download/. Git 各平台安装包下载地址为:http://git-scm.com/dow ...
- Vue指令之`v-for`和`key`属性
2.2.0+ 的版本里,**当在组件中使用** v-for 时,key 现在是必须的. 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用 “**就地复用**” 策略.如果数据项 ...
- nginx的proxy模块详解以及参数
文章来源 运维公会:nginx的proxy模块详解以及参数 使用nginx配置代理的时候,肯定是要用到http_proxy模块.这个模块也是在安装nginx的时候默认安装.它的作用就是将请求转发到相应 ...
- mycat-rule
<?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apac ...
- 《TheOne团队》团队作业三:团队项目原型设计与开发
项目 内容 作业所属课程 http://www.cnblogs.com/nwnu-daizh/ 作业要求 https://www.cnblogs.com/nwnu-daizh/p/10761596.h ...
- python 'NoneType' object has no attribute 'get'
获取 页面链接的时候报错 'NoneType' object has no attribute 'get' href = div.find("a").get("href& ...