当前位置:网站首页>Gin framework: implementing service end flow limiting Middleware

Gin framework: implementing service end flow limiting Middleware

2022-06-24 01:47:00 Trespass

Introduce

Through a complete example , Based on Gin Framework in microservices 【 Current limiting 】 middleware .

We will use rk-boot To start up Gin Microservices of the framework .

Please visit the following address for a complete tutorial :https://rkdocs.netlify.app/cn

install

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

Quick start

1. establish boot.yaml

In order to verify , We launched the following options :

  • commonService:commonService It contains a series of general API. details

We aim at /rk/v1/healthy Carry out current limiting treatment , Set to 0, other API Set to 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. establish 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. Folder structure

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

0 directories, 4 files

4. start-up main.go

$ go run main.go

5. verification

  • send out /rk/v1/healthy request , Will be restricted
$ curl -X GET localhost:8080/rk/v1/healthy
{
    "error":{
        "code":429,
        "status":"Too Many Requests",
        "message":"",
        "details":[
            "slow down your request"
        ]
    }
}
  • send out /rk/v1/info request , normal
$ 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 Options

name

describe

type

The default value is

gin.interceptors.rateLimit.enabled

Start the current limiting middleware

boolean

false

gin.interceptors.rateLimit.algorithm

Current limiting algorithm , Support tokenBucket and leakyBucket

string

tokenBucket

gin.interceptors.rateLimit.reqPerSec

Global current limit

int

0

gin.interceptors.rateLimit.paths.path

HTTP route

string

""

gin.interceptors.rateLimit.paths.reqPerSec

be based on HTTP The current limit of the path

int

0

原网站

版权声明
本文为[Trespass ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211114041412831H.html

随机推荐