共计 912 个字符,预计需要花费 3 分钟才能阅读完成。
简介:本文将介绍优雅敞开 gRPC 微服务。在过程收到敞开信号时,咱们须要敞开后盾运行的逻辑,比方,MySQL 连贯等等。
介绍
本文将介绍优雅敞开 gRPC 微服务。
什么是优雅敞开?
在过程收到敞开信号时,咱们须要敞开后盾运行的逻辑,比方,MySQL 连贯等等。
咱们将会应用 rk-boot 来启动 gRPC 服务。
请拜访如下地址获取残缺教程:
- https://rkdev.info/cn
- https://rkdocs.netlify.app/cn (备用)
装置
go get github.com/rookie-ninja/rk-boot
疾速开始
1. 创立 boot.yaml
--- | |
grpc: | |
- name: greeter # Name of grpc entry | |
port: 8080 # Port of grpc entry | |
enabled: true # Enable grpc entry |
2. 创立 main.go
通过 AddShutdownHookFunc() 来增加 shutdownhook 函数。
package main | |
import ( | |
"context" | |
"github.com/rookie-ninja/rk-boot" | |
"github.com/rookie-ninja/rk-gin/interceptor/context" | |
) | |
// Application entrance. | |
func main() { | |
// Create a new boot instance. | |
boot := rkboot.NewBoot() | |
// Add shutdown hook function | |
boot.AddShutdownHookFunc("shutdown-hook", func() {fmt.Println("shutting down") | |
}) | |
// Bootstrap | |
boot.Bootstrap(context.Background()) | |
// Wait for shutdown sig | |
boot.WaitForShutdownSig(context.Background()) | |
} |
3. 启动 main.go
$ go run main.go
4.ctrl-c
通过 ctrl-c 关闭程序,咱们会看到打印如下信息。
shutting down
原文链接
本文为阿里云原创内容,未经容许不得转载。
正文完