Browse Source

根据配置能运行成不同的服务

master
李寻欢 4 months ago
parent
commit
9514264681
  1. 1
      .drone.yml
  2. 17
      config/app_info.go
  3. 7
      config/config.go
  4. 27
      config/loki.go
  5. 28
      config/nacos.go
  6. 6
      core/core.go
  7. 74
      core/logger.go
  8. 81
      core/loki.go
  9. 15
      core/nacos.go
  10. 49
      go.mod
  11. 1170
      go.sum
  12. 15
      initialization/init.go
  13. 92
      initialization/nacos.go
  14. 142
      main.go
  15. 78
      middleware/jaeger.go
  16. 41
      utils/env_utils.go
  17. 25
      utils/ip_utils.go

1
.drone.yml

@ -1,5 +1,6 @@
kind: pipeline
name: build
steps:
- name: 打包Docker镜像并推送
image: plugins/docker

17
config/app_info.go

@ -0,0 +1,17 @@
package config
import "api/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}
}

7
config/config.go

@ -0,0 +1,7 @@
package config
var (
AppInfo appInfo
LokiConfig lokiConfig
NacosConfig nacosConfig
)

27
config/loki.go

@ -0,0 +1,27 @@
package config
import (
"api/utils"
"fmt"
)
// 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)
}

28
config/nacos.go

@ -0,0 +1,28 @@
package config
import (
"api/utils"
)
// Nacos配置
type nacosConfig struct {
Host string // 主机
Port uint64 // 端口
NamespaceId string // 命名空间
CenterConfigName string // 外部配置文件名,多个以逗号隔开
}
// InitNacosConfig 初始化OSS配置
func InitNacosConfig() {
host := utils.GetEnvVal("NACOS_HOST", "nacos-headless")
port := utils.GetEnvIntVal("NACOS_PORT", 8848)
namespaceId := utils.GetEnvVal("NACOS_NAMESPACE", "")
centerConfigName := utils.GetEnvVal("NACOS_CONFIG_NAME", "application.yml")
NacosConfig = nacosConfig{
Host: host,
Port: uint64(port),
NamespaceId: namespaceId,
CenterConfigName: centerConfigName,
}
}

6
core/core.go

@ -0,0 +1,6 @@
package core
var (
Log Logger
NacosClient nacosClient
)

74
core/logger.go

@ -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

@ -0,0 +1,81 @@
package core
import (
"api/config"
"fmt"
"github.com/AllCute/loki-client-go/loki"
"github.com/go-kit/kit/log"
"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)
}

15
core/nacos.go

@ -0,0 +1,15 @@
package core
import (
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
)
type nacosClient struct {
client naming_client.INamingClient
config config_client.IConfigClient
}
func NewNacosClient(c naming_client.INamingClient, n config_client.IConfigClient) nacosClient {
return nacosClient{c, n}
}

49
go.mod

@ -3,35 +3,64 @@ module api
go 1.17
require (
github.com/AllCute/loki-client-go v1.0.0
github.com/gin-gonic/gin v1.7.4
github.com/go-kit/kit v0.11.0
github.com/nacos-group/nacos-sdk-go v1.0.8
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
go.uber.org/zap v1.17.0
)
require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // 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/cespare/xxhash/v2 v2.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // 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/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // 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/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f // indirect
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // 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/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/pkg/errors v0.9.1 // 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/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
go.uber.org/zap v1.15.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // 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/ini.v1 v1.42.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

1170
go.sum

File diff suppressed because it is too large

15
initialization/init.go

@ -0,0 +1,15 @@
package initialization
import (
"api/config"
"api/core"
)
func InitSystem() {
// 初始化APP信息
config.InitAppInfo()
// 初始化日志工具
core.InitLogger()
initNacos()
}

92
initialization/nacos.go

