mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化配置读取,去消默认cluster目录
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/duanhf2012/origin/v2/rpc"
|
"github.com/duanhf2012/origin/v2/rpc"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -70,12 +71,8 @@ type NodeInfoList struct {
|
|||||||
NodeList []NodeInfo
|
NodeList []NodeInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func validConfigFile(f os.DirEntry) bool {
|
func validConfigFile(f string) bool {
|
||||||
if f.IsDir() == true || (filepath.Ext(f.Name()) != ".json" && filepath.Ext(f.Name()) != ".yml" && filepath.Ext(f.Name()) != ".yaml") {
|
return strings.HasSuffix(f, ".json")|| strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".yaml")
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func yamlToJson(data []byte, v interface{}) ([]byte, error) {
|
func yamlToJson(data []byte, v interface{}) ([]byte, error) {
|
||||||
@@ -277,32 +274,33 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
|||||||
var discoveryInfo DiscoveryInfo
|
var discoveryInfo DiscoveryInfo
|
||||||
var rpcMode RpcMode
|
var rpcMode RpcMode
|
||||||
|
|
||||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
|
||||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
|
||||||
if err != nil {
|
|
||||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||||
for _, f := range fileInfoList {
|
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error)error {
|
||||||
if !validConfigFile(f) {
|
if info.IsDir() {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
if err != nil {
|
||||||
fileNodeInfoList, rErr := cls.ReadClusterConfig(filePath)
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !validConfigFile(info.Name()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fileNodeInfoList, rErr := cls.ReadClusterConfig(path)
|
||||||
if rErr != nil {
|
if rErr != nil {
|
||||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read file path %s is error:%+v", filePath, rErr)
|
return fmt.Errorf("read file path %s is error:%+v", path, rErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cls.SetRpcMode(&fileNodeInfoList.RpcMode, &rpcMode)
|
err = cls.SetRpcMode(&fileNodeInfoList.RpcMode, &rpcMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return discoveryInfo, nil, rpcMode, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = discoveryInfo.setDiscovery(&fileNodeInfoList.Discovery)
|
err = discoveryInfo.setDiscovery(&fileNodeInfoList.Discovery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return discoveryInfo, nil, rpcMode, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, nodeInfo := range fileNodeInfoList.NodeList {
|
for _, nodeInfo := range fileNodeInfoList.NodeList {
|
||||||
@@ -310,6 +308,12 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
|||||||
nodeInfoList = append(nodeInfoList, nodeInfo)
|
nodeInfoList = append(nodeInfoList, nodeInfo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return discoveryInfo, nil, rpcMode, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if nodeId != rpc.NodeIdNull && (len(nodeInfoList) != 1) {
|
if nodeId != rpc.NodeIdNull && (len(nodeInfoList) != 1) {
|
||||||
@@ -331,32 +335,32 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cls *Cluster) readLocalService(localNodeId string) error {
|
func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
|
||||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var globalCfg interface{}
|
var globalCfg interface{}
|
||||||
publicService := map[string]interface{}{}
|
publicService := map[string]interface{}{}
|
||||||
nodeService := map[string]interface{}{}
|
nodeService := map[string]interface{}{}
|
||||||
|
|
||||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||||
for _, f := range fileInfoList {
|
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error)error{
|
||||||
if !validConfigFile(f) {
|
if info.IsDir() {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
|
||||||
currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(filePath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !validConfigFile(info.Name()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if currGlobalCfg != nil {
|
if currGlobalCfg != nil {
|
||||||
//不允许重复的配置global配置
|
//不允许重复的配置global配置
|
||||||
if globalCfg != nil {
|
if globalCfg != nil {
|
||||||
return fmt.Errorf("[Global] does not allow repeated configuration in %s", f.Name())
|
return fmt.Errorf("[Global] does not allow repeated configuration in %s", info.Name())
|
||||||
}
|
}
|
||||||
globalCfg = currGlobalCfg
|
globalCfg = currGlobalCfg
|
||||||
}
|
}
|
||||||
@@ -372,7 +376,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
|||||||
pubCfg, ok := serviceConfig[s]
|
pubCfg, ok := serviceConfig[s]
|
||||||
if ok == true {
|
if ok == true {
|
||||||
if _, publicOk := publicService[s]; publicOk == true {
|
if _, publicOk := publicService[s]; publicOk == true {
|
||||||
return fmt.Errorf("public service [%s] does not allow repeated configuration in %s", s, f.Name())
|
return fmt.Errorf("public service [%s] does not allow repeated configuration in %s", s, info.Name())
|
||||||
}
|
}
|
||||||
publicService[s] = pubCfg
|
publicService[s] = pubCfg
|
||||||
}
|
}
|
||||||
@@ -388,12 +392,17 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if _, nodeOK := nodeService[s]; nodeOK == true {
|
if _, nodeOK := nodeService[s]; nodeOK == true {
|
||||||
return fmt.Errorf("NodeService NodeId[%s] Service[%s] does not allow repeated configuration in %s", cls.localNodeInfo.NodeId, s, f.Name())
|
return fmt.Errorf("NodeService NodeId[%s] Service[%s] does not allow repeated configuration in %s", cls.localNodeInfo.NodeId, s, info.Name())
|
||||||
}
|
}
|
||||||
nodeService[s] = nodeCfg
|
nodeService[s] = nodeCfg
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//组合所有的配置
|
//组合所有的配置
|
||||||
|
|||||||
Reference in New Issue
Block a user