当前位置:网站首页>gin 服务退出
gin 服务退出
2022-07-29 06:20:00 【Mar丶流年】
退出一般使用chan + 信号(ctrl+c,kill)进行退出,这样后续退出能做一些收尾工作
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
)
var basePath string
func init() {
_, file, _, _ := runtime.Caller(0)
basePath = filepath.Dir(file)
}
func getPath(path string) string {
return filepath.Join(basePath, path)
}
func main() {
r := gin.Default()
r.LoadHTMLGlob(getPath("template/**/*"))
r.Static("/static", getPath("static"))
r.GET("/user", func(c *gin.Context) {
//a/user.tmpl
c.HTML(http.StatusOK, "a/user", gin.H{
"title": "用户列表",
})
})
go func() {
r.Run(":8080")
}()
q := make(chan os.Signal)
//接收ctrl + c ,kill(排除 kill -9)
signal.Notify(q, syscall.SIGINT, syscall.SIGTERM)
<-q
//后续操作处理,比如主动从服务中心中移除当前节点
fmt.Println("zookeeper remove current node")
}
网上还有些使用 Shutdown 退出。但是启动的时候没有使用gin的run相关方法启动。自己写的遗漏了对http2的协议升级
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
)
var basePath string
func init() {
_, file, _, _ := runtime.Caller(0)
basePath = filepath.Dir(file)
}
func getPath(path string) string {
return filepath.Join(basePath, path)
}
func main() {
r := gin.Default()
r.LoadHTMLGlob(getPath("template/**/*"))
r.Static("/static", getPath("static"))
r.GET("/user", func(c *gin.Context) {
//a/user.tmpl
c.HTML(http.StatusOK, "a/user", gin.H{
"title": "用户列表",
})
})
//run 是根据engine.UseH2C 来决定是否使用http2协议
//如果要使用这种启动方法,请根据engine.UseH2C完善代码
server := http.Server{
Addr: ":8080",
Handler: r,
}
go func() {
server.ListenAndServe()
}()
q := make(chan os.Signal)
//接收ctrl + c ,kill(排除 kill -9)
signal.Notify(q, syscall.SIGINT, syscall.SIGTERM)
<-q
//后续操作处理,比如主动从服务中心中移除当前节点
fmt.Println("zookeeper remove current node")
if err := server.Shutdown(context.Background()); err != nil {
panic(err)
}else{
fmt.Println("正常退出")
}
}
具体使用那种,要看自己的理解了。有些人认为我退出导致一些做到一半的接口响应失败并不会影响业务,那么可以选择第一种。如果是认为我一定要做完才能退出,可以使用第二种。
边栏推荐
- Simulation volume leetcode [general] 150. evaluation of inverse Polish expression
- MySQL:当你CRUD时BufferPool中发生了什么?十张图就能说清楚
- win11系统错误:由于找不到 iertutil.dll,无法继续执行代码。重新安装程序可能会解决此问题
- VMware16创建虚拟机:无法创建新虚拟机,不具备执行此操作的权限
- ECCV 2022 lightweight model frame Parc net press apple mobilevit code and paper Download
- VMware16安装虚拟机遇到的问题
- Invalid access control
- Image noise and matrix inversion
- Teacher Cui Xueting's course notes on optimization theory and methods 00 are written in the front
- leetcode-592:分数加减运算
猜你喜欢
IO stream - file - properties
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
实战!聊聊如何解决MySQL深分页问题
解决CSDN因版权不明而无法发布博客的问题
我的创业邻居们
Improved Pillar with Fine-grained Feature for 3D Object Detection论文笔记
城市花样精~侬好!DESIGN#可视化电台即将开播
Unity free element special effect recommendation
Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
Windows 上 php 7.4 连接 oracle 配置
随机推荐
Teacher Wu Enda machine learning course notes 05 octave tutorial
C language memory stack and heap usage
mysql查询区分大小写
Federal learning backdoor attack summary (2019-2022)
Summary of 2022 SQL classic interview questions (with analysis)
SS command details
Teacher Wu Enda's machine learning course notes 03 review of linear algebra
上采样之反卷积操作
gin 模版
Teacher Wu Enda's machine learning course notes 04 multiple linear regression
Difference between CNAME record and a record
记 - 踩坑-实时数仓开发 - doris/pg/flink
模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
Flink real-time warehouse DWD layer (processing complex data - installation and replacement of streams and tables) template code
Simulation volume leetcode [ordinary] 172. Zero after factorial
Windows 上 php 7.4 连接 oracle 配置
Teacher wangshuyao's notes on operations research 03 KKT theorem
Simulation volume leetcode [normal] 061. rotating linked list
Invalid access control
Pytorch多GPU条件下DDP集群分布训练实现(简述-从无到有)