当前位置:网站首页>Gin service exit
Gin service exit
2022-07-29 07:16:00 【Mar, LiuNian】
Quit general use chan + The signal (ctrl+c,kill) Proceed to exit , In this way, the subsequent exit can do some finishing work
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": " User list ",
})
})
go func() {
r.Run(":8080")
}()
q := make(chan os.Signal)
// receive ctrl + c ,kill( exclude kill -9)
signal.Notify(q, syscall.SIGINT, syscall.SIGTERM)
<-q
// Subsequent operations , For example, actively remove the current node from the service center
fmt.Println("zookeeper remove current node")
}
There are also some uses on the Internet Shutdown sign out . But it was not used when starting gin Of run Relevant methods start . What I wrote is missing. Right http2 The protocol upgrade
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": " User list ",
})
})
//run It's based on engine.UseH2C To decide whether to use http2 agreement
// If you want to use this startup method , Please according to engine.UseH2C Perfect the code
server := http.Server{
Addr: ":8080",
Handler: r,
}
go func() {
server.ListenAndServe()
}()
q := make(chan os.Signal)
// receive ctrl + c ,kill( exclude kill -9)
signal.Notify(q, syscall.SIGINT, syscall.SIGTERM)
<-q
// Subsequent operations , For example, actively remove the current node from the service center
fmt.Println("zookeeper remove current node")
if err := server.Shutdown(context.Background()); err != nil {
panic(err)
}else{
fmt.Println(" The normal exit ")
}
}
What kind of , It depends on your understanding . Some people think that the failure of interface response caused by my withdrawal will not affect the business , Then you can choose the first . If you think I have to finish before I can quit , You can use the second .
边栏推荐
- Custom events
- Student status management system based on C language design
- 解决CSDN因版权不明而无法发布博客的问题
- 数组的子集能否累加出K
- [C language brush leetcode] 1054. Bar code with equal distance (m)
- ETL为什么经常变成ELT甚至LET?
- Leetcode 879. profit plan
- [solution] error: lib/bridge_ generated. dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
- win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案
- gin 服务退出
猜你喜欢

Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem

Leetcode 879. profit plan

SpingBoot整合Quartz框架实现动态定时任务(支持实时增删改查任务)

Ethernet interface introduction

VMware16创建虚拟机:Win11无法安装

JS 鸡生蛋与蛋生鸡问题,Object与Function究竟谁出现的更早?Function算不算Function的实例?

Vite3.0 has been released, can you still roll it (list of new features)

buck电路boot和ph引脚实测

After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is

Summary of OCR optical character recognition methods
随机推荐
MySQL----多表查询
接口测试实战项目03:执行测试用例
要不要满足客户所有的需求
2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f
Variables and encryption in ansible
WPF简单登录页面的完成案例
Implementation of book borrowing management system based on C language
CVPR2021| 基于自监督学习的多视图立体匹配 (CVPR2021)
VMware16创建虚拟机:Win11无法安装
Remote invocation of microservices
gin 服务退出
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)
Pod基本介绍
Record - step on the pit - real-time data warehouse development - doris/pg/flink
Flink real time warehouse DWD layer (traffic domain) template code
外包干了3年,跳槽后转自动化测试工资是原来的2倍,秘诀原来是......
2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
最新百亿量化私募名单
330. 按要求补齐数组