Gin 框架:实现服务端限流中间件

1,468 阅读1分钟

这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战

介绍

通过一个完整例子,在基于 Gin 框架的微服务中实现【限流】中间件。

我们将会使用 rk-boot 来启动 Gin 框架的微服务。

请访问如下地址获取完整教程:rkdocs.netlify.app/cn

安装

go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-gin

快速开始

1.创建 boot.yaml

为了验证,我们启动了如下几个选项:

  • commonService:commonService 里包含了一系列通用 API。详情

我们针对 /rk/v1/healthy 进行限流处理,设置成 0,其他 API 设置成 100。

---
gin:
  - name: greeter                     # Required
    port: 8080                        # Required
    enabled: true                     # Required
    commonService:
      enabled: true                   # Optional, default: false
    interceptors:
      rateLimit:
        enabled: true
        reqPerSec: 100
        paths:
          - path: "/rk/v1/healthy"
            reqPerSec: 0

2.创建 main.go

// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
        _ "github.com/rookie-ninja/rk-gin/boot"
)

// Application entrance.
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

3.文件夹结构

$ tree
.
├── boot.yaml
├── go.mod
├── go.sum
└── main.go

0 directories, 4 files

4.启动 main.go

$ go run main.go

5.验证

  • 发送 /rk/v1/healthy 请求,会被限流
$ curl -X GET localhost:8080/rk/v1/healthy
{
    "error":{
        "code":429,
        "status":"Too Many Requests",
        "message":"",
        "details":[
            "slow down your request"
        ]
    }
}
  • 发送 /rk/v1/info 请求,正常
$ curl -X GET localhost:8080/rk/v1/info
{
    "appName":"rk-demo",
    "version":"master-2c9c6fd",
    "description":"Internal RK entry which describes application with fields of appName, version and etc.",
    "keywords":[],
    "homeUrl":"",
    "iconUrl":"",
    "docsUrl":[],
    "maintainers":[],
    "uid":"501",
    "gid":"20",
    "username":"Dongxun Yin",
    "startTime":"2021-11-14T00:32:09+08:00",
    "upTimeSec":123,
    "upTimeStr":"2 minutes",
    "region":"",
    "az":"",
    "realm":"",
    "domain":""
}

YAML 选项

名字描述类型默认值
gin.interceptors.rateLimit.enabled启动限流中间件booleanfalse
gin.interceptors.rateLimit.algorithm限流算法, 支持 tokenBucket 和 leakyBucketstringtokenBucket
gin.interceptors.rateLimit.reqPerSec全局限流值int0
gin.interceptors.rateLimit.paths.pathHTTP 路径string""
gin.interceptors.rateLimit.paths.reqPerSec基于 HTTP 路径的限流值int0