consul修改为单个配置,不走环境变量,新增k3s获取环境变量
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/gogf/gf/v2/net/gsel"
|
||||
"github.com/gogf/gf/v2/net/gsvc"
|
||||
"github.com/gogf/gf/v2/os/gcfg"
|
||||
"github.com/gogf/gf/v2/os/genv"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"github.com/hashicorp/consul/api"
|
||||
"github.com/r3labs/diff/v2"
|
||||
@@ -127,16 +126,17 @@ func checkConsulHealth(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func init() {
|
||||
consulAddr = g.Cfg().MustGet(context.Background(), "consul.address").String()
|
||||
ctx := context.Background()
|
||||
|
||||
consulAddr = g.Cfg().MustGet(ctx, "consul.address").String()
|
||||
if consulAddr == "" {
|
||||
g.Log().Warning(context.Background(), "⚠️ Consul 配置未找到,跳过初始化")
|
||||
g.Log().Debug(ctx, "📄 [Consul] 配置文件中未设置 consul.address,跳过 Consul 初始化")
|
||||
return
|
||||
}
|
||||
|
||||
if err := connectConsul(context.Background()); err != nil {
|
||||
g.Log().Errorf(context.Background(), "❌ Consul 初始化失败: %v", err)
|
||||
if err := connectConsul(ctx); err != nil {
|
||||
g.Log().Errorf(ctx, "❌ Consul 初始化失败: %v", err)
|
||||
} else {
|
||||
// 连接成功后启动健康检查和自动重连
|
||||
go startHealthCheckAndReconnect()
|
||||
}
|
||||
|
||||
@@ -147,14 +147,12 @@ func loadConfigFromConsul() {
|
||||
ctx := context.Background()
|
||||
|
||||
serviceName := g.Cfg().MustGet(ctx, "server.name", "admin-go").String()
|
||||
env := genv.Get("APP_ENV", "").String()
|
||||
fmt.Printf("服务名称: %s, 环境变量 APP_ENV: %s\n", serviceName, env)
|
||||
fmt.Printf("服务名称: %s\n", serviceName)
|
||||
|
||||
if env == "" {
|
||||
panic("未设置 APP_ENV 环境变量,无法从 Consul 加载配置")
|
||||
}
|
||||
consulKey := fmt.Sprintf("config/%s/%s", serviceName, serviceName)
|
||||
fmt.Printf("从 Consul 读取配置键: %s\n", consulKey)
|
||||
|
||||
consulData, lastIndex, err := loadEnvFromConsul(env, serviceName)
|
||||
consulData, lastIndex, err := loadFromConsul(consulKey)
|
||||
if err == nil && len(consulData) > 0 {
|
||||
adapter, err := gcfg.NewAdapterContent()
|
||||
if err != nil {
|
||||
@@ -166,13 +164,13 @@ func loadConfigFromConsul() {
|
||||
|
||||
fmt.Printf("已从 Consul 成功加载初始配置\n")
|
||||
|
||||
go watchConsulConfig(env, serviceName, lastIndex)
|
||||
go watchConsulConfig(consulKey, lastIndex)
|
||||
} else {
|
||||
fmt.Printf("从 Consul 获取配置失败,使用本地配置文件\n")
|
||||
}
|
||||
}
|
||||
|
||||
func loadEnvFromConsul(env string, serviceName string) ([]byte, uint64, error) {
|
||||
func loadFromConsul(consulKey string) ([]byte, uint64, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
consulAddress := g.Cfg().MustGet(ctx, "consul.address", "127.0.0.1:8500").String()
|
||||
@@ -186,8 +184,6 @@ func loadEnvFromConsul(env string, serviceName string) ([]byte, uint64, error) {
|
||||
fmt.Printf("创建 Consul 客户端失败: %v\n", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
consulKey := fmt.Sprintf("config/%s/%s-%s", serviceName, serviceName, env)
|
||||
fmt.Printf("从 Consul 读取配置键: %s\n", consulKey)
|
||||
|
||||
kv := client.KV()
|
||||
|
||||
@@ -211,7 +207,7 @@ func loadEnvFromConsul(env string, serviceName string) ([]byte, uint64, error) {
|
||||
return pair.Value, meta.LastIndex, nil
|
||||
}
|
||||
|
||||
func watchConsulConfig(env string, serviceName string, lastIndex uint64) {
|
||||
func watchConsulConfig(consulKey string, lastIndex uint64) {
|
||||
ctx := context.Background()
|
||||
|
||||
consulAddress := g.Cfg().MustGet(ctx, "consul.address", "127.0.0.1:8500").String()
|
||||
@@ -224,8 +220,6 @@ func watchConsulConfig(env string, serviceName string, lastIndex uint64) {
|
||||
return
|
||||
}
|
||||
|
||||
consulKey := fmt.Sprintf("config/%s/%s-%s", serviceName, serviceName, env)
|
||||
|
||||
for {
|
||||
opts := &api.QueryOptions{
|
||||
WaitIndex: lastIndex,
|
||||
|
||||
Reference in New Issue
Block a user