Skip to content

Commit

Permalink
feature 支持优雅退出
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoFlight committed Aug 15, 2024
1 parent 0b8557a commit 7e87ad2
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.4.0

新特性(Features)

* 支持优雅退出

# v0.3.0

新特性(Features)
Expand Down
18 changes: 17 additions & 1 deletion api/httpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package api
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/tylerb/graceful"
"net/http"
v1 "openfly/api/v1"
"openfly/conf"
"openfly/logger"
)

var Done = make(chan bool)

func StartHttpServer() {
router := gin.New()
router.Use(gin.Recovery())
Expand All @@ -31,8 +35,20 @@ func StartHttpServer() {

// 启动http服务
logger.GLogger.Info("开始启动http服务")
err := router.Run(fmt.Sprintf(":%d", conf.GConf.Http.Port))
server := graceful.Server{
Server: &http.Server{
Addr: fmt.Sprintf(":%d", conf.GConf.Http.Port),
Handler: router,
},
BeforeShutdown: func() bool {
logger.GLogger.Info("即将关闭http服务")
return true
},
}
err := server.ListenAndServe()
if err != nil {
logger.GLogger.Fatal(err)
}
logger.GLogger.Info("http服务已退出")
Done <- true
}
2 changes: 1 addition & 1 deletion conf/const.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package conf

var (
Version = "v0.3.0"
Version = "v0.4.0"
NgFileExtension = ".conf"
EtcdSubPathL4 = "/l4"
)
Expand Down
98 changes: 98 additions & 0 deletions docs/v0.4.0/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# 健康检查

```shell
curl http://127.0.0.1:1216/v1/health
```

# 鉴权

```shell
# 获取Token
token=$(curl -s -XPOST http://127.0.0.1:1216/v1/login -d "{\"username\":\"admin\",\"password\":\"admin\"}" -H "Content-Type: application/json" | jq -r .data)
echo ${token}
# 访问需要鉴权的接口
curl -H "Authorization: ${token}" http://127.0.0.1:1216/xxx
```

# Nginx相关API

```shell
# 删除配置
curl -XDELETE -H "Authorization: ${token}" http://127.0.0.1:1216/v1/admin/nginx/delete?listen=30001
# 禁用指定端口的配置(启用为on,禁用为off)
curl -XPOST -H "Authorization: ${token}" http://127.0.0.1:1216/v1/admin/nginx/switch -d "listen=30001&switch=off"
# 获取指定端口的配置
curl -s -H "Authorization: ${token}" http://127.0.0.1:1216/v1/admin/nginx/get?listen=30001
# 获取所有配置
curl -H "Authorization: ${token}" http://127.0.0.1:1216/v1/admin/nginx/getAll
# 新增L4配置
curl -i -H "Content-Type: application/json" -XPOST -H "Authorization: ${token}" http://127.0.0.1:1216/v1/admin/nginx/add -d '
{
"listen":30001,
"upstream":{
"hosts":[
{
"ip":"1.1.1.1",
"port":53
}
]
}
}'
# 更新L4配置
curl -i -H "Content-Type: application/json" -XPOST -H "Authorization: ${token}" http://127.0.0.1:1216/v1/admin/nginx/set -d '
{
"listen":30001,
"upstream":{
"hosts":[
{
"ip":"1.1.1.1",
"port":53
}
]
}
}'
```

# 所有支持的参数

```shell
curl -i -H "Content-Type: application/json" -XPOST -H "Authorization: ${token}" http://127.0.0.1:1216/v1/admin/nginx/set -d '
{
"disable": false,
"listen": 30001,
"comments": ["我是注释","I am the comment."],
"includeFiles": ["/etc/nginx/my.conf"],
"proxyUploadRate": "10M",
"proxyDownloadRate": "20M",
"proxyConnectTimeout": "10s",
"proxyTimeout": "10m",
"upstream":{
"hosts":[
{
"ip": "1.1.1.1",
"port": 53,
"isBackup": false,
"weight": 100,
"maxFails": 10,
"failTimeoutSecond":2
}
],
"isHash": true,
"hashField": "test_field",
"interval": 10,
"rise": 2,
"fall": 2,
"timeout": 1000
},
"whiteList": [
{
"type": "allow",
"target": "1.1.1.1"
},
{
"type": "deny",
"target": "all"
}
]
}'
```
6 changes: 6 additions & 0 deletions docs/v0.4.0/dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 一些测试case

```shell
# 写入有问题的配置
etcdctl put /openfly/l4/30001 '{"listen":30001,"upstream":{"hosts":[{"ip":"","port":53}]}}'
```
18 changes: 18 additions & 0 deletions docs/v0.4.0/faq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
如何优雅退出

* 发送term(15号)给openfly进程,可以在INFO日志中查看到退出过程。示例:```kill <pid>``

若nginx因配置错误无法启动,如何解决?

1. 保证nginx本身配置无错误。
2. 通过openfly接口 修改/删除 错误配置。
3. 通过etcd 修改/删除 错误配置。

添加重复接口会报错吗?

*

若配置有问题,会导致线上问题吗?

* 不会。
* nginx -t失败后配置不生效。
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.9.1
github.com/sirupsen/logrus v1.9.3
github.com/tylerb/graceful v1.2.15
go.etcd.io/etcd/api/v3 v3.5.13
go.etcd.io/etcd/client/v3 v3.5.13
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/tylerb/graceful v1.2.15 h1:B0x01Y8fsJpogzZTkDg6BDi6eMf03s01lEKGdrv83oA=
github.com/tylerb/graceful v1.2.15/go.mod h1:LPYTbOYmUTdabwRt0TGhLllQ0MUNbs0Y5q1WXJOI9II=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"openfly/conf"
"openfly/flag"
"openfly/logger"
"os"
"os/signal"
"syscall"
)

func main() {
Expand Down Expand Up @@ -35,6 +38,23 @@ func main() {
// 启动http服务
go api.StartHttpServer()

// 阻塞主进程
select {}
// 优雅退出
sig := make(chan os.Signal)
done := make(chan bool)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
for {
s := <-sig
switch s {
case syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
logger.GLogger.Info("app收到退出信号:", s)
<-api.Done
logger.GLogger.Info("app正常退出")
done <- true
default:
fmt.Println("app收到即将忽略的信号:", s)
}
}
}()
<-done
}

0 comments on commit 7e87ad2

Please sign in to comment.