这里写目录标题
一级目录
二级目录
三级目录
一. 前言
2019年4月25号,Alibaba Sentinel Dashboard1.6正式发布,带来 Spring Cloud Gateway 支持、控制台登录功能、改进的热点限流和注解 fallback 等多项新特性。

需要注意的是
Alibaba Sentinel Dashboard的jar中包含Sentinel,且版本对照一致。
二.Sentinel网关流控实现原理
当通过 GatewayRuleManager 加载网关流控规则(GatewayFlowRule)时,无论是否针对请求属性进行限流,Sentinel 底层都会将网关流控规则转化为热点参数规则(ParamFlowRule),存储在GatewayRuleManager 中,与正常的热点参数规则相隔离。转换时 Sentinel 会根据请求属性配置,为网关流控规则设置参数索引(idx),并同步到生成的热点参数规则中。
外部请求进入 API Gateway 时会经过 Sentinel 实现的 filter,其中会依次进行 路由/API 分组匹配、请求属性解析和参数组装。Sentinel 会根据配置的网关流控规则来解析请求属性,并依照参数索引顺序组装参数数组,最终传入SphU.entry(res, args) 中。Sentinel API Gateway Adapter Common 模块向 Slot Chain 中添加了一个 GatewayFlowSlot,专门用来做网关规则的检查。GatewayFlowSlot 会从GatewayRuleManager中提取生成的热点参数规则,根据传入的参数依次进行规则检查。若某条规则不针对请求属性,则会在参数最后一个位置置入预设的常量,达到普通流控的效果。