@ -0,0 +1,92 @@
package initialization
import (
"api/config"
"api/core"
"api/utils"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
)
// 初始化Nacos注册
func initNacos() {
config.InitNacosConfig()
sc := []constant.ServerConfig{
*constant.NewServerConfig(config.NacosConfig.Host, config.NacosConfig.Port),
}
cc := constant.ClientConfig{
AppName: config.AppInfo.AppName,
NamespaceId: config.NacosConfig.NamespaceId,
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
RotateTime: "1h",
MaxAge: 3,
LogLevel: "debug",
}
configParam := vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
}
client, err := clients.NewNamingClient(configParam)
if err != nil {
panic(err)
}
ip := config.AppInfo.AppName
if ips := utils.GetIps(); ips != nil {
ip = ips[0]
}
success, err := client.RegisterInstance(vo.RegisterInstanceParam{
Ip: ip,
Port: config.AppInfo.Port,
Weight: 10,
Enable: true,
Healthy: true,
ServiceName: config.AppInfo.AppName,
Ephemeral: true,
})
core.Log.Debug("Nacos注册结果: %v", success)
if !success {
core.Log.Panic("服务注册失败,退出程序: %v", err.Error())
}
// 打印所有服务
go func() {
instances, err := client.SelectAllInstances(vo.SelectAllInstancesParam{
ServiceName: config.AppInfo.AppName,
})
if err != nil {
core.Log.Error("获取所有服务失败")
return
}
core.Log.Debug("获取到服务总数: %v", len(instances))
for _, instance := range instances {
core.Log.Debug("服务ID: %v --> %v:%v", instance.InstanceId, instance.Ip, instance.Port)
}
}()
//configClient, err := clients.NewConfigClient(configParam)
//if err != nil {
// core.Log.Error("创建配置连接失败: %v", err.Error())
//}
//_ = configClient.ListenConfig(vo.ConfigParam{
// DataId: config.NacosConfig.CenterConfigName,
// Group: "DEFAULT_GROUP",
// OnChange: configChanged,
//})
// 读取初始配置
//core.NacosClient = core.NewNacosClient(client, configClient)
//configStr, err := configClient.GetConfig(vo.ConfigParam{DataId: "gateway.yml", Group: "DEFAULT_GROUP"})
//if err != nil {
// core.Log.Panic("读取配置文件错误: %v", err.Error())
//}
//core.ConfigChanged(configStr)
}
//func configChanged(namespace, group, dataId, data string) {
// core.ConfigChanged(data)
//}

142
main.go

