INFINI Gateway 如何防止大跨度查询

背景

业务每天生成一个日期后缀的索引,写入当日数据。

业务查询有时会查询好多天的数据,导致负载告警。

现在想对查询进行限制--只允许查询一天的数据(不限定是哪天),如果想查询多天的数据就走申请。

技术分析

在每天一个索引的情况下,要进行多天的数据查询,有三种途径:

  1. 查询时,指定多个索引
  2. 查询时,写前缀+*号,模糊匹配多个索引
  3. 查询别名,别名关联多个索引

需求实现

我们只需用网关代理 ES 集群,并在 default_flow 中增加一段 request_path_filter 过滤器的配置,只允许查询一个索引且格式如 "xxx-2023-12-06", "xxx.2023.12.06", "xxx20231206" 。

      - request_path_filter:
           message: "Query scope exceeds limit, please contact the administrator for application."
           must:
             suffix:
                - _search
             regex:
                - \/[a-z]+[-.]?\d{4}[-.]?\d{1,2}[-.]?\d{1,2}\/

如果需要指定其他格式,请自行修改 regex 的正则表达式。

创建测试索引

INFINI Console 开发工具中执行下列语句:

POST test-2023-12-06/_doc
{
  "test":"test"
}

POST test-2023-12-6/_doc
{
  "test":"test"
}
POST test.2023.12.06/_doc
{
  "test":"test"
}
POST test.2023.12.6/_doc
{
  "test":"test"
}

POST test20231206/_doc
{
  "test":"test"
}

POST test/_doc
{
  "test":"test"
}

查询测试语句

#预计成功的查询
curl localhost:8000/test-2023-12-06/_search?pretty
curl localhost:8000/test-2023-12-6/_search?pretty
curl localhost:8000/test.2023.12.06/_search?pretty
curl localhost:8000/test.2023.12.6/_search?pretty
curl localhost:8000/test20231206/_search?pretty
#预计失败的查询
curl localhost:8000/test-2023-12-06,test-2023-12-6/_search?pretty
curl localhost:8000/test-2023-12*/_search?pretty
curl localhost:8000/test*/_search?pretty
curl localhost:8000/*/_search?pretty

查询结果

预计成功的查询
预计失败的查询

此外,我们在 Console 中的 Request Analysis 看板中也能看到,哪些请求被拒绝,哪些请求被"放行"。

查询多个索引(多天)

现在我们已经实现了业务只能查一个索引,即一天的数据。当业务需要查询多天的索引时,我们只需创建一个别名,关联多个索引就行了。注意别名也要符合格式要求:字母开头 + 日期格式后缀。

下面我们创建一个 test-1111-1-1 的别名,关联前面的三个测试索引。

POST /_aliases
{
  "actions" : [
    { "add" : { "indices" : ["test-2023-12-06", "test.2023.12.06","test-2023-12-6"], "alias" : "test-1111-1-1" } }
  ]
}

查询别名

待业务查询用完之后,删除别名即可。

POST /_aliases
{
  "actions" : [
    { "remove": { "indices" : ["test-2023-12-06", "test.2023.12.06","test-2023-12-6"], "alias" : "test-1111-1-1" } }
  ]
}

最后,我们只需严格控制别名的创建,就能实现我们最初的需求了。

相关推荐
A ?Charis19 小时前
记录一下_treafik使用Gateway-APi使用的细节参数
gateway
运维开发王义杰4 天前
Kubernetes:EKS 中 Istio Ingress Gateway 负载均衡器配置及常见问题解析
kubernetes·gateway·istio
铭毅天下5 天前
极限网关 INFINI Gateway 从 0 到 1:简单易懂的入门教程
gateway
铭毅天下5 天前
极限网关核心架构解析:从 Nginx 到 INFINI Gateway 的演进
运维·nginx·架构·gateway
Xwzzz_5 天前
Spring Cloud Gateway中断言路由和过滤器的使用
java·spring cloud·gateway
zzyh1234565 天前
springcloudalibaba组件gateway
前端·javascript·gateway
@明明不知道5 天前
spring cloud 微服务部署(2025年)第一章:Nacos、LoadBalancer、GateWay、Ribbon集成之Nacos部署
spring cloud·微服务·nacos·gateway·loadbalancer
山海不说话7 天前
从零搭建微服务项目(第7章——微服务网关模块基础实现)
java·spring boot·spring·spring cloud·微服务·gateway
默辨7 天前
再谈SpringCloud Gateway源码
java·网关·spring cloud·gateway·reactor·webflux
飞火流星020277 天前
【动态路由】系统web url整合系列【springcloud-gateway实现】【不改hosts文件版】组件一:多个Eureka路由过滤器
gateway·gateway动态路由·gateway实现反向代理·系统url整合·eureka路由过滤器·gateway路由过滤器