三Sentinel文档
Sentinel 1.6.0 引入了 Sentinel API Gateway Adapter Common 模块,此模块中包含网关限流的规则和自定义 API 的实体和管理逻辑:
GatewayFlowRule:网关限流规则,针对 API Gateway 的场景定制的限流规则,可以针对不同 route 或自定义的 API 分组进行限流,支持针对请求中的参数、Header、来源 IP 等进行定制化的限流。
ApiDefinition:用户自定义的 API 定义分组,可以看做是一些 URL 匹配的组合。比如我们可以定义一个 API 叫 myapi,请求 path 模式为 /foo/ 和 /baz/ 的都归到 myapi 这个 API 分组下面。限流的时候可以针对这个自定义的 API 分组维度进行限流。
其中网关限流规则 GatewayFlowRule 的字段解释如下:
• resource:资源名称,可以是网关中的 route 名称或者用户自定义的 API 分组名称。
• resourceMode:规则是针对 API Gateway 的 route(RESOURCEMODEROUTEID)还是用户在 Sentinel 中定义的 API 分组(RESOURCEMODECUSTOMAPI_NAME),默认是 route。
• grade:限流指标维度,同限流规则的 grade 字段
• count:限流阈值
• intervalSec:统计时间窗口,单位是秒,默认是 1 秒
• controlBehavior:流量整形的控制效果,同限流规则的 controlBehavior 字段,目前支持快速失败和匀速排队两种模式,默认是快速失败。
• burst:应对突发请求时额外允许的请求数目。
• maxQueueingTimeoutMs:匀速排队模式下的最长排队时间,单位是毫秒,仅在匀速排队模式下生效。
• paramItem:参数限流配置。若不提供,则代表不针对参数进行限流,该网关规则将会被转换成普通流控规则;否则会转换成热点规则。其中的字段:
• parseStrategy:从请求中提取参数的策略,目前支持提取来源 IP(PARAMPARSESTRATEGYCLIENTIP)、Host(PARAMPARSESTRATEGYHOST)、任意 Header(PARAMPARSESTRATEGYHEADER)和任意 URL 参数(PARAMPARSESTRATEGYURLPARAM)四种模式。
• fieldName:若提取策略选择 Header 模式或 URL 参数模式,则需要指定对应的 header 名称或 URL 参数名称。
• pattern 和 matchStrategy:为后续参数匹配特性预留,目前未实现。
用户可以通过 GatewayRuleManager.loadRules(rules) 手动加载网关规则,或通过 GatewayRuleManager.register2Property(property) 注册动态规则源动态推送(推荐方式)。
三Sentinel整合gateway模块实践测试
1依赖
在有alibaba-sentinel,spring-cloud-starter-gateway依赖的基础上, 需要增加sentinel-spring-cloud-gateway-adapter两个组件的适配器依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.6.0</version>
</dependency>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>FengboSoft</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-gateway-global</artifactId>
<dependencies>
<!--gateway依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<!--nacos客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacosconfig依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--阿里哨兵-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--sentinel支持gateway依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
</project>
父依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>FengboSoft</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-client-account</module>
<module>eureka-common-entity</module>
<module>eureka-system-account</module>
<module>eureka-client-finance</module>
<module>eureka-client-meter</module>
<module>eureka-gateway-global</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring-cloud-alibaba版本必须对应boot和cloud版本,否则ribbon会报错-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2 配置
#应用环境
spring.application.name=gateway
spring.profiles.active=null
#端口号
server.port=11111
#eureka的健康检查机制开关
eureka.client.healthcheck.enabled=true
#gateway
#默认为false,如果设置为True,则对全部服务起作用,就不用对单独服务配置,如果需要对单独服务进行配置,则需配置成默认值
#设置服务与发现结合,这样可以采用服务名的路由策略
spring.cloud.gateway.discovery.locator.enabled=true
#服务路由名小写开启小写验证,默认feign根据服务名查找都是用的全大写
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
#设置路由id,个性话配置服务调用网关
spring.cloud.gateway.routes[0].id=zero
#设置路由的uri,uri以lb://开头(lb=loadblance代表从注册中心获取服务)
spring.cloud.gateway.routes[0].uri=lb://accountconsumer
#设置路由断言(必须配置),代理servicerId为auth-service的/gateway/路径
spring.cloud.gateway.routes[0].predicates=Path=/gateway/**
#必须加这个配置,否则使用微服务名会调用不成功
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
#nacos
spring.cloud.nacos.discovery.server-addr=192.168.1.187:8848
spring.cloud.nacos.config.server-addr=192.168.1.187:8848
#sentinel
#sentinel控制台url
spring.cloud.sentinel.transport.dashboard=192.168.1.187:8080
#与应用交互的httpserver端口号
spring.cloud.sentinel.transport.port=8719
#取消控制台懒加载
spring.cloud.sentinel.eager=true
配置了Path路由,等会使用 http:/192.168.1.187:11111/gateway/+微服务 进行访问即可。
那么这里面的 route ID(如 zero)会被标识为 Sentinel 的资源。
3配置类
增加一个GatewayConfiguration 类,用于Sentinel配置Gateway限流要用到的类,目前是手动配置的方式,后面肯定是可以通过注解启用,配置文件中指定限流规则的方式来使用,当然这部分工作会交给Spring Cloud Alibaba来做,后面肯定会发新版本的,大家耐心等待就行了。
package fengbo.config;
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;
import java.util.Collections;
import java.util.List;
@Configuration
public class GatewayConfiguration {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
} }
4启动类
package fengbo;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Created by @author LiuChunhang on 2020/8/4.
*/
@SpringBootApplication
@EnableDiscoveryClient
public class Gateway {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Gateway.class);
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);
}
}
5测试
①使用网关访问服务若干次

②打开Sentinel Dashboard1.7.0 ,可看到 Sentinel 对 gateway已经起到了监控作用

③测试对Gateway 总流量接口 限流
sentinel Dashboard 操作

频繁访问之后,被Openfeign和微服务整合的客户端降级

目前的版本,对于gateway网关的根流量端口来说,所有请求都会通过gateway,所以被Openfeign fallback之后,sentinel上不会显示被拒绝。

④测试对route ID 限流
sentinel Dashboard 操作

频繁访问之后,直接被sentinel降级

所有请求仍然都会通过gateway,但降级处理不会被Openfeign fallback,而是在sentinel上直接fallback,而且在面板上会显示被Sentinel拒绝请求数量,Sentinel的fallback也可以自定义,不知道怎么办的小伙伴请看我过去的文章。


4034

被折叠的 条评论
为什么被折叠?



