当前位置:网站首页>Grpc: implement service end flow restriction

Grpc: implement service end flow restriction

2022-06-24 02:54:00 Trespass

Introduce

This article will show you how to gRPC Implementation in microservices 【 Current limiting 】 Interceptor / middleware .

We will use rk-boot To start up gRPC service .

Please visit the following address for a complete tutorial :

https://rkdev.info/cn

https://rkdocs.netlify.app/cn ( spare )

install

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

Quick start

1. establish boot.yaml

In order to verify , We started commonService,commonService Contains a series of commonly used API, for example /rk/v1/healthy.

We aim at /rk.api.v1.RkCommonService/Healthy Carry out current limiting treatment , Set to 0, other API Set to 100.

---
grpc:
  - name: greeter                   # Name of grpc entry
    port: 8080                      # Port of grpc entry
    enabled: true                   # Enable grpc entry
    commonService:
      enabled: true                 # Enable common service for testing
    interceptors:
      rateLimit:
        enabled: true
        reqPerSec: 100
#        algorithm: "leakyBucket"
        paths:
          - path: "/rk.api.v1.RkCommonService/Healthy"
            reqPerSec: 0

2. establish main.go

package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	_ "github.com/rookie-ninja/rk-grpc/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 a request to /rk/v1/healthy, Will be Current limiting .
{
    "code":8,
    "message":"Slow down your request.",
    "details":[
        {
            "@type":"type.googleapis.com/rk.api.v1.ErrorDetail",
            "code":8,
            "status":"ResourceExhausted",
            "message":"[from-grpc] Slow down your request."
        }
    ]
}
  • Send a request to /rk/v1/info, normal
{
    "appName":"demo",
    "az":"",
    "description":"Internal RK entry which describes application with fields of appName, version and etc.",
    "docsUrl":[

    ],
    "domain":"",
    "gid":"20",
    "homeUrl":"",
    "iconUrl":"",
    "keywords":[

    ],
    "maintainers":[

    ],
    "realm":"",
    "region":"",
    "startTime":"2021-10-25T01:15:48+08:00",
    "uid":"501",
    "upTimeSec":76,
    "upTimeStr":"1 minute",
    "username":"Dongxun Yin",
    "version":"master-557da30"
}

YAML Options

name

describe

type

The default value is

grpc.interceptors.rateLimit.enabled

Start current limiting Lan Jie

boolean

false

grpc.interceptors.rateLimit.algorithm

Current limiting algorithm , Support tokenBucket and leakyBucket

string

tokenBucket

grpc.interceptors.rateLimit.reqPerSec

Global current limit

int

0

grpc.interceptors.rateLimit.paths.path

gRPC Method path

string

""

grpc.interceptors.rateLimit.paths.reqPerSec

be based on gRPC The current limit of the method path

int

0

原网站

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