Gin 框架:实现超时中间件
2023-03-14 22:59:04 时间
介绍
通过一个完整例子,在基于 Gin 框架的微服务中实现【超时】中间件。
我们将会使用 rk-boot 来启动 Gin 框架的微服务。
请访问如下地址获取完整教程:
安装
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-gin
快速开始
支持全局超时和 API 超时设定。
1.创建 boot.yaml
boot.yaml 文件告诉 rk-boot 如何启动 Gin 服务。
为了验证,我们启动了如下几个选项:
- commonService:commonService 里包含了一系列通用 API。详情
设定全局超时为 5秒,让 GC 的超时时间为 1 毫秒,GC 一般会超过 1 毫秒。
---
gin:
- 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
2.创建 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.启动 main.go
$ go run main.go
4.验证
发送 GC 请求。
$ curl -X GET localhost:8080/rk/v1/gc
{
"error":{
"code":408,
"status":"Request Timeout",
"message":"Request timed out!",
"details":[]
}
}
相关文章
- AWS 发布针对 Amazon EC2 的 Apple macOS 实例
- re:Invent 2020 大会直播博客:Andy Jassy 主题演讲
- 企业微信、钉钉接收 Amazon CloudWatch 告警
- 使用 Amazon EKS 轻松运行 Flink 作业
- 推出 Amazon Managed Workflows for Apache Airflow (MWAA)
- 新增功能 – 适用于 AWS Lambda 的信任和完整性控件:代码签名
- 新增功能 – AWS Single Sign-On 支持基于属性的访问控制
- 通过 Elasticsearch 构建WAF日志分析系统
- 介绍 Amazon S3 Storage Lens — 组织范围内对象存储的可见性
- 现已为 AWS 托管的 Microsoft 活动目录启用多区域复制
- AWS Network Firewall — VPC 中的新托管防火墙服务
- 在 Amazon SageMaker 中构建一套定制化推荐系统
- 规划具有 Hot-Warm 架构的 Amazon Elasticsearch Service 集群
- SOCA 帮助半导体企业快速启动 EDA 云上部署
- AWS Organizations 中组织单元的最佳实践
- 利用技术手段实现对业务运行环境的持续安全合规检测
- 使用 Amazon Textract 与 Amazon Augmented AI 配合人工循环处理 PDF 文档
- 手把手教你快速将宁盾双因素认证(MFA)与 Amazon WorkSpaces 集成
- 基于 QuickSight 的成本可视化方案
- 手把手教你快速部署 Spot 实例并配置中断邮件警告