115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
|
package env
|
|||
|
|
|||
|
import (
|
|||
|
"encoding/json"
|
|||
|
"fmt"
|
|||
|
"net/url"
|
|||
|
"strings"
|
|||
|
"sync"
|
|||
|
|
|||
|
"git.wishpal.cn/wishpal_ironfan/xframe/component/agollo/env/config"
|
|||
|
"git.wishpal.cn/wishpal_ironfan/xframe/component/agollo/utils"
|
|||
|
"git.wishpal.cn/wishpal_ironfan/xframe/component/logger"
|
|||
|
)
|
|||
|
|
|||
|
const (
|
|||
|
defaultNotificationID = int64(-1)
|
|||
|
comma = ","
|
|||
|
)
|
|||
|
|
|||
|
var (
|
|||
|
//app config
|
|||
|
appConfig *config.AppConfig
|
|||
|
//real servers ip
|
|||
|
servers sync.Map
|
|||
|
|
|||
|
//next try connect period - 60 second
|
|||
|
nextTryConnectPeriod int64 = 60
|
|||
|
)
|
|||
|
|
|||
|
// InitConfig 使用指定配置初始化配置
|
|||
|
func InitConfig(cfg *config.AppConfig) {
|
|||
|
appConfig = cfg
|
|||
|
}
|
|||
|
|
|||
|
// SplitNamespaces 根据namespace字符串分割后,并执行callback函数
|
|||
|
func SplitNamespaces(namespacesStr string, callback func(namespace string)) *sync.Map {
|
|||
|
namespaces := sync.Map{}
|
|||
|
split := strings.Split(namespacesStr, comma)
|
|||
|
for _, namespace := range split {
|
|||
|
if callback != nil {
|
|||
|
callback(namespace)
|
|||
|
}
|
|||
|
namespaces.Store(namespace, defaultNotificationID)
|
|||
|
}
|
|||
|
return &namespaces
|
|||
|
}
|
|||
|
|
|||
|
// SyncServerIPListSuccessCallBack 同步服务器列表成功后的回调
|
|||
|
func SyncServerIPListSuccessCallBack(responseBody []byte) (o interface{}, err error) {
|
|||
|
logger.Debugln("get all server info:", string(responseBody))
|
|||
|
|
|||
|
tmpServerInfo := make([]*config.ServerInfo, 0)
|
|||
|
err = json.Unmarshal(responseBody, &tmpServerInfo)
|
|||
|
if err != nil {
|
|||
|
logger.Errorln("Unmarshal json Fail,Error:", err)
|
|||
|
return
|
|||
|
}
|
|||
|
if len(tmpServerInfo) == 0 {
|
|||
|
logger.Infoln("get no real server!")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
for _, server := range tmpServerInfo {
|
|||
|
if server == nil {
|
|||
|
continue
|
|||
|
}
|
|||
|
servers.Store(server.HomepageURL, server)
|
|||
|
}
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
// SetDownNode 设置失效节点
|
|||
|
func SetDownNode(host string) {
|
|||
|
if host == "" || appConfig == nil {
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
if host == appConfig.GetHost() {
|
|||
|
appConfig.SetNextTryConnTime(nextTryConnectPeriod)
|
|||
|
}
|
|||
|
|
|||
|
servers.Range(func(k, v interface{}) bool {
|
|||
|
server := v.(*config.ServerInfo)
|
|||
|
// if some node has down then select next node
|
|||
|
if strings.Index(k.(string), host) > -1 {
|
|||
|
server.IsDown = true
|
|||
|
return false
|
|||
|
}
|
|||
|
return true
|
|||
|
})
|
|||
|
}
|
|||
|
|
|||
|
// GetAppConfig 获取app配置
|
|||
|
func GetAppConfig() *config.AppConfig {
|
|||
|
return appConfig
|
|||
|
}
|
|||
|
|
|||
|
// GetServicesConfigURL 获取服务器列表url
|
|||
|
func GetServicesConfigURL(config *config.AppConfig) string {
|
|||
|
return fmt.Sprintf("%sservices/config?appId=%s&ip=%s",
|
|||
|
config.GetHost(),
|
|||
|
url.QueryEscape(config.AppID),
|
|||
|
utils.GetInternal())
|
|||
|
}
|
|||
|
|
|||
|
// GetPlainAppConfig 获取原始配置
|
|||
|
func GetPlainAppConfig() *config.AppConfig {
|
|||
|
return appConfig
|
|||
|
}
|
|||
|
|
|||
|
// GetServers 获取服务器数组
|
|||
|
func GetServers() *sync.Map {
|
|||
|
return &servers
|
|||
|
}
|