暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

微服务(SpringCloud )实战之Sentinel整合Gateway实现网关限流

大数据技术天涯 2021-01-07
1233

基于Spring Cloud架构微服务平台技术点

介绍

Sentinel是阿里开源的一个框架具有熔断、限流功能,是分布式系统的流量防卫兵。

Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

来源于Sentinel Github主页介绍

Sentinel 具有以下特征:

1、丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。

2、完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。

3、广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。

4、完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

Sentinel 的主要特性


Sentinel 的开源生态

集成步骤

1、在微服务网关项目的pom文件中添加Sentinel依赖

   <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>


<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>


<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>

2、配置限流规则示例

/**
* 网关限流配置
*/
@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(Ordered.HIGHEST_PRECEDENCE)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}


@PostConstruct
public void doInit() {
// initSystemRule();
initCustomizedApis();
initGatewayRules();
}


private void initCustomizedApis() {
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("customized_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/api/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
ApiDefinition api2 = new ApiDefinition("book_content_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/api/book/queryBookContent**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
definitions.add(api1);
definitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}


/**
* 自定义网关限流规则(反爬虫机制)
* 1.对所有api接口通过IP进行限流,每个IP,2秒钟内请求数量大于10,即视为爬虫
* 2.对小说内容接口访问进行限流,每个IP,1秒钟请求数量大于1,则视为爬虫
* */
private void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("customized_api")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(10)
.setIntervalSec(2)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)
)
);
rules.add(new GatewayFlowRule("book_content_api")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(1)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)
)
);
GatewayRuleManager.loadRules(rules);
}
}

注:

1、initCustomizedApis()方法:自定义的 API 定义分组(ApiDefinition),可以看做是一些 URL 匹配的组合。

上面代码实现,可以针对不同的API 分组维度进行限流

请求 path 模式为 /api/** 归到 customized_api这个 API 分组下面

请求 path 模式为/api/book/queryBookContent** 归到book_content_api这个 API 分组下面

2、initGatewayRules()方法:自定义网关限流规则,可以针对不同 route 或自定义的 API 分组进行限流,支持针对请求中的参数、Header、来源 IP 等进行定制化的限流。

customized_api这个 API 分组,对所有api接口通过IP进行限流,每个IP,2秒钟内请求数量大于10,即视为爬虫,进行限流。

book_content_api这个 API 分组,对小说内容接口访问进行限流,每个IP,1秒钟请求数量大于1,则视为爬虫,进行限流。


文章转载自大数据技术天涯,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论