Skip to content

Commit

Permalink
修改 配置文件热更新功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Menah3m committed Mar 29, 2022
1 parent 8e64e3c commit 095684b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 72 deletions.
2 changes: 1 addition & 1 deletion global/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
ServerSetting *setting.ServiceSettings
AppSetting *setting.AppSettings
FileSetting *setting.FileSettings
DatabaseSetting *setting.DatabaseSettings
QywechatSetting *setting.QywechatSettings
Expand Down
10 changes: 5 additions & 5 deletions internal/alert/qywechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ func getAccessToken() (interface{}, error) {
}

//getQywechatAlertInfo 企业微信通知消息具体内容
func getQywechatAlertInfo(l *model.LogInfo, c int32) *QywechatAlertInfo {
func getQywechatAlertInfo(l *model.LogInfo, c int) *QywechatAlertInfo {
return &QywechatAlertInfo{
Title: fmt.Sprintf("%s 环境 %s 的日志报警", l.Env, l.ServiceName),
Content: fmt.Sprintf("Time: %s \n Level:%s \n ERROR INFO:\n %s\n 最近5分钟内出现了%v次", l.StartAt, l.LogLevel, l.LogInfo, c),
Content: fmt.Sprintf("Time: %s \nLevel:%s \nINFO:\n %s\n最近%.0f分钟内出现了%v次", l.StartAt, l.LogLevel, l.LogInfo, global.AppSetting.Duration.Minutes(), c),
}
}

Expand All @@ -98,8 +98,8 @@ func getPostBody(info *QywechatInfo, alertinfo *QywechatAlertInfo) *map[string]i
"textcard": map[string]interface{}{
"title": alertinfo.Title,
"description": alertinfo.Content,
"url": "www.baidu.com",
"btntext": "跳转到百度",
"url": "http://192.168.108.206:32567/dashboard",
"btntext": "跳转到Kuboard",
},
}
case "markdown":
Expand All @@ -115,7 +115,7 @@ func getPostBody(info *QywechatInfo, alertinfo *QywechatAlertInfo) *map[string]i
return nil
}

func QywechatAlert(l *model.LogInfo, c int32) (map[string]interface{}, error) {
func QywechatAlert(l *model.LogInfo, c int) (map[string]interface{}, error) {

//绑定企业微信参数设置
qywechatInfo := bindQywechatInfo(global.QywechatSetting)
Expand Down
13 changes: 6 additions & 7 deletions internal/parse/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package parse
*/

import (
"fmt"
"github.com/hpcloud/tail"
"golo3/global"
"golo3/internal/alert"
Expand Down Expand Up @@ -43,7 +42,7 @@ func getLogfilePath() string {
//ReadLine 按行读取日志文件内容
func ReadLine() error {
fp := getLogfilePath()
fmt.Println(fp)

t, err := tail.TailFile(fp, tail.Config{
Follow: true,
})
Expand All @@ -54,6 +53,7 @@ func ReadLine() error {

// 监听
for line := range t.Lines {

logInfo := &model.LogInfo{}
now := time.Now()
formatNow := now.Format("2006-01-02 15:04:05")
Expand All @@ -75,15 +75,14 @@ func ReadLine() error {
return err
}

var c int32
var duration time.Duration = 5
d := duration * time.Minute
start := time.Now().Add(-d).Format("2006-01-02 15:04:05")
var c int
start := time.Now().Add(-global.AppSetting.Duration).Format("2006-01-02 15:04:05")
end := time.Now().Format("2006-01-02 15:04:05")

//统计次数
c = process.Count(global.DBEngine, logInfo.LogKeyword, start, end)

if c > 170 {
if c > global.AppSetting.Threshold {

// 发送通知
_, err = alert.QywechatAlert(logInfo, c)
Expand Down
4 changes: 2 additions & 2 deletions internal/process/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
负责 处理日志信息
-- 记数
*/
func Count(db *gorm.DB, keyword string, start, end string) int32 {
var count int32
func Count(db *gorm.DB, keyword string, start, end string) int {
var count int
db.Model(&model.LogInfo{}).Where("log_keyword = ? AND start_at BETWEEN ? AND ? ", keyword, start, end).Count(&count)
return count
}
50 changes: 3 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package main

import (
"golo3/global"
"golo3/configs"
"golo3/internal/parse"
"golo3/model"
"golo3/pkg/setting"
"log"
"time"
)

// 初始化工作,包括 读取设置, 连接数据库..等等
func init() {
err := setupSetting()
err := configs.SetupSetting()
if err != nil {
log.Fatalf("init.setupSetting err:%v", err)
}

err = setupDBEngine()
err = configs.SetupDBEngine()
if err != nil {
log.Fatalf("init.setupDBEngine err :%v", err)
}
Expand All @@ -32,44 +29,3 @@ func main() {
}

}

func setupSetting() error {
newSetting, err := setting.NewSetting()
if err != nil {
return err
}
err = newSetting.ReadSection("Server", &global.ServerSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("File", &global.FileSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("Database", &global.DatabaseSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("Qywechat", &global.QywechatSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("Email", &global.EmailSetting)
if err != nil {
return err
}

global.ServerSetting.ReadTimeout *= time.Second
global.ServerSetting.WriteTimeout *= time.Second

return nil
}

func setupDBEngine() error {
var err error
global.DBEngine, err = model.NewDBEngine(global.DatabaseSetting)
if err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewDBEngine(databaseSetting *setting.DatabaseSettings) (*gorm.DB, error) {
if err != nil {
return nil, err
}
if global.ServerSetting.RunMode == "debug" {
if global.AppSetting.RunMode == "debug" {
db.LogMode(true)
}
db.SingularTable(true)
Expand Down
11 changes: 5 additions & 6 deletions pkg/setting/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ package setting

import "time"

type ServiceSettings struct {
RunMode string
HttpPort string
ReadTimeout time.Duration
WriteTimeout time.Duration
type AppSettings struct {
RunMode string
Duration time.Duration
Threshold int
}

type FileSettings struct {
Expand Down Expand Up @@ -48,7 +47,7 @@ type EmailSettings struct {
}

func (s *Setting) ReadSection(k string, v interface{}) error {
err := s.vp.UnmarshalKey(k, v)
err := s.Vp.UnmarshalKey(k, v)
if err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package setting

// 负责设置读取相关

import "github.com/spf13/viper"
import (
"github.com/spf13/viper"
)

type Setting struct {
vp *viper.Viper
Vp *viper.Viper
}

func NewSetting() (*Setting, error) {
vp := viper.New()
vp.SetConfigName("config")
vp.SetConfigName("golo-config")
//如果不配置path,默认会从程序执行的当前目录寻找配置文件
vp.AddConfigPath("configs/")
vp.AddConfigPath("./")
vp.SetConfigType("yaml")
//监听配置文件
err := vp.ReadInConfig()
if err != nil {
return nil, err
}

return &Setting{vp}, nil
}

0 comments on commit 095684b

Please sign in to comment.