Kaynağa Gözat

feat:xiugai

wangchuanjin 3 ay önce
ebeveyn
işleme
19e266c4e6
9 değiştirilmiş dosya ile 86 ekleme ve 98 silme
  1. 4 54
      config/config.go
  2. 9 1
      go.mod
  3. 1 0
      go.sum
  4. 25 14
      main.go
  5. 14 15
      oss/oss.go
  6. 1 1
      oss/rpc.go
  7. 22 2
      util/email.go
  8. 7 7
      util/heartbeat.go
  9. 3 4
      util/weixin.go

+ 4 - 54
config/config.go

@@ -1,11 +1,10 @@
 package config
 
 import (
-	. "app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/es"
 	"app.yhyue.com/moapp/jybase/go-xweb/log"
-	"app.yhyue.com/moapp/jybase/mail"
-	"gopkg.in/natefinch/lumberjack.v2"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gctx"
 )
 
 // OSSAccount 表示OSS帐号信息
@@ -32,59 +31,10 @@ type PushMail struct {
 	MailReTry    int    `json:"mailReTry"`
 }
 
-// Config 为全局配置结构体
-type Config struct {
-	Port        string       `json:"port"`
-	OSSAccounts []OSSAccount `json:"oss_accounts"`
-	Buckets     []BucketInfo `json:"buckets"`
-	Node        struct {
-		NodeName string `json:"node_name"`
-	} `json:"node"`
-	Redis struct {
-		Address  string `json:"address"`
-		Password string `json:"password"`
-	} `json:"redis"`
-	ElasticSearch struct {
-		Address   string `json:"address"`
-		Size      int    `json:"size"`
-		Version   string `json:"version"`
-		UserName  string `json:"userName"`
-		Password  string `json:"password"`
-		IndexName string `json:"indexName"`
-	} `json:"elasticSearch"`
-	Email struct {
-		Mails      []*PushMail `json:"mails"`
-		Title      string      `json:"title"`
-		Recipients []string    `json:"recipients"`
-	} `json:"email"`
-	Weixin struct {
-		WebhookURL string `json:"webhook_url"`
-	} `json:"weixin"`
-	WarnMaxNodeNum int                `json:"warnMaxNodeNum"`
-	WarnInterval   int64              `json:"warnInterval"`
-	Logger         *lumberjack.Logger `json:"logger"`
-}
-
-var (
-	AppConfig Config
-	Gmails    []*mail.GmailAuth
-)
-
 // LoadConfig 从指定的配置文件中加载配置
 func init() {
-	ReadConfig(&AppConfig)
-	Gmails = make([]*mail.GmailAuth, len(AppConfig.Email.Mails))
-	for k, v := range AppConfig.Email.Mails {
-		Gmails[k] = &mail.GmailAuth{
-			SmtpHost: v.Addr,
-			SmtpPort: v.Port,
-			User:     v.User,
-			Pwd:      v.Pwd,
-			PoolSize: v.MailPoolSize,
-			ReTry:    v.MailReTry,
-		}
-	}
 	log.Println("开始初始化elasticSearch。。。")
-	es.NewEs(AppConfig.ElasticSearch.Version, AppConfig.ElasticSearch.Address, AppConfig.ElasticSearch.Size, AppConfig.ElasticSearch.UserName, AppConfig.ElasticSearch.Password)
+	ctx := gctx.New()
+	es.NewEs(g.Config().MustGet(ctx, "elasticSearch.version").String(), g.Config().MustGet(ctx, "elasticSearch.address").String(), g.Config().MustGet(ctx, "elasticSearch.size").Int(), g.Config().MustGet(ctx, "elasticSearch.userName").String(), g.Config().MustGet(ctx, "elasticSearch.password").String())
 	log.Println("初始化elasticSearch结束。。。")
 }

+ 9 - 1
go.mod

@@ -9,6 +9,7 @@ require (
 	bp.jydev.jianyu360.cn/BaseService/pushpkg v0.0.0-20250304072104-4817d2821c84
 	github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
 	github.com/go-redis/redis/v8 v8.11.5
+	github.com/gogf/gf/v2 v2.7.0
 	gopkg.in/natefinch/lumberjack.v2 v2.2.1
 	jygit.jydev.jianyu360.cn/BaseService/ossClient v0.0.0-20250415084929-bc2e49395095
 )
