From 298a5d37217e5bfc234c30ca9b30cbaedd5f3edc Mon Sep 17 00:00:00 2001 From: orgin Date: Thu, 13 Oct 2022 13:54:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96service=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cluster/parsecfg.go | 86 ++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/cluster/parsecfg.go b/cluster/parsecfg.go index 87d6492..9a0b54d 100644 --- a/cluster/parsecfg.go +++ b/cluster/parsecfg.go @@ -116,44 +116,80 @@ func (cls *Cluster) readLocalService(localNodeId int) error { 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 { - if f.IsDir() == false { - filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name() + if f.IsDir() == true { + continue + } + + filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name() currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(filePath) - if err != nil { - continue + if err != nil { + continue + } + + if currGlobalCfg != nil { + //不允许重复的配置global配置 + if globalCfg != nil { + return fmt.Errorf("[Global] does not allow repeated configuration in %s.",f.Name()) } + globalCfg = currGlobalCfg + } - if currGlobalCfg != nil { - cls.globalCfg = currGlobalCfg - } - - for _, s := range cls.localNodeInfo.ServiceList { - for { - //取公共服务配置 - pubCfg, ok := serviceConfig[s] - if ok == true { - cls.localServiceCfg[s] = pubCfg + //保存公共配置 + for _, s := range cls.localNodeInfo.ServiceList { + for { + //取公共服务配置 + pubCfg, ok := serviceConfig[s] + if ok == true { + 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] - if ok == false { - break - } - sCfg, ok := nodeService[s] - if ok == false { - break - } - - cls.localServiceCfg[s] = sCfg + //取指定结点配置的服务 + nodeServiceCfg,ok := mapNodeService[localNodeId] + if ok == false { break } + nodeCfg, ok := nodeServiceCfg[s] + if ok == false { + break + } + + 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 } } } + //组合所有的配置 + 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 }