[agent-go] 解决代码bug
This commit is contained in:
@@ -16,23 +16,26 @@ import (
|
|||||||
var log = G.LOG
|
var log = G.LOG
|
||||||
var group = ""
|
var group = ""
|
||||||
|
|
||||||
func InitNacos(configFileName string) {
|
func InitNacos(configFileName string) *viper.Viper {
|
||||||
|
|
||||||
v := parseAgentConfigFile(configFileName, nil)
|
v := parseAgentConfigFile(configFileName, nil)
|
||||||
group = v.GetString("spring.cloud.nacos.config.group")
|
group = v.GetString("spring.cloud.nacos.config.group")
|
||||||
|
|
||||||
|
// build the nacos connection
|
||||||
configClient := startNacosConnection(v)
|
configClient := startNacosConnection(v)
|
||||||
|
|
||||||
|
// get all needed nacos config and merge
|
||||||
allNacosConfig := getAllNacosConfig(v, group, configClient)
|
allNacosConfig := getAllNacosConfig(v, group, configClient)
|
||||||
|
|
||||||
for _, nacosConfigContent := range allNacosConfig {
|
for _, nacosConfigContent := range allNacosConfig {
|
||||||
log.Debug(fmt.Sprintf("nacos config conetent is %s", nacosConfigContent))
|
log.Debug(fmt.Sprintf("nacos config conetent is %s", nacosConfigContent))
|
||||||
|
|
||||||
//parseNacosConfigContend(nacosConfigContent, v)
|
parseNacosConfigContend(nacosConfigContent, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info(fmt.Sprintf("%s config read result are %v", configFileName, v.AllSettings()))
|
log.Info(fmt.Sprintf("%s config read result are %v", configFileName, v.AllSettings()))
|
||||||
|
|
||||||
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAgentConfigFile(configFileName string, v *viper.Viper) *viper.Viper {
|
func parseAgentConfigFile(configFileName string, v *viper.Viper) *viper.Viper {
|
||||||
@@ -59,7 +62,10 @@ func parseAgentConfigFile(configFileName string, v *viper.Viper) *viper.Viper {
|
|||||||
func parseNacosConfigContend(configContent string, v *viper.Viper) *viper.Viper {
|
func parseNacosConfigContend(configContent string, v *viper.Viper) *viper.Viper {
|
||||||
|
|
||||||
v.SetConfigType("yaml")
|
v.SetConfigType("yaml")
|
||||||
err := v.ReadConfig(bytes.NewBuffer([]byte(configContent)))
|
|
||||||
|
// use merge
|
||||||
|
|
||||||
|
err := v.MergeConfig(bytes.NewBuffer([]byte(configContent)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("nacos config contend read error !", zap.Error(err))
|
log.Error("nacos config contend read error !", zap.Error(err))
|
||||||
}
|
}
|
||||||
@@ -68,19 +74,21 @@ func parseNacosConfigContend(configContent string, v *viper.Viper) *viper.Viper
|
|||||||
}
|
}
|
||||||
func startNacosConnection(v *viper.Viper) config_client.IConfigClient {
|
func startNacosConnection(v *viper.Viper) config_client.IConfigClient {
|
||||||
|
|
||||||
|
serverAddr := v.GetString("spring.cloud.nacos.config.server-addr")
|
||||||
|
|
||||||
clientConfig := constant.ClientConfig{
|
clientConfig := constant.ClientConfig{
|
||||||
NamespaceId: "public",
|
//Endpoint: serverAddr,
|
||||||
|
NamespaceId: "",
|
||||||
TimeoutMs: v.GetUint64("spring.cloud.nacos.config.timeout"),
|
TimeoutMs: v.GetUint64("spring.cloud.nacos.config.timeout"),
|
||||||
NotLoadCacheAtStart: true,
|
NotLoadCacheAtStart: true,
|
||||||
AppendToStdout: true,
|
AppendToStdout: true,
|
||||||
//UpdateCacheWhenEmpty: true,
|
UpdateCacheWhenEmpty: true,
|
||||||
//LogDir: "/tmp/nacos/log",
|
//LogDir: "/tmp/nacos/log",
|
||||||
//CacheDir: "/tmp/nacos/cache",
|
//CacheDir: "/tmp/nacos/cache",
|
||||||
Username: "nacos",
|
Username: "nacos",
|
||||||
Password: "Superwmm.23",
|
Password: "Superwmm.23",
|
||||||
}
|
}
|
||||||
|
|
||||||
serverAddr := v.GetString("spring.cloud.nacos.config.server-addr")
|
|
||||||
split := strings.Split(serverAddr, ":")
|
split := strings.Split(serverAddr, ":")
|
||||||
if len(split) != 2 {
|
if len(split) != 2 {
|
||||||
log.Error("nacos server addr error!")
|
log.Error("nacos server addr error!")
|
||||||
@@ -131,15 +139,15 @@ func getAllNacosConfig(v *viper.Viper, group string, configClient config_client.
|
|||||||
|
|
||||||
for _, addConfigMap := range m {
|
for _, addConfigMap := range m {
|
||||||
|
|
||||||
real, _ := addConfigMap.(map[string]interface{})
|
realMap, _ := addConfigMap.(map[string]interface{})
|
||||||
|
|
||||||
for additionalNacosConfigFileName, additionalNacosConfigGroup := range real {
|
// 拿到配置的Key
|
||||||
|
dataId := realMap["data-id"].(string)
|
||||||
s := additionalNacosConfigGroup.(string)
|
group := realMap["group"].(string)
|
||||||
configContent := getConfig(additionalNacosConfigFileName, s, configClient)
|
|
||||||
result = append(result, configContent)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 查询
|
||||||
|
config := getConfig(dataId, group, configClient)
|
||||||
|
result = append(result, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@@ -150,13 +158,13 @@ func getConfig(dataId string, group string, configClient config_client.IConfigCl
|
|||||||
|
|
||||||
log.Debug(fmt.Sprintf("nacos config get method dataID is %s, group is %s", dataId, group))
|
log.Debug(fmt.Sprintf("nacos config get method dataID is %s, group is %s", dataId, group))
|
||||||
|
|
||||||
content := ""
|
content, err := configClient.GetConfig(vo.ConfigParam{
|
||||||
configClient.GetConfig(vo.ConfigParam{
|
|
||||||
DataId: dataId,
|
DataId: dataId,
|
||||||
Group: group,
|
Group: group,
|
||||||
Content: content,
|
|
||||||
Type: "yaml",
|
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error("nacos config get error !", zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
log.Debug(fmt.Sprintf("dataId %s , group %s, nacos config content is %s", dataId, group, content))
|
log.Debug(fmt.Sprintf("dataId %s , group %s, nacos config content is %s", dataId, group, content))
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ const (
|
|||||||
ExecOmType = "EXECUTOR"
|
ExecOmType = "EXECUTOR"
|
||||||
StatusOmType = "STATUS"
|
StatusOmType = "STATUS"
|
||||||
InitOmType = "INIT"
|
InitOmType = "INIT"
|
||||||
|
|
||||||
// write about
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger, _ = NewLogger()
|
var logger, _ = NewLogger()
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ require (
|
|||||||
github.com/spf13/viper v1.15.0
|
github.com/spf13/viper v1.15.0
|
||||||
github.com/streadway/amqp v1.0.0
|
github.com/streadway/amqp v1.0.0
|
||||||
go.uber.org/zap v1.24.0
|
go.uber.org/zap v1.24.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -48,5 +49,4 @@ require (
|
|||||||
google.golang.org/protobuf v1.28.1 // indirect
|
google.golang.org/protobuf v1.28.1 // indirect
|
||||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func main() {
|
|||||||
println(filename)
|
println(filename)
|
||||||
|
|
||||||
// 初始化Nacos的连接配置
|
// 初始化Nacos的连接配置
|
||||||
//config.InitNacos(filename)
|
g.G.NacosConfig = g.InitNacos(filename)
|
||||||
|
|
||||||
// 执行初始化之策工作
|
// 执行初始化之策工作
|
||||||
g.G.AgentServerInfo = register.INIT()
|
g.G.AgentServerInfo = register.INIT()
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ spring:
|
|||||||
file-extension: yaml
|
file-extension: yaml
|
||||||
max-retry: 3
|
max-retry: 3
|
||||||
server-addr: "150.230.198.103:21060"
|
server-addr: "150.230.198.103:21060"
|
||||||
|
# server-addr: "42.192.52.227:21060"
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
config-long-poll-timeout: 5000
|
config-long-poll-timeout: 5000
|
||||||
extension-configs:
|
extension-configs:
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package rabbitmq
|
package rabbitmq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-go/g"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/streadway/amqp"
|
"github.com/streadway/amqp"
|
||||||
)
|
)
|
||||||
@@ -23,6 +22,7 @@ type ConnectProperty struct {
|
|||||||
func Send(conn *RabbitMQConn, connProp *ConnectProperty, message []byte) {
|
func Send(conn *RabbitMQConn, connProp *ConnectProperty, message []byte) {
|
||||||
// 往哪里发
|
// 往哪里发
|
||||||
channel := conn.Channel
|
channel := conn.Channel
|
||||||
|
|
||||||
// 发送
|
// 发送
|
||||||
channel.Publish(
|
channel.Publish(
|
||||||
connProp.ExchangeName,
|
connProp.ExchangeName,
|
||||||
@@ -44,7 +44,7 @@ func Read(conn *RabbitMQConn, connProp *ConnectProperty, autoAck bool) <-chan am
|
|||||||
// 开始读取队列中的全部消息
|
// 开始读取队列中的全部消息
|
||||||
msgs, err := channel.Consume(
|
msgs, err := channel.Consume(
|
||||||
connProp.QueueName, // 队列名称
|
connProp.QueueName, // 队列名称
|
||||||
g.G.AgentServerInfo.AgentTopicName, // 消费者名称
|
"", // 消费者名称
|
||||||
autoAck, // auto-ack
|
autoAck, // auto-ack
|
||||||
false, // exclusive
|
false, // exclusive
|
||||||
false, // no-local
|
false, // no-local
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ func parseRabbitMQEndpointFromG() string {
|
|||||||
res.WriteString(host)
|
res.WriteString(host)
|
||||||
res.WriteString(":")
|
res.WriteString(":")
|
||||||
res.WriteString(port)
|
res.WriteString(port)
|
||||||
|
res.WriteString("/")
|
||||||
res.WriteString(virtualHost)
|
res.WriteString(virtualHost)
|
||||||
|
|
||||||
s := res.String()
|
s := res.String()
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func INIT() *config.AgentServerInfo {
|
|||||||
log.Error("init to server queue established error!")
|
log.Error("init to server queue established error!")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer rabbitmq.CloseChannel(initToServer)
|
//defer rabbitmq.CloseChannel(initToServer)
|
||||||
|
|
||||||
initFromServer, err := rabbitmq.NewRabbitMQConn(
|
initFromServer, err := rabbitmq.NewRabbitMQConn(
|
||||||
initFromServerProp,
|
initFromServerProp,
|
||||||
@@ -53,7 +53,7 @@ func INIT() *config.AgentServerInfo {
|
|||||||
log.Error("init from server queue established error!")
|
log.Error("init from server queue established error!")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer rabbitmq.CloseChannel(initFromServer)
|
//defer rabbitmq.CloseChannel(initFromServer)
|
||||||
|
|
||||||
// 组装OctopusMessage
|
// 组装OctopusMessage
|
||||||
var octopusMsg *config.OctopusMessage
|
var octopusMsg *config.OctopusMessage
|
||||||
@@ -77,7 +77,6 @@ func INIT() *config.AgentServerInfo {
|
|||||||
handleInitMsgFromServer(initFromServer, initFromServerProp, initToServer, initToServerProp)
|
handleInitMsgFromServer(initFromServer, initFromServerProp, initToServer, initToServerProp)
|
||||||
|
|
||||||
return agentServerInfo
|
return agentServerInfo
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleInitMsgFromServer 处理从Server接收的注册信息
|
// handleInitMsgFromServer 处理从Server接收的注册信息
|
||||||
|
|||||||
Reference in New Issue
Block a user