diff --git a/doc/wiki-cn/blademaster-mid.md b/doc/wiki-cn/blademaster-mid.md index c5f8df358..892d12046 100644 --- a/doc/wiki-cn/blademaster-mid.md +++ b/doc/wiki-cn/blademaster-mid.md @@ -103,6 +103,32 @@ func Example() { } ``` +# 内置中间件 + +## 自适应限流 + +更多关于自适应限流的信息,请参考:[kratos 自适应限流](/doc/wiki-cn/ratelimit.md) + +```go +func Example() { + myHandler := func(ctx *bm.Context) { + mid := metadata.Int64(ctx, metadata.Mid) + ctx.JSON(fmt.Sprintf("%d", mid), nil) + } + + + e := bm.DefaultServer(nil) + + // 挂载自适应限流中间件到 bm engine,使用默认配置 + limiter := bm.NewRateLimiter(nil) + e.Use(limiter.Limit()) + + e.GET("/user", myHandler) + + e.Start() +} +``` + # 扩展阅读 [bm快速开始](blademaster-quickstart.md) [bm模块说明](blademaster-mod.md) [bm基于pb生成](blademaster-pb.md) diff --git a/doc/wiki-cn/warden-mid.md b/doc/wiki-cn/warden-mid.md index 6c0fdb5f8..8609d32f6 100644 --- a/doc/wiki-cn/warden-mid.md +++ b/doc/wiki-cn/warden-mid.md @@ -320,6 +320,52 @@ func serverLogging() grpc.UnaryServerInterceptor { } ``` +# 内置拦截器 + +## 自适应限流拦截器 + +更多关于自适应限流的信息,请参考:[kratos 自适应限流](/doc/wiki-cn/ratelimit.md) + +```go +package grpc + +import ( + pb "kratos-demo/api" + "kratos-demo/internal/service" + "github.com/bilibili/kratos/pkg/conf/paladin" + "github.com/bilibili/kratos/pkg/net/rpc/warden" + "github.com/bilibili/kratos/pkg/net/rpc/warden/ratelimiter" +) + +// New new a grpc server. +func New(svc *service.Service) *warden.Server { + var rc struct { + Server *warden.ServerConfig + } + if err := paladin.Get("grpc.toml").UnmarshalTOML(&rc); err != nil { + if err != paladin.ErrNotExist { + panic(err) + } + } + ws := warden.NewServer(rc.Server) + + // 挂载自适应限流拦截器到 warden server,使用默认配置 + limiter := ratelimiter.New(nil) + ws.Use(limiter.Limit()) + + // 注意替换这里: + // RegisterDemoServer方法是在"api"目录下代码生成的 + // 对应proto文件内自定义的service名字,请使用正确方法名替换 + pb.RegisterDemoServer(ws.Server(), svc) + + ws, err := ws.Start() + if err != nil { + panic(err) + } + return ws +} +``` + # 扩展阅读 [warden快速开始](warden-quickstart.md) [warden基于pb生成](warden-pb.md) [warden负载均衡](warden-balancer.md) [warden服务发现](warden-resolver.md) diff --git a/pkg/net/rpc/warden/internal/ratelimiter/ratelimiter.go b/pkg/net/rpc/warden/ratelimiter/ratelimiter.go similarity index 100% rename from pkg/net/rpc/warden/internal/ratelimiter/ratelimiter.go rename to pkg/net/rpc/warden/ratelimiter/ratelimiter.go