最近公司上马go的项目,每次更新代码都要停机琢磨了一下做了一个减配方案,后期再上新方案
1、复制项目main.go

main.go文件监听8080
run.go 文件监听8081
分别执行
go build -o run1 main.go
go build -o run2 run.go
将打包好的run1和run2覆盖到linux的项目目录下
nohup后台启动项目
nohup ./run1 > sys1.out 2>&1 &
nohup ./run2 > sys2.out 2>&1 &
nginx代理轮询服务
upstream go_http {
server 10.101.21.10:8080;
server 10.101.21.11:8081;
}server {
listen 80;
server_name www.test.com;
root xxxxx/xxx/xxx/xx;
location /api/ {
proxy_pass http://go_http/;
}
}
后续更新代码可以逐个覆盖重启项目做到平滑更新
当然这里必不可少的有一个优雅关闭,具体的自行百度吧每个框架的做法不一样。这里贴出iris的处理方式
serverWG := new(sync.WaitGroup)
defer serverWG.Wait()
// Iris ========================================
iris.RegisterOnInterrupt(func() {
serverWG.Add(1)
defer serverWG.Done()
timeout := 5 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// 关闭所有主机
app.Shutdown(ctx)
})
本文介绍了如何使用Go语言实现项目部署,通过多实例和负载均衡,结合优雅关闭机制,确保代码更新时服务无缝切换。还探讨了如何利用Nginx代理和nohup进行后台管理和部署,以及Iris框架的中断处理。
1976

被折叠的 条评论
为什么被折叠?



