当前位置:网站首页>阿里云 MSE 支持 Go 语言流量防护
阿里云 MSE 支持 Go 语言流量防护
2022-07-28 16:02:00 【InfoQ】
概述
什么是限流降级

- Target: 针对什么样的流量
- Strategy: 限流降级的策略
- FallbackAction: 触发后的行为

如何使用 MSE 限流降级
应用接入
- 下载**MSE Go SDK[1]**,解压到项目的./pkg-custom/github.com/aliyun/aliyun-mse-go-sdk-v1.0.7 目录。
- 可以参考 SDK 的 example 目录下的例子,来进行接入。
- 在 go.mod 文件中,添加如下依赖声明:
require (
github.com/aliyun/aliyun-mse-go-sdk v1.0.7
)
replace github.com/aliyun/aliyun-mse-go-sdk => ./pkg-custom/github.com/aliyun/aliyun-mse-go-sdk
- 在应用的启动过程中,添加 MSE SDK 初始化命令:
import (
mse_sdk "github.com/aliyun/aliyun-mse-go-sdk"
)
// 在应用的初始化逻辑中加入以下代码。
// Sentinel core的初始化包含在了这里面。如果之前有调用过Sentinel的初始化函数,需要去掉。
err := mse_sdk.InitMseDefault
if err != nil {
log.Fatalf("Failed to init MSE: %+v", err)
}
应用部署


资源定义
- Sentinel 定义资源
import (
sentinel "github.com/alibaba/sentinel-golang/api"
)
// Entry 方法用于埋点
e, b := sentinel.Entry("your-resource-name", sentinel.WithTrafficType(base.Inbound))
if b != nil {
// 请求被流控,可以从 BlockError 中获取限流详情
} else {
// 请求可以通过,在此处编写您的业务逻辑
// 务必保证业务逻辑结束后 Exit
e.Exit()
}
- dubbo-go 方式定义资源
import (
_ "github.com/alibaba/sentinel-golang/adapter/dubbo"
)
- Dubbo 应用是在代码中通过 import 包的形式引入 Dubbo adapter,其中的 init()函数会自动注入相关 filter。Dubbo-Go 版本需要≥1.3.0。Sentinel Dubbo adapter 会自动统计所有 provider 和 consumer 的调用。
- gRPC应用接入
import (
sentinelPlugin "github.com/alibaba/sentinel-golang/adapter/grpc"
"google.golang.org/grpc"
)
s := grpc.NewServer(grpc.UnaryInterceptor(sentinelPlugin.NewUnaryServerInterceptor()))
- gRPC 应用在 gRPC 的初始化代码中引入 Sentinel 提供的 interceptor,Sentinel 针对 Server 和 Client 都提供了 unary 和 streaming 两种 interceptor,以上代码以 Server 端为例。默认的限流处理逻辑是返回 Sentinel 的 BlockError。您也可以在创建 interceptor 时提供自定义的 fallback 处理逻辑。
- Gin Web 应用接入
import (
sentinelPlugin "github.com/alibaba/sentinel-golang/adapter/gin"
"github.com/gin-gonic/gin"
)
r := gin.New()
r.Use(sentinelPlugin.SentinelMiddleware())
- Gin Web 应用在 Gin 的初始化代码中引入 SentinelMiddleware。Sentinel 会对每个 API route 进行统计,资源名称类似于 GET:/foo/:id。默认的限流处理逻辑是返回 429 (Too Many Requests)错误码。
- Micro应用接入
import (
sentinelPlugin "github.com/alibaba/sentinel-golang/adapter/micro"
"github.com/micro/go-micro/v2"
)
svc := micro.NewService(micro.WrapHandler(sentinelPlugin.NewHandlerWrapper()))
- 在 Go-Micro 的初始化代码中引入 Sentinel 提供的 wrapper。Sentinel 针对 Go-Micro Server 和 Client 都提供了 wrapper。以上代码以 Server端为例。埋点默认会提取服务 method 作为资源名,默认的流控处理逻辑是返回 Sentinel 的 BlockError。您也可以在创建 wrapper 时提供自定义的 fallback 处理逻辑。
如何配置限流降级规则
- 查看应用



- 配置流控规则


- 配置隔离规则

- 配置熔断规则

- 配置热点规则

- 针对一段时间内最频繁购买的商品 ID 进行限制,防止击穿缓存而导致大量请求到数据库的情形。
- 针对一段时间内频繁访问的用户 ID 进行限制,防止恶意刷单。
MSE 微服务治理规划
边栏推荐
- Fx3 development board and schematic diagram
- 有趣的 Kotlin 0x0A:Fun with composition
- Asp.net large file block upload breakpoint resume demo
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- Ansa secondary development - Introduction to interface development tools
- Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
- Applet: get element node information
- Analysis of echo service model in the first six chapters of unp
- 微软:Edge 浏览器已内置磁盘缓存压缩技术,可节省空间占用且不降低系统性能
- 智慧园区是未来发展的趋势吗?
猜你喜欢

Interesting kotlin 0x08:what am I

Microsoft: edge browser has built-in disk cache compression technology, which can save space and not reduce system performance

排序1-插入排序与希尔排序

【深度学习】:《PyTorch入门到项目实战》第四天:从0到1实现logistic回归(附源码)

ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境

排序2-冒泡排序与快速排序(递归加非递归讲解)

在vs code上配置Hypermesh二次开发环境

关于 CMS 垃圾回收器,你真的懂了吗?

Interesting kotlin 0x0a:fun with composition

HM secondary development - data names and its use
随机推荐
【从零开始学习SLAM】将坐标系变换关系发布到 topic tf
MySQL CDC if the binlog log file is incomplete, can you read all the data in the full volume stage
Mysql与Oracle的13点区别
微软:Edge 浏览器已内置磁盘缓存压缩技术,可节省空间占用且不降低系统性能
Analysis of echo service model in the first six chapters of unp
有趣的 Kotlin 0x08:What am I
给定正整数N、M,均介于1~10 ^ 9之间,N <= M,找出两者之间(含N、M)的位数为偶数的数有多少个
parseJson
Tcp/ip related
“蔚来杯“2022牛客暑期多校训练营3 J.Journey 0-1最短路
PHP calculate coordinate distance
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
Sort 1-insert sort and Hill sort
Some suggestions on optimizing HyperMesh script performance
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
Installation of QT learning
“蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
Splash (rendering JS service) introduction installation
【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
Introduction and implementation of queue (detailed explanation)