@@ -17,9 +18,11 @@ require (
 	app.yhyue.com/moapp/esv1 v0.0.0-20220414031211-3da4123e648d // indirect
 	bp.jydev.jianyu360.cn/BaseService/resourceCenter v0.1.0 // indirect
 	bp.jydev.jianyu360.cn/BaseService/userCenter v1.2.17 // indirect
+	github.com/BurntSushi/toml v1.2.0 // indirect
 	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/cenkalti/backoff/v4 v4.2.0 // indirect
 	github.com/cespare/xxhash/v2 v2.2.0 // indirect
+	github.com/clbanning/mxj/v2 v2.7.0 // indirect
 	github.com/coreos/go-semver v0.3.1 // indirect
 	github.com/coreos/go-systemd/v22 v22.5.0 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
@@ -35,7 +38,6 @@ require (
 	github.com/go-openapi/jsonreference v0.20.1 // indirect
 	github.com/go-openapi/swag v0.22.3 // indirect
 	github.com/go-sql-driver/mysql v1.7.0 // indirect
-	github.com/gogf/gf/v2 v2.7.0 // indirect
 	github.com/gogo/protobuf v1.3.2 // indirect
 	github.com/golang/mock v1.6.0 // indirect
 	github.com/golang/protobuf v1.5.3 // indirect
@@ -45,20 +47,25 @@ require (
 	github.com/google/go-cmp v0.5.9 // indirect
 	github.com/google/gofuzz v1.2.0 // indirect
 	github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
+	github.com/gorilla/websocket v1.5.0 // indirect
+	github.com/grokify/html-strip-tags-go v0.0.1 // indirect
 	github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 // indirect
 	github.com/jinzhu/inflection v1.0.0 // indirect
 	github.com/jinzhu/now v1.1.1 // indirect
 	github.com/josharian/intern v1.0.0 // indirect
 	github.com/json-iterator/go v1.1.12 // indirect
 	github.com/klauspost/compress v1.15.15 // indirect
+	github.com/magiconair/properties v1.8.6 // indirect
 	github.com/mailru/easyjson v0.7.7 // indirect
 	github.com/mattn/go-colorable v0.1.13 // indirect
 	github.com/mattn/go-isatty v0.0.17 // indirect
+	github.com/mattn/go-runewidth v0.0.13 // indirect
 	github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
 	github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
+	github.com/olekukonko/tablewriter v0.0.5 // indirect
 	github.com/olivere/elastic v6.2.37+incompatible // indirect
 	github.com/olivere/elastic/v7 v7.0.22 // indirect
 	github.com/openzipkin/zipkin-go v0.4.1 // indirect
@@ -68,6 +75,7 @@ require (
 	github.com/prometheus/client_model v0.3.0 // indirect
 	github.com/prometheus/common v0.42.0 // indirect
 	github.com/prometheus/procfs v0.9.0 // indirect
+	github.com/rivo/uniseg v0.2.0 // indirect
 	github.com/spaolacci/murmur3 v1.1.0 // indirect
 	github.com/xdg-go/pbkdf2 v1.0.0 // indirect
 	github.com/xdg-go/scram v1.1.1 // indirect

+ 1 - 0
go.sum

@@ -260,6 +260,7 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
 github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
+github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
 github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
 github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
 github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=

+ 25 - 14
main.go

@@ -1,25 +1,34 @@
 package main
 
 import (
+	"app.yhyue.com/moapp/jybase/endless"
 	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gctx"
+	"github.com/gogf/gf/v2/os/gfsnotify"
+	"gopkg.in/natefinch/lumberjack.v2"
+	"jygit.jydev.jianyu360.cn/BaseService/ossClient/constant"
+	ossService "jygit.jydev.jianyu360.cn/BaseService/ossService/oss"
+	"jygit.jydev.jianyu360.cn/BaseService/ossService/util"
 	"log"
 	"net/http"
 	"net/rpc"
 	"time"
-
-	"app.yhyue.com/moapp/jybase/endless"
-	"jygit.jydev.jianyu360.cn/BaseService/ossClient/constant"
-	"jygit.jydev.jianyu360.cn/BaseService/ossService/config"
-	ossService "jygit.jydev.jianyu360.cn/BaseService/ossService/oss"
-	"jygit.jydev.jianyu360.cn/BaseService/ossService/util"
 )
 
 func main() {
-	log.SetOutput(config.AppConfig.Logger)
+	ctx := gctx.New()
+	var logger *lumberjack.Logger
+	g.Config().MustGet(ctx, "logger").Struct(&logger)
+	log.SetOutput(logger)
 	// 初始化OSS帐号与bucket信息
 	ossService.LoadOSSAccounts()
-	ossService.InitBuckets()
-
+	// 注册一个回调函数,当配置发生变更时会被调用
+	gfsnotify.Add("./config.json", func(event *gfsnotify.Event) {
+		if event.IsWrite() || event.IsChmod() {
+			ossService.LoadOSSAccounts()
+		}
+	})
 	// 初始化Redis(用于节点心跳检测)
 	util.InitRedis()
 
@@ -34,10 +43,11 @@ func main() {
 				log.Println("Heartbeat check error", err)
 				continue
 			}
-			if len(nodes) < config.AppConfig.WarnMaxNodeNum {
-				alertMsg := fmt.Sprintf("在线节点数少于%d: 当前在线节点数:%d", config.AppConfig.WarnMaxNodeNum, len(nodes))
+			warnMaxNodeNum := g.Config().MustGet(ctx, "warnMaxNodeNum").Int()
+			if len(nodes) < warnMaxNodeNum {
+				alertMsg := fmt.Sprintf("在线节点数少于%d: 当前在线节点数:%d", warnMaxNodeNum, len(nodes))
 				log.Println(alertMsg)
-				if nowUnix := time.Now().Unix(); nowUnix-prevWarn > config.AppConfig.WarnInterval {
+				if nowUnix := time.Now().Unix(); nowUnix-prevWarn > g.Config().MustGet(ctx, "warnInterval").Int64() {
 					prevWarn = nowUnix
 					util.SendWeixinNotification(alertMsg)
 					util.SendEmailNotification(alertMsg)
@@ -55,8 +65,9 @@ func main() {
 	http.HandleFunc(constant.DeleteUrl, ossService.DeleteHandler)
 	http.HandleFunc("/ossservice/nodes", ossService.NodesHandler)
 	http.HandleFunc(constant.GetBidDetailUrl, ossService.BidDetailHandler)
-	log.Println("HTTP server started on " + config.AppConfig.Port)
-	if err := endless.ListenAndServe(config.AppConfig.Port, nil, func() {}); err != nil {
+	port := g.Config().MustGet(ctx, "port").String()
+	log.Println("HTTP server started on " + port)
+	if err := endless.ListenAndServe(port, nil, func() {}); err != nil {
 		log.Fatalln("HTTP server error", err)
 	}
 }

+ 14 - 15
oss/oss.go

@@ -4,6 +4,8 @@ import (
 	"bytes"
 	"compress/gzip"
 	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gctx"
 	"io"
 	"io/ioutil"
 	"log"
@@ -14,23 +16,17 @@ import (
 	"jygit.jydev.jianyu360.cn/BaseService/ossService/config"
 )
 
-// bucketMap 用来缓存bucket配置,避免重复查询数据库
-var bucketMap sync.Map
-
 // accountMap 用来缓存OSS帐号信息
 var accountMap sync.Map
 
-// InitBuckets 从配置中加载bucket信息
-func InitBuckets() {
-	for _, bucket := range config.AppConfig.Buckets {
-		bucketMap.Store(bucket.BucketID, bucket)
-	}
-}
-
 // GetBucket 根据bucketID获取bucket信息,如果没有则模拟查询数据库(这里只查询一次)
 func GetBucket(bucketID string) (config.BucketInfo, error) {
-	if val, ok := bucketMap.Load(bucketID); ok {
-		return val.(config.BucketInfo), nil
+	var buckets []*config.BucketInfo
+	g.Config().MustGet(gctx.New(), "buckets").Structs(&buckets)
+	for _, v := range buckets {
+		if v.BucketID == bucketID {
+			return *v, nil
+		}
 	}
 	log.Println("Bucket %s not found in cache, querying database", bucketID)
 	// 模拟数据库查询:实际业务中这里应查询数据库
@@ -148,8 +144,10 @@ func DeleteAttachment(bucketID, objectName string) error {
 
 // LoadOSSAccounts 加载OSS帐号信息到缓存
 func LoadOSSAccounts() {
-	for _, acc := range config.AppConfig.OSSAccounts {
-		accountMap.Store(acc.ID, acc)
+	var oas []*config.OSSAccount
+	g.Config().MustGet(gctx.New(), "oss_accounts").Structs(&oas)
+	for _, acc := range oas {
+		accountMap.LoadOrStore(acc.ID, *acc)
 	}
 }
 
@@ -159,7 +157,8 @@ func GetBidDetail(bucketID, objectName string) []byte {
 	if e == nil && b != nil && len(b) > 0 {
 		return b
 	}
-	list := es.VarEs.Get(config.AppConfig.ElasticSearch.IndexName, config.AppConfig.ElasticSearch.IndexName, fmt.Sprintf(`{"query":{"bool":{"filter":{"term":{"_id":"%s"}}}},"_source":["detail"]}`, objectName))
+	indexName := g.Config().MustGet(gctx.New(), "elasticSearch.indexName").String()
+	list := es.VarEs.Get(indexName, indexName, fmt.Sprintf(`{"query":{"bool":{"filter":{"term":{"_id":"%s"}}}},"_source":["detail"]}`, objectName))
 	if list != nil && len(*list) > 0 {
 		detail, _ := (*list)[0]["detail"].(string)
 		return []byte(detail)

+ 1 - 1
oss/rpc.go

@@ -38,7 +38,7 @@ func (s *OSSService) Upload(args *entity.UploadArgs, reply *api.Result) error {
 	log.Println("rpc方式上传文件", args.BucketID, args.ObjectName, args.Gzip, len(args.Stream))
 	err := UploadAttachment(args.BucketID, args.ObjectName, ioutil.NopCloser(bytes.NewReader(args.Stream)), args.Gzip)
 	if err != nil {
-		log.Println(constant.UploadFail, err)
+		log.Printf(constant.UploadFail, err)
 		return err
 	}
 	reply.Error_msg = constant.UploadSuccess

+ 22 - 2
util/email.go

@@ -1,13 +1,33 @@
 package util
 
 import (
+	"app.yhyue.com/moapp/jybase/mail"
 	. "bp.jydev.jianyu360.cn/BaseService/pushpkg/pusher"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gctx"
 	"jygit.jydev.jianyu360.cn/BaseService/ossService/config"
 )
 
 // SendEmailNotification 发送邮件通知给多个收件人
 func SendEmailNotification(body string) {
-	for _, v := range config.AppConfig.Email.Recipients {
-		VarPush.SendMail(true, 0, config.Gmails, v, config.AppConfig.Email.Title, body)
+	var email struct {
+		Mails      []*config.PushMail
+		Title      string
+		Recipients []string
+	}
+	g.Config().MustGet(gctx.New(), "email.mails").Struct(&email)
+	gmails := make([]*mail.GmailAuth, len(email.Mails))
+	for k, v := range email.Mails {
+		gmails[k] = &mail.GmailAuth{
+			SmtpHost: v.Addr,
+			SmtpPort: v.Port,
+			User:     v.User,
+			Pwd:      v.Pwd,
+			PoolSize: v.MailPoolSize,
+			ReTry:    v.MailReTry,
+		}
+	}
+	for _, v := range email.Recipients {
+		VarPush.SendMail(true, 0, gmails, v, email.Title, body)
 	}
 }

+ 7 - 7
util/heartbeat.go

@@ -3,12 +3,11 @@ package util
 import (
 	"context"
 	"encoding/json"
+	"github.com/go-redis/redis/v8"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gctx"
 	"log"
 	"time"
-
-	"jygit.jydev.jianyu360.cn/BaseService/ossService/config"
-
-	"github.com/go-redis/redis/v8"
 )
 
 var ctx = context.Background()
@@ -16,9 +15,10 @@ var rdb *redis.Client
 
 // InitRedis 初始化redis客户端
 func InitRedis() {
+	ctx := gctx.New()
 	rdb = redis.NewClient(&redis.Options{
-		Addr:     config.AppConfig.Redis.Address,
-		Password: config.AppConfig.Redis.Password,
+		Addr:     g.Config().MustGet(ctx, "redis.address").String(),
+		Password: g.Config().MustGet(ctx, "redis.password").String(),
 		DB:       0,
 	})
 }
@@ -31,7 +31,7 @@ type NodeInfo struct {
 
 // SendHeartbeat 将节点心跳信息写入redis,过期时间为10秒
 func SendHeartbeat() {
-	nodeName := config.AppConfig.Node.NodeName
+	nodeName := g.Config().MustGet(ctx, "node.node_name").String()
 	node := NodeInfo{
 		NodeName:  nodeName,
 		Timestamp: time.Now().Unix(),

+ 3 - 4
util/weixin.go

@@ -3,10 +3,10 @@ package util
 import (
 	"bytes"
 	"encoding/json"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gctx"
 	"log"
 	"net/http"
-
-	"jygit.jydev.jianyu360.cn/BaseService/ossService/config"
 )
 
 type WeixinMessage struct {
@@ -18,14 +18,13 @@ type WeixinMessage struct {
 
 // SendWeixinNotification 发送企业微信机器人告警
 func SendWeixinNotification(message string) error {
-	webhookURL := config.AppConfig.Weixin.WebhookURL
 	msg := WeixinMessage{MsgType: "text"}
 	msg.Text.Content = message
 	data, err := json.Marshal(msg)
 	if err != nil {
 		return err
 	}
-	resp, err := http.Post(webhookURL, "application/json", bytes.NewReader(data))
+	resp, err := http.Post(g.Config().MustGet(gctx.New(), "weixin.webhook_url").String(), "application/json", bytes.NewReader(data))
 	if err != nil {
 		log.Println("SendWeixinNotification error", err)
 		return err