3

关于 go gin 程序如果热更新的问题

 1 year ago
source link: https://www.v2ex.com/t/880300
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Go 编程语言

关于 go gin 程序如果热更新的问题

  Nasser · 4 小时 6 分钟前 · 754 次点击

大家好,请教一下 我准备使用 gin 来做后端 api 接口 但是在部署和热更新方案上比较困惑 应该怎样来实现 gin 程序的热更新呢 (还望大家不吝赐教)

16 条回复    2022-09-15 19:40:17 +08:00

hdczsf      3 小时 54 分钟前

不是有负载均衡吗,逐个重启更新节点,服务不会中断的.

Nasser      3 小时 50 分钟前

@hdczsf gin 直接打包到服务器运行的,怎么重启不会导致服务重点呢?

moliliang      3 小时 48 分钟前

```golang
func runServer(addr string, engine *gin.Engine) error {
server := &http.Server{
Addr: addr,
Handler: engine,
WriteTimeout: 60 * time.Second,
ReadTimeout: 60 * time.Second,
}

// 平滑关闭
// 下面的 notify 是基于 signal.Notify 封装的,当监听到 kill ,会将 server shutdown
notify.Subscribe(func() {
timeoutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
server.Shutdown(timeoutCtx)
})

return server.ListenAndServe()
}
```

上面的代码会监听 kill notify ,然后触发 shutdown ,shutdown 里面会停掉 listeners ,然后处理完所有已经连接的 socket 。

理论上可以在启动新进程的时候,来给旧进程发送 kill ,旧进程挂掉,新进程启动监听端口就好啦

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK