[agent-go] 完成RabbitMQ连接部分的代码
This commit is contained in:
105
agent-go/register/AgentIntitilization.go
Normal file
105
agent-go/register/AgentIntitilization.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package register
|
||||
|
||||
import (
|
||||
"agent-go/g"
|
||||
"agent-go/rabbitmq"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
var log = g.G.LOG
|
||||
var omType = g.InitOmType
|
||||
|
||||
func INIT() {
|
||||
|
||||
// 获取系统的环境变量
|
||||
agentServerInfo := parseAgentServerInfo()
|
||||
|
||||
nacosConfig := g.G.NacosConfig
|
||||
|
||||
initToServerProp := &rabbitmq.ConnectProperty{
|
||||
ExchangeName: nacosConfig.GetString("octopus.message.init_exchange"),
|
||||
QueueName: nacosConfig.GetString("octopus.message.init_to_server"),
|
||||
ExchangeType: g.QueueDirect,
|
||||
TopicKey: nacosConfig.GetString("octopus.message.init_to_server_key"),
|
||||
}
|
||||
|
||||
initFromServerProp := &rabbitmq.ConnectProperty{
|
||||
ExchangeName: nacosConfig.GetString("octopus.message.init_exchange"),
|
||||
QueueName: nacosConfig.GetString("octopus.message.init_from_server"),
|
||||
ExchangeType: g.QueueDirect,
|
||||
TopicKey: nacosConfig.GetString("octopus.message.init_from_server_key"),
|
||||
}
|
||||
|
||||
// 建立RabbitMQ的连接
|
||||
// defer 关闭初始化连接
|
||||
initToServer, err := rabbitmq.NewRabbitMQConn(
|
||||
initToServerProp,
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("init to server queue established error!")
|
||||
panic(err)
|
||||
}
|
||||
defer rabbitmq.CloseChannel(initToServer)
|
||||
|
||||
initFromServer, err := rabbitmq.NewRabbitMQConn(
|
||||
initFromServerProp,
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("init from server queue established error!")
|
||||
panic(err)
|
||||
}
|
||||
defer rabbitmq.CloseChannel(initFromServer)
|
||||
|
||||
// 组装OctopusMessage
|
||||
var octopusMsg *rabbitmq.OctopusMessage
|
||||
octopusMsg = octopusMsg.BuildOctopusMsg(
|
||||
omType,
|
||||
agentServerInfo,
|
||||
)
|
||||
msgBytes, err := json.Marshal(octopusMsg)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("octopus message convert to json is wrong! msg is => %v", octopusMsg))
|
||||
}
|
||||
// 发送OM至MQ中O
|
||||
rabbitmq.Send(
|
||||
initToServer,
|
||||
initToServerProp,
|
||||
msgBytes,
|
||||
)
|
||||
|
||||
// 监听初始化连接中的信息
|
||||
// 建立运行时RabbitMQ连接
|
||||
handleInitMsgFromServer()
|
||||
|
||||
}
|
||||
|
||||
func handleInitMsgFromServer() {
|
||||
|
||||
}
|
||||
|
||||
func parseAgentServerInfo() AgentServerInfo {
|
||||
|
||||
// 约定文件地址为 /etc/environment.d/octopus-agent.conf
|
||||
// 目前使用
|
||||
var agentServerInfo AgentServerInfo
|
||||
yamlFile, err := ioutil.ReadFile("C:\\Users\\wdd\\IdeaProjects\\ProjectOctopus\\agent-go\\server-env.yaml")
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to read YAML file: %v", err))
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(yamlFile, &agentServerInfo)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to unmarshal YAML: %v", err))
|
||||
}
|
||||
|
||||
jsonFormat, err := json.Marshal(agentServerInfo)
|
||||
if err != nil {
|
||||
return AgentServerInfo{}
|
||||
}
|
||||
log.Info(fmt.Sprintf("agent server info is %v", string(jsonFormat)))
|
||||
|
||||
return agentServerInfo
|
||||
}
|
||||
Reference in New Issue
Block a user