优化service配置读取规则

This commit is contained in:
orgin
2022-10-13 13:54:07 +08:00
parent 64fb9368bf
commit 298a5d3721

View File

@@ -116,9 +116,16 @@ func (cls *Cluster) readLocalService(localNodeId int) error {
return fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err) return fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err)
} }
var globalCfg interface{}
publicService := map[string]interface{}{}
nodeService := map[string]interface{}{}
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件 //读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
for _, f := range fileInfoList { for _, f := range fileInfoList {
if f.IsDir() == false { if f.IsDir() == true {
continue
}
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name() filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(filePath) currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(filePath)
if err != nil { if err != nil {
@@ -126,34 +133,63 @@ func (cls *Cluster) readLocalService(localNodeId int) error {
} }
if currGlobalCfg != nil { if currGlobalCfg != nil {
cls.globalCfg = currGlobalCfg //不允许重复的配置global配置
if globalCfg != nil {
return fmt.Errorf("[Global] does not allow repeated configuration in %s.",f.Name())
}
globalCfg = currGlobalCfg
} }
//保存公共配置
for _, s := range cls.localNodeInfo.ServiceList { for _, s := range cls.localNodeInfo.ServiceList {
for { for {
//取公共服务配置 //取公共服务配置
pubCfg, ok := serviceConfig[s] pubCfg, ok := serviceConfig[s]
if ok == true { if ok == true {
cls.localServiceCfg[s] = pubCfg if _,publicOk := publicService[s];publicOk == true {
return fmt.Errorf("public service [%s] does not allow repeated configuration in %s.",s,f.Name())
}
publicService[s] = pubCfg
} }
//如果结点配置了该服务,则覆盖之 //取指定结点配置的服务
nodeService, ok := mapNodeService[localNodeId] nodeServiceCfg,ok := mapNodeService[localNodeId]
if ok == false { if ok == false {
break break
} }
sCfg, ok := nodeService[s] nodeCfg, ok := nodeServiceCfg[s]
if ok == false { if ok == false {
break break
} }
cls.localServiceCfg[s] = sCfg if _,nodeOK := nodeService[s];nodeOK == true {
return fmt.Errorf("NodeService NodeId[%d] Service[%s] does not allow repeated configuration in %s.",cls.localNodeInfo.NodeId,s,f.Name())
}
nodeService[s] = nodeCfg
break break
} }
} }
} }
//组合所有的配置
for _, s := range cls.localNodeInfo.ServiceList {
//先从NodeService中找
var serviceCfg interface{}
var ok bool
serviceCfg,ok = nodeService[s]
if ok == true {
cls.localServiceCfg[s] =serviceCfg
continue
} }
//如果找不到从PublicService中找
serviceCfg,ok = publicService[s]
if ok == true {
cls.localServiceCfg[s] =serviceCfg
}
}
cls.globalCfg = globalCfg
return nil return nil
} }