Skip to content

Commit 039805a

Browse files
committed
Feat: hot reload conf
1 parent 9176aee commit 039805a

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

README.MD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ curl http://localhost:8080/health
6161

6262
<br/>
6363

64-
=======
6564
-------------------------------------
6665

6766
## 启动
@@ -136,6 +135,7 @@ feiyu563/prometheus-alert:latest
136135
- 增加告警语音播报插件。
137136
- 增加支持飞书机器人应用。
138137
- 增加告警组,可以将通知媒介写到告警组里面,便于配置和修改。
138+
- 增加热加载配置接口
139139

140140
-------------------------------------
141141

@@ -198,6 +198,7 @@ feiyu563/prometheus-alert:latest
198198
* [语音播报](doc/readme/conf-voice.md)
199199
* [飞书机器人应用](doc/readme/conf-feishuapp.md)
200200
* [告警组配置](doc/readme/alertgroup.md)
201+
* [热加载配置](doc/readme/hotreload.md)
201202

202203
* [【告警系统接入PrometheusAlert配置】](doc/readme/system.md)
203204
* [Prometheus 接入配置](doc/readme/system-prometheus.md)

conf/app-example.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ to_es_url=http://localhost:9200
5959
# to_es_pwd=password
6060
# 长连接最大空闲数
6161
maxIdleConns=100
62+
# 热更新配置文件
63+
open-hotreload=0
6264

6365
#---------------------↓webhook-----------------------
6466
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启

controllers/hotreload.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package controllers
2+
3+
import (
4+
"github.com/astaxie/beego"
5+
"github.com/astaxie/beego/logs"
6+
)
7+
8+
type ConfigController struct {
9+
beego.Controller
10+
}
11+
12+
var configPath = "conf/app.conf"
13+
14+
// hot reload config
15+
func (c *ConfigController) Reload() {
16+
logsign := "[" + LogsSign() + "]"
17+
if open := beego.AppConfig.String("open-hotreload"); open != "1" {
18+
logs.Info(logsign, "[hotreload]", "open-hotreload is 0.")
19+
c.Ctx.WriteString("open-hotreload is disable.\n")
20+
return
21+
}
22+
23+
// apply config file
24+
err := beego.LoadAppConfig("ini", configPath)
25+
if err != nil {
26+
logs.Error(logsign, "[hotreload]", err.Error())
27+
c.Ctx.ResponseWriter.WriteHeader(500)
28+
c.Ctx.WriteString("Error reloading config: " + err.Error() + "\n")
29+
return
30+
}
31+
32+
logs.Info(logsign, "[hotreload]", "Config reloaded successfully.")
33+
c.Ctx.WriteString("Config reloaded successfully.\n")
34+
}
35+
36+
// Add auth middleware or not?

doc/readme/hotreload.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 热加载配置接口文档
2+
3+
热加载配置接口,类似于 Prometheus 的热加载配置接口。
4+
5+
配置文件中的配置项为 `open-hotreload`,默认关闭。可以将其设置为 1 来开启这个功能。暂时没有给接口添加认证。
6+
7+
> **请注意:** 如果启动之前没有打开热加载,那么在启动程序之后再开启热加载是不生效的(因为内存中的热加载配置还是关闭的,因此程序还是判断未启用热加载),需要重新启动程序。<br>
8+
> **考虑:** 是否需要给这个接口添加认证?
9+
10+
<br/>
11+
12+
热加载接口原理:使用 `beego.LoadAppConfig("ini", "conf/app.conf")` 来加载配置。响应中会返回成功还是错误的相关信息。
13+
14+
<br/>
15+
<br/>
16+
17+
## 使用方法
18+
19+
使用方法:
20+
21+
1. 配置文件中开启热加载
22+
2. 启动程序
23+
3. 修改配置
24+
4. 热加载配置
25+
26+
```bash
27+
# conf/app.conf
28+
# 开启热加载
29+
open-hotreload=1
30+
31+
# 热加载接口
32+
curl -X POST http://PrometheusAlert:8080/-/reload
33+
```

routers/router.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func init() {
5151
beego.Router("/gitlab/weixin", &controllers.GitlabController{}, "post:GitlabWeixin")
5252
beego.Router("/gitlab/dingding", &controllers.GitlabController{}, "post:GitlabDingding")
5353
beego.Router("/gitlab/feishu", &controllers.GitlabController{}, "post:GitlabFeishu")
54-
// Todo
55-
// Email
56-
// Feishu
54+
55+
// hotreload
56+
beego.Router("/-/reload", &controllers.ConfigController{}, "post:Reload")
5757

5858
//已经下线的接口
5959
//beego.Router("/prometheus/dingding", &controllers.PrometheusController{},"post:PrometheusRouter")

0 commit comments

Comments
 (0)