当前位置:网站首页>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("正常退出")
}
}
具体使用那种,要看自己的理解了。有些人认为我退出导致一些做到一半的接口响应失败并不会影响业务,那么可以选择第一种。如果是认为我一定要做完才能退出,可以使用第二种。
边栏推荐
- HJ37 统计每个月兔子的总数 斐波那契数列
- Pod基本介绍
- 数据库系统概述
- VMware16创建虚拟机:无法创建新虚拟机,不具备执行此操作的权限
- Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
- ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
- Security in quantum machine learning
- spark学习笔记(七)——sparkcore核心编程-RDD序列化/依赖关系/持久化/分区器/累加器/广播变量
- 数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
- 建木持续集成平台v2.5.2发布
猜你喜欢

建木持续集成平台v2.5.2发布

Is online legend software testing training really so black hearted? Are they all scams?

Cvpr2022oral special series (I): low light enhancement

Some tips of vim text editor

Share some tips for better code, smooth coding and improve efficiency

Teacher wangshuyao's notes on operations research course 10 linear programming and simplex method (discussion on detection number and degradation)

Flink real time warehouse DWD layer (traffic domain) template code

Flink real-time warehouse DWD layer (transaction domain - additional purchase dimension degradation processing) template code

LeetCode 879. 盈利计划

基于C语言实现图书借阅管理系统
随机推荐
SSH password free login - two virtual machines establish password free channel two-way trust
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
Cvpr2022oral special series (I): low light enhancement
模拟卷Leetcode【普通】172. 阶乘后的零
Connecting PHP 7.4 to Oracle configuration on Windows
Flink实时仓库-DWD层(交易域-加购维度退化处理)模板代码
Simulation volume leetcode [normal] 061. rotating linked list
IDEA找不到Database解决方法
Overview of database system
好文佳句摘录
Salesforce中过滤器Filter使用的相对日期
Teacher Wu Enda machine learning course notes 01 introduction
Not so simple singleton mode
Teacher Wu Enda's machine learning course notes 00 are written in the front
Excerpts from good essays
Implementation of DDP cluster distributed training under pytoch multi GPU conditions (brief introduction - from scratch)
竣达技术 | 适用于”日月元”品牌UPS微信云监控卡
Analog volume leetcode [normal] 093. Restore IP address
Improved Pillar with Fine-grained Feature for 3D Object Detection论文笔记
HJ37 统计每个月兔子的总数 斐波那契数列