✨ 完善代码
This commit is contained in:
parent
200820df93
commit
441132c04f
17
config/app_info.go
Normal file
17
config/app_info.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "gateway/utils"
|
||||||
|
|
||||||
|
type appInfo struct {
|
||||||
|
AppName string
|
||||||
|
Port uint64
|
||||||
|
JaegerServer string
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitAppInfo() {
|
||||||
|
appName := utils.GetEnvVal("APP_NAME", "go-web")
|
||||||
|
port := utils.GetEnvIntVal("PORT", 8888)
|
||||||
|
jaegerServer := utils.GetEnvVal("JAEGER_SERVER", "")
|
||||||
|
|
||||||
|
AppInfo = appInfo{appName, uint64(port), jaegerServer}
|
||||||
|
}
|
6
config/config.go
Normal file
6
config/config.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
var (
|
||||||
|
AppInfo appInfo
|
||||||
|
LokiConfig lokiConfig
|
||||||
|
)
|
27
config/loki.go
Normal file
27
config/loki.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gateway/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Loki配置信息
|
||||||
|
type lokiConfig struct {
|
||||||
|
Host string
|
||||||
|
Port string
|
||||||
|
Source string
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitLokiConfig 初始化Loki配置
|
||||||
|
func InitLokiConfig() {
|
||||||
|
host := utils.GetEnvVal("LOKI_HOST", "")
|
||||||
|
port := utils.GetEnvVal("LOKI_PORT", "")
|
||||||
|
source := utils.GetEnvVal("LOKI_SOURCE", "goweb")
|
||||||
|
|
||||||
|
LokiConfig = lokiConfig{Host: host, Port: port, Source: source}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPushURL 获取组装后的日志推送接口
|
||||||
|
func (c lokiConfig) GetPushURL() string {
|
||||||
|
return fmt.Sprintf("http://%v:%v/loki/api/v1/push", c.Host, c.Port)
|
||||||
|
}
|
@ -3,4 +3,5 @@ package core
|
|||||||
var (
|
var (
|
||||||
NacosClient nacosClient
|
NacosClient nacosClient
|
||||||
ServiceMap []serviceMap
|
ServiceMap []serviceMap
|
||||||
|
Log Logger
|
||||||
)
|
)
|
||||||
|
74
core/logger.go
Normal file
74
core/logger.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logger struct {
|
||||||
|
zap *zap.SugaredLogger
|
||||||
|
loki lokiClient
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化Zap日志工具
|
||||||
|
func initZapLogger() *zap.SugaredLogger {
|
||||||
|
// 配置 sugaredLogger
|
||||||
|
writer := zapcore.AddSync(os.Stdout)
|
||||||
|
|
||||||
|
// 自定义时间输出格式
|
||||||
|
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||||
|
enc.AppendString("[" + t.Format("2006-01-02 15:04:05.000") + "]")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式相关的配置
|
||||||
|
encoderConfig := zap.NewProductionEncoderConfig()
|
||||||
|
// 修改时间戳的格式
|
||||||
|
encoderConfig.EncodeTime = customTimeEncoder
|
||||||
|
// 日志级别使用大写带颜色显示
|
||||||
|
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
|
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
||||||
|
// 将日志级别设置为 DEBUG
|
||||||
|
core := zapcore.NewCore(encoder, writer, zapcore.DebugLevel)
|
||||||
|
// 增加 caller 信息
|
||||||
|
logger := zap.New(core, zap.AddCaller())
|
||||||
|
return logger.Sugar()
|
||||||
|
}
|
||||||
|
|
||||||
|
// InitLogger 初始化日志系统
|
||||||
|
func InitLogger() {
|
||||||
|
zapLogger := initZapLogger()
|
||||||
|
loki := initLokiClient()
|
||||||
|
Log = Logger{zap: zapLogger, loki: loki}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug Debug级别日志
|
||||||
|
func (l Logger) Debug(template string, args ...interface{}) {
|
||||||
|
l.zap.Debugf(template, args...)
|
||||||
|
l.loki.Debug("", template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info Info级别日志
|
||||||
|
func (l Logger) Info(template string, args ...interface{}) {
|
||||||
|
l.zap.Infof(template, args...)
|
||||||
|
l.loki.Info("", template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn Warn级别日志
|
||||||
|
func (l Logger) Warn(template string, args ...interface{}) {
|
||||||
|
l.zap.Warnf(template, args...)
|
||||||
|
l.loki.Warn("", template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error Error级别日志
|
||||||
|
func (l Logger) Error(template string, args ...interface{}) {
|
||||||
|
l.zap.Errorf(template, args...)
|
||||||
|
l.loki.Error("", template, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Panic 打印异常并退出系统
|
||||||
|
func (l Logger) Panic(template string, args ...interface{}) {
|
||||||
|
l.loki.Error("", template, args...)
|
||||||
|
l.zap.Panicf(template, args...)
|
||||||
|
}
|
81
core/loki.go
Normal file
81
core/loki.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gateway/config"
|
||||||
|
"github.com/go-kit/kit/log"
|
||||||
|
"github.com/grafana/loki-client-go/loki"
|
||||||
|
"github.com/prometheus/common/model"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type lokiClient struct {
|
||||||
|
client *loki.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化Loki
|
||||||
|
func initLokiClient() lokiClient {
|
||||||
|
config.InitLokiConfig()
|
||||||
|
// 初始化配置
|
||||||
|
cfg, err := loki.NewDefaultConfig(config.LokiConfig.GetPushURL())
|
||||||
|
// 创建连接对象
|
||||||
|
client, err := loki.NewWithLogger(cfg, log.NewNopLogger())
|
||||||
|
if err != nil {
|
||||||
|
Log.zap.Panicf("Loki初始化失败: %v", err.Error())
|
||||||
|
}
|
||||||
|
label := model.LabelSet{"job": "initialization"}
|
||||||
|
label["source"] = model.LabelValue(config.LokiConfig.Source)
|
||||||
|
label["level"] = "Debug"
|
||||||
|
err = client.Handle(label, time.Now().Local(), "Loki服务连接成功")
|
||||||
|
if err != nil {
|
||||||
|
Log.zap.Errorf("日志推送到Loki失败: %v", err.Error())
|
||||||
|
}
|
||||||
|
return lokiClient{client: client}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送日志到Loki
|
||||||
|
func (c lokiClient) send(level string, job string, logStr string) {
|
||||||
|
label := model.LabelSet{"job": model.LabelValue(job)}
|
||||||
|
label["source"] = model.LabelValue(config.LokiConfig.Source)
|
||||||
|
label["level"] = model.LabelValue(level)
|
||||||
|
err := c.client.Handle(label, time.Now().Local(), logStr)
|
||||||
|
if err != nil {
|
||||||
|
Log.zap.Errorf("日志推送到Loki失败: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug Debug日志
|
||||||
|
func (c lokiClient) Debug(job, template string, args ...interface{}) {
|
||||||
|
logStr := template
|
||||||
|
if len(args) > 0 {
|
||||||
|
logStr = fmt.Sprintf(template, args...)
|
||||||
|
}
|
||||||
|
c.send("Debug", job, logStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info Info日志
|
||||||
|
func (c lokiClient) Info(job, template string, args ...interface{}) {
|
||||||
|
logStr := template
|
||||||
|
if len(args) > 0 {
|
||||||
|
logStr = fmt.Sprintf(template, args...)
|
||||||
|
}
|
||||||
|
c.send("Info", job, logStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn Warn日志
|
||||||
|
func (c lokiClient) Warn(job, template string, args ...interface{}) {
|
||||||
|
logStr := template
|
||||||
|
if len(args) > 0 {
|
||||||
|
logStr = fmt.Sprintf(template, args...)
|
||||||
|
}
|
||||||
|
c.send("Warn", job, logStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error Error日志
|
||||||
|
func (c lokiClient) Error(job, template string, args ...interface{}) {
|
||||||
|
logStr := template
|
||||||
|
if len(args) > 0 {
|
||||||
|
logStr = fmt.Sprintf(template, args...)
|
||||||
|
}
|
||||||
|
c.send("Error", job, logStr)
|
||||||
|
}
|
46
go.mod
46
go.mod
@ -4,42 +4,64 @@ go 1.17
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.7.4
|
github.com/gin-gonic/gin v1.7.4
|
||||||
|
github.com/go-kit/kit v0.11.0
|
||||||
|
github.com/grafana/loki-client-go v0.0.0-20210114103019-6057f29bbf5b
|
||||||
github.com/nacos-group/nacos-sdk-go v1.0.8
|
github.com/nacos-group/nacos-sdk-go v1.0.8
|
||||||
github.com/opentracing/opentracing-go v1.2.0
|
github.com/opentracing/opentracing-go v1.2.0
|
||||||
|
github.com/prometheus/common v0.30.0
|
||||||
github.com/uber/jaeger-client-go v2.29.1+incompatible
|
github.com/uber/jaeger-client-go v2.29.1+incompatible
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
go.uber.org/zap v1.17.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
|
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
|
||||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
|
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
|
||||||
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23 // indirect
|
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.1.1 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-errors/errors v1.0.1 // indirect
|
github.com/go-errors/errors v1.0.1 // indirect
|
||||||
|
github.com/go-logfmt/logfmt v0.5.0 // indirect
|
||||||
github.com/go-playground/locales v0.13.0 // indirect
|
github.com/go-playground/locales v0.13.0 // indirect
|
||||||
github.com/go-playground/universal-translator v0.17.0 // indirect
|
github.com/go-playground/universal-translator v0.17.0 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.4.1 // indirect
|
github.com/go-playground/validator/v10 v10.4.1 // indirect
|
||||||
github.com/golang/protobuf v1.3.3 // indirect
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
github.com/json-iterator/go v1.1.9 // indirect
|
github.com/golang/snappy v0.0.2 // indirect
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
|
github.com/jpillora/backoff v1.0.0 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.11 // indirect
|
||||||
github.com/leodido/go-urn v1.2.0 // indirect
|
github.com/leodido/go-urn v1.2.0 // indirect
|
||||||
github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f // indirect
|
github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f // indirect
|
||||||
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect
|
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.12 // indirect
|
github.com/mattn/go-isatty v0.0.12 // indirect
|
||||||
|
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||||
|
github.com/mitchellh/mapstructure v1.2.2 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
|
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||||
|
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/stretchr/testify v1.7.0 // indirect
|
github.com/prometheus/client_golang v1.11.0 // indirect
|
||||||
|
github.com/prometheus/client_model v0.2.0 // indirect
|
||||||
|
github.com/prometheus/procfs v0.6.0 // indirect
|
||||||
|
github.com/prometheus/prometheus v1.8.2-0.20201028100903-3245b3267b24 // indirect
|
||||||
github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3 // indirect
|
github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3 // indirect
|
||||||
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
|
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
|
||||||
github.com/ugorji/go/codec v1.1.7 // indirect
|
github.com/ugorji/go/codec v1.1.7 // indirect
|
||||||
go.uber.org/atomic v1.6.0 // indirect
|
go.uber.org/atomic v1.7.0 // indirect
|
||||||
go.uber.org/multierr v1.5.0 // indirect
|
go.uber.org/multierr v1.6.0 // indirect
|
||||||
go.uber.org/zap v1.15.0 // indirect
|
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b // indirect
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
|
||||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
|
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect
|
||||||
|
golang.org/x/text v0.3.6 // indirect
|
||||||
|
google.golang.org/appengine v1.6.6 // indirect
|
||||||
|
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
|
||||||
|
google.golang.org/grpc v1.38.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.27.1 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
||||||
gopkg.in/ini.v1 v1.42.0 // indirect
|
gopkg.in/ini.v1 v1.42.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.2.8 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
)
|
)
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
package initialization
|
package initialization
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gateway/config"
|
||||||
|
"gateway/core"
|
||||||
|
)
|
||||||
|
|
||||||
// InitSystem 初始化系统
|
// InitSystem 初始化系统
|
||||||
func InitSystem() {
|
func InitSystem() {
|
||||||
// 服务路由规则
|
// 初始化APP信息
|
||||||
//core.InitServiceMap()
|
config.InitAppInfo()
|
||||||
|
// 初始化日志工具
|
||||||
|
core.InitLogger()
|
||||||
// Nacos
|
// Nacos
|
||||||
initNacos()
|
initNacos()
|
||||||
}
|
}
|
||||||
|
41
utils/env_utils.go
Normal file
41
utils/env_utils.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 从环境变量获取字符串类型值
|
||||||
|
func GetEnvVal(key, defaultVal string) string {
|
||||||
|
val, exist := os.LookupEnv(key)
|
||||||
|
if !exist {
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从环境变量获取数字类型值
|
||||||
|
func GetEnvIntVal(key string, defaultVal int) int {
|
||||||
|
valStr, exist := os.LookupEnv(key)
|
||||||
|
if !exist {
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
val, err := strconv.Atoi(valStr)
|
||||||
|
if err != nil {
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从环境变量获取数字类型值
|
||||||
|
func GetEnvBoolVal(key string, defaultVal bool) bool {
|
||||||
|
valStr, exist := os.LookupEnv(key)
|
||||||
|
if !exist {
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
val, err := strconv.ParseBool(valStr)
|
||||||
|
if err != nil {
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user