@ -1,137 +1,51 @@
package main
import (
"api/config"
"api/initialization"
"api/middleware"
"api/utils"
"fmt"
"github.com/gin-gonic/gin"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var port = "8888"
func initNacos() {
sc := []constant.ServerConfig{
*constant.NewServerConfig("nacos", 8848),
}
cc := constant.ClientConfig{
AppName: "api1",
NamespaceId: "", //namespace id
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
RotateTime: "1h",
MaxAge: 3,
LogLevel: "debug",
}
func main() {
initialization.InitSystem()
configParam := vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
}
app := gin.Default()
client, err := clients.NewNamingClient(configParam)
app.Use(middleware.OpenTracing())
if err != nil {
panic(err)
}
portNum, _ := strconv.Atoi(port)
pp := uint64(portNum)
ip := "api-demo"
if ips := getIps(); ips != nil {
ip = ips[0]
}
ipStr := strings.Join(utils.GetIps(), ",")
regParam := vo.RegisterInstanceParam{
Ip: ip,
Port: pp,
Weight: 10,
Enable: true,
Healthy: true,
ServiceName: "api1",
Ephemeral: true,
}
success, err := client.RegisterInstance(regParam)
log.Println("Nacos注册结果: ", success)
if err != nil {
log.Println("Nacos注册错误信息: ", err.Error())
}
if !success {
retry := 0
for true {
time.Sleep(5 * time.Second)
retry += 1
success, err = client.RegisterInstance(regParam)
log.Printf("Nacos第%v次注册结果: %v\n", retry, success)
if err != nil {
log.Println("Nacos注册错误信息: ", err.Error())
}
if success || retry == 10 {
break
}
}
}
group := app.Group(fmt.Sprintf("/%v", config.AppInfo.AppName))
configClient, err := clients.NewConfigClient(configParam)
if err != nil {
log.Fatalf("创建配置连接失败: %v", err.Error())
}
_ = configClient.ListenConfig(vo.ConfigParam{
DataId: "api.yml",
Group: "DEFAULT_GROUP",
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("监控到配置文件变动: group:" + group + ", dataId:" + dataId + ", data:" + data)
},
group.GET("/hello", func(context *gin.Context) {
name := context.Query("name")
context.String(http.StatusBadGateway, fmt.Sprintf("[GET]-[%v]-[latest]\n%v\n%v", config.AppInfo.AppName, ipStr, name))
})
//log.Println("监听配置失败: ", err.Error())
}
func getIps() []string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil
}
var ips []string
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip := ipnet.IP.String()
//fmt.Println(ip)
ips = append(ips, ip)
}
group.POST("/hello", func(context *gin.Context) {
type param struct {
User string `json:"user"`
Age int `json:"age"`
}
}
//ipStr := strings.Join(aaaa, ",")
return ips
}
func main() {
args := os.Args
if len(args) > 1 {
port = args[1]
}
app := gin.Default()
initNacos()
ipStr := strings.Join(getIps(), ",")
app.GET("/hello", func(context *gin.Context) {
context.String(http.StatusOK, "[GET]-[API]-[latest]\n"+ipStr)
p := param{}
err := context.ShouldBindJSON(&p)
if err != nil {
context.String(http.StatusInternalServerError, "服务异常")
return
}
context.String(http.StatusOK, fmt.Sprintf("[GET]-[%v]-[latest]\n%v\n%v", config.AppInfo.AppName, ipStr, p.User))
})
app.POST("/hello", func(context *gin.Context) {
context.String(http.StatusOK, "[POST]-[API]-[latest]\n"+ipStr)
group.GET("/ip", func(context *gin.Context) {
time.Sleep(1 * time.Second)
context.String(http.StatusOK, fmt.Sprintf("[%v]\n%v", config.AppInfo.AppName, ipStr))
})
app.GET("/ip", func(context *gin.Context) {
context.String(http.StatusOK, strings.Join(getIps(), ","))
})
app.Run(fmt.Sprintf(":%v", port))
app.Run(fmt.Sprintf(":%v", config.AppInfo.Port))
}

78
middleware/jaeger.go

@ -0,0 +1,78 @@
package middleware
import (
gc "api/config"
"fmt"
"github.com/gin-gonic/gin"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go/config"
"io"
)
func OpenTracing() gin.HandlerFunc {
return func(c *gin.Context) {
var parentSpan opentracing.Span
tracer, closer := NewJaegerTracer()
defer closer.Close()
spCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header))
if err != nil {
//core.Log.Error("Jaeger错误: %v", err.Error())
parentSpan = tracer.StartSpan(c.Request.URL.Path)
defer parentSpan.Finish()
} else {
parentSpan = opentracing.StartSpan(
c.Request.URL.Path,
opentracing.ChildOf(spCtx),
opentracing.Tag{Key: string(ext.Component), Value: "HTTP"},
ext.SpanKindRPCServer,
)
defer parentSpan.Finish()
}
c.Set("Tracer", tracer)
asd := fmt.Sprintf("%v", parentSpan.Context())
c.Set("UberTraceId", asd)
c.Set("ParentSpanContext", parentSpan.Context())
// 设置接口地址
ext.HTTPUrl.Set(parentSpan, c.Request.RequestURI)
// 设置请求方式
ext.HTTPMethod.Set(parentSpan, c.Request.Method)
// 执行后续操作
c.Next()
// 设置返回状态码
ext.HTTPStatusCode.Set(parentSpan, uint16(c.Writer.Status()))
// 如果状态码大于299,设置错误标签
if c.Writer.Status() > 299 {
ext.Error.Set(parentSpan, true)
}
}
}
func NewJaegerTracer() (opentracing.Tracer, io.Closer) {
cfg := &config.Configuration{
Sampler: &config.SamplerConfig{
Type: "const", //固定采样
Param: 1, //1=全采样、0=不采样
},
Reporter: &config.ReporterConfig{
LogSpans: true,
LocalAgentHostPort: gc.AppInfo.JaegerServer,
},
ServiceName: gc.AppInfo.AppName,
}
tracer, closer, err := cfg.NewTracer()
if err != nil {
//core.Log.Error("Jaeger连接失败: %v", err.Error())
return nil, nil
}
//core.Log.Debug("Jaeger连接成功")
opentracing.SetGlobalTracer(tracer)
return tracer, closer
}

41
utils/env_utils.go

@ -0,0 +1,41 @@
package utils
import (
"os"
"strconv"
)
// GetEnvVal 从环境变量获取字符串类型值
func GetEnvVal(key, defaultVal string) string {
val, exist := os.LookupEnv(key)
if !exist {
return defaultVal
}
return val
}
// GetEnvIntVal 从环境变量获取数字类型值
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
}
// GetEnvBoolVal 从环境变量获取数字类型值
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
}

25
utils/ip_utils.go

@ -0,0 +1,25 @@
package utils
import (
"net"
)
// GetIps 获取本机IP地址
func GetIps() []string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil
}
var ips []string
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip := ipnet.IP.String()
ips = append(ips, ip)
}
}
}
//ipStr := strings.Join(ips, ",")
return ips
}
Loading…
Cancel
Save