前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Gin 框架:实现服务端限流中间件

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

原创
作者头像
尹东勋
修改2021-12-13 00:32:52
1.2K0
修改2021-12-13 00:32:52
举报

介绍

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

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

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

安装

代码语言:txt
复制
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。

代码语言:txt
复制
---
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

代码语言:txt
复制
// 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.文件夹结构

代码语言:txt
复制
$ tree
.
├── boot.yaml
├── go.mod
├── go.sum
└── main.go

0 directories, 4 files

4.启动 main.go

代码语言:txt
复制
$ go run main.go

5.验证

  • 发送 /rk/v1/healthy 请求,会被限流
代码语言:txt
复制
$ curl -X GET localhost:8080/rk/v1/healthy
{
    "error":{
        "code":429,
        "status":"Too Many Requests",
        "message":"",
        "details":[
            "slow down your request"
        ]
    }
}
  • 发送 /rk/v1/info 请求,正常
代码语言:txt
复制
$ 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

启动限流中间件

boolean

false

gin.interceptors.rateLimit.algorithm

限流算法, 支持 tokenBucket 和 leakyBucket

string

tokenBucket

gin.interceptors.rateLimit.reqPerSec

全局限流值

int

0

gin.interceptors.rateLimit.paths.path

HTTP 路径

string

""

gin.interceptors.rateLimit.paths.reqPerSec

基于 HTTP 路径的限流值

int

0

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 安装
  • 快速开始
    • 1.创建 boot.yaml
      • 2.创建 main.go
        • 3.文件夹结构
          • 4.启动 main.go
            • 5.验证
            • YAML 选项
            相关产品与服务
            消息队列 TDMQ
            消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档