通过一个完整例子,在基于 Echo 框架的微服务中实现【超时】中间件。
我们将会使用 rk-boot 来启动 Echo 框架的微服务。
请访问如下地址获取完整教程:
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-echo
支持全局超时和 API 超时设定。
boot.yaml 文件告诉 rk-boot 如何启动 Echo 服务。
为了验证,我们启动了如下几个选项:
设定全局超时为 5秒,让 GC 的超时时间为 1 毫秒,GC 一般会超过 1 毫秒。
---
echo:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
commonService:
enabled: true # Optional, Enable common service for testing
interceptors:
timeout:
enabled: true # Optional, default: false
timeoutMs: 5000 # Optional, default: 5000
paths:
- path: "/rk/v1/gc" # Optional, default: ""
timeoutMs: 1 # Optional, default: 5000
// 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-echo/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())
}
$ go run main.go
发送 GC 请求。
$ curl -X GET localhost:8080/rk/v1/gc
{
"error":{
"code":408,
"status":"Request Timeout",
"message":"Request timed out!",
"details":[]
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。