mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-06 14:17:31 +08:00
优化代码规范
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/rpc"
|
||||
@@ -9,25 +10,24 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
type EtcdList struct {
|
||||
NetworkName []string
|
||||
Endpoints []string
|
||||
Endpoints []string
|
||||
}
|
||||
|
||||
type EtcdDiscovery struct {
|
||||
DialTimeoutMillisecond time.Duration
|
||||
TTLSecond int64
|
||||
TTLSecond int64
|
||||
|
||||
EtcdList []EtcdList
|
||||
}
|
||||
|
||||
type OriginDiscovery struct {
|
||||
TTLSecond int64
|
||||
TTLSecond int64
|
||||
MasterNodeList []NodeInfo
|
||||
}
|
||||
|
||||
@@ -35,40 +35,39 @@ type DiscoveryType int
|
||||
|
||||
const (
|
||||
InvalidType = 0
|
||||
OriginType = 1
|
||||
EtcdType = 2
|
||||
OriginType = 1
|
||||
EtcdType = 2
|
||||
)
|
||||
|
||||
const MinTTL = 3
|
||||
|
||||
type DiscoveryInfo struct {
|
||||
discoveryType DiscoveryType
|
||||
Etcd *EtcdDiscovery //etcd
|
||||
Origin *OriginDiscovery //orign
|
||||
Etcd *EtcdDiscovery //etcd
|
||||
Origin *OriginDiscovery //origin
|
||||
}
|
||||
|
||||
type NatsConfig struct {
|
||||
NatsUrl string
|
||||
NatsUrl string
|
||||
NoRandomize bool
|
||||
}
|
||||
|
||||
type RpcMode struct {
|
||||
Typ string `json:"Type"`
|
||||
Nats NatsConfig
|
||||
|
||||
Typ string `json:"Type"`
|
||||
Nats NatsConfig
|
||||
}
|
||||
|
||||
type NodeInfoList struct {
|
||||
RpcMode RpcMode
|
||||
Discovery DiscoveryInfo
|
||||
NodeList []NodeInfo
|
||||
RpcMode RpcMode
|
||||
Discovery DiscoveryInfo
|
||||
NodeList []NodeInfo
|
||||
}
|
||||
|
||||
func (d *DiscoveryInfo) getDiscoveryType() DiscoveryType{
|
||||
func (d *DiscoveryInfo) getDiscoveryType() DiscoveryType {
|
||||
return d.discoveryType
|
||||
}
|
||||
|
||||
func (d *DiscoveryInfo) setDiscovery(discoveryInfo *DiscoveryInfo) error{
|
||||
func (d *DiscoveryInfo) setDiscovery(discoveryInfo *DiscoveryInfo) error {
|
||||
var err error
|
||||
err = d.setOrigin(discoveryInfo.Origin)
|
||||
if err != nil {
|
||||
@@ -83,30 +82,30 @@ func (d *DiscoveryInfo) setDiscovery(discoveryInfo *DiscoveryInfo) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiscoveryInfo) setEtcd(etcd *EtcdDiscovery) error{
|
||||
func (d *DiscoveryInfo) setEtcd(etcd *EtcdDiscovery) error {
|
||||
if etcd == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if d.discoveryType != InvalidType {
|
||||
return fmt.Errorf("Repeat configuration of Discovery")
|
||||
if d.discoveryType != InvalidType {
|
||||
return fmt.Errorf("repeat configuration of Discovery")
|
||||
}
|
||||
|
||||
//Endpoints不允许重复
|
||||
mapAddr:=make (map[string]struct{})
|
||||
mapAddr := make(map[string]struct{})
|
||||
for _, n := range etcd.EtcdList {
|
||||
for _,endPoint := range n.Endpoints {
|
||||
if _,ok:=mapAddr[endPoint];ok == true {
|
||||
return fmt.Errorf("etcd discovery config Etcd.EtcdList.Endpoints %+v is repeat",endPoint)
|
||||
for _, endPoint := range n.Endpoints {
|
||||
if _, ok := mapAddr[endPoint]; ok == true {
|
||||
return fmt.Errorf("etcd discovery config Etcd.EtcdList.Endpoints %+v is repeat", endPoint)
|
||||
}
|
||||
mapAddr[endPoint] = struct{}{}
|
||||
}
|
||||
|
||||
//networkName不允许重复
|
||||
mapNetworkName := make(map[string]struct{})
|
||||
for _,netName := range n.NetworkName{
|
||||
if _,ok := mapNetworkName[netName];ok == true {
|
||||
return fmt.Errorf("etcd discovery config Etcd.EtcdList.NetworkName %+v is repeat",n.NetworkName)
|
||||
for _, netName := range n.NetworkName {
|
||||
if _, ok := mapNetworkName[netName]; ok == true {
|
||||
return fmt.Errorf("etcd discovery config Etcd.EtcdList.NetworkName %+v is repeat", n.NetworkName)
|
||||
}
|
||||
|
||||
mapNetworkName[netName] = struct{}{}
|
||||
@@ -124,13 +123,13 @@ func (d *DiscoveryInfo) setEtcd(etcd *EtcdDiscovery) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DiscoveryInfo) setOrigin(originDiscovery *OriginDiscovery) error{
|
||||
if originDiscovery== nil || len(originDiscovery.MasterNodeList)==0 {
|
||||
func (d *DiscoveryInfo) setOrigin(originDiscovery *OriginDiscovery) error {
|
||||
if originDiscovery == nil || len(originDiscovery.MasterNodeList) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if d.discoveryType != InvalidType {
|
||||
return fmt.Errorf("Repeat configuration of Discovery")
|
||||
return fmt.Errorf("repeat configuration of Discovery")
|
||||
}
|
||||
|
||||
mapListenAddr := make(map[string]struct{})
|
||||
@@ -204,7 +203,7 @@ func (cls *Cluster) readServiceConfig(filepath string) (interface{}, map[string]
|
||||
return GlobalCfg, serviceConfig, mapNodeService, nil
|
||||
}
|
||||
|
||||
func (cls *Cluster) SetRpcMode(cfgRpcMode *RpcMode,rpcMode *RpcMode) error {
|
||||
func (cls *Cluster) SetRpcMode(cfgRpcMode *RpcMode, rpcMode *RpcMode) error {
|
||||
//忽略掉没有设置的配置
|
||||
if cfgRpcMode.Typ == "" {
|
||||
return nil
|
||||
@@ -212,16 +211,16 @@ func (cls *Cluster) SetRpcMode(cfgRpcMode *RpcMode,rpcMode *RpcMode) error {
|
||||
|
||||
//不允许重复的配置Rpc模式
|
||||
|
||||
if cfgRpcMode.Typ != "" && rpcMode.Typ != ""{
|
||||
if cfgRpcMode.Typ != "" && rpcMode.Typ != "" {
|
||||
return errors.New("repeat config RpcMode")
|
||||
}
|
||||
|
||||
//检查Typ是否合法
|
||||
if cfgRpcMode.Typ!="Nats" && cfgRpcMode.Typ!="Default" {
|
||||
if cfgRpcMode.Typ != "Nats" && cfgRpcMode.Typ != "Default" {
|
||||
return fmt.Errorf("RpcMode %s is not support", rpcMode.Typ)
|
||||
}
|
||||
|
||||
if cfgRpcMode.Typ == "Nats" && len(cfgRpcMode.Nats.NatsUrl)==0 {
|
||||
if cfgRpcMode.Typ == "Nats" && len(cfgRpcMode.Nats.NatsUrl) == 0 {
|
||||
return fmt.Errorf("nats rpc mode config NatsUrl is empty")
|
||||
}
|
||||
|
||||
@@ -230,7 +229,7 @@ func (cls *Cluster) SetRpcMode(cfgRpcMode *RpcMode,rpcMode *RpcMode) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []NodeInfo,RpcMode, error) {
|
||||
func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []NodeInfo, RpcMode, error) {
|
||||
var nodeInfoList []NodeInfo
|
||||
var discoveryInfo DiscoveryInfo
|
||||
var rpcMode RpcMode
|
||||
@@ -238,27 +237,26 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
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)
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
}
|
||||
|
||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||
for _, f := range fileInfoList {
|
||||
if f.IsDir() == false {
|
||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
||||
fileNodeInfoList, rerr := cls.ReadClusterConfig(filePath)
|
||||
|
||||
if rerr != nil {
|
||||
return discoveryInfo, nil,rpcMode, fmt.Errorf("read file path %s is error:%+v", filePath, rerr)
|
||||
fileNodeInfoList, rErr := cls.ReadClusterConfig(filePath)
|
||||
if rErr != nil {
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read file path %s is error:%+v", filePath, rErr)
|
||||
}
|
||||
|
||||
err = cls.SetRpcMode(&fileNodeInfoList.RpcMode,&rpcMode)
|
||||
err = cls.SetRpcMode(&fileNodeInfoList.RpcMode, &rpcMode)
|
||||
if err != nil {
|
||||
return discoveryInfo, nil,rpcMode, err
|
||||
return discoveryInfo, nil, rpcMode, err
|
||||
}
|
||||
|
||||
err = discoveryInfo.setDiscovery(&fileNodeInfoList.Discovery)
|
||||
if err != nil {
|
||||
return discoveryInfo,nil,rpcMode,err
|
||||
return discoveryInfo, nil, rpcMode, err
|
||||
}
|
||||
|
||||
for _, nodeInfo := range fileNodeInfoList.NodeList {
|
||||
@@ -270,10 +268,10 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
}
|
||||
|
||||
if nodeId != rpc.NodeIdNull && (len(nodeInfoList) != 1) {
|
||||
return discoveryInfo, nil,rpcMode, fmt.Errorf("nodeid %s configuration error in NodeList", nodeId)
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("nodeid %s configuration error in NodeList", nodeId)
|
||||
}
|
||||
|
||||
for i, _ := range nodeInfoList {
|
||||
for i := range nodeInfoList {
|
||||
for j, s := range nodeInfoList[i].ServiceList {
|
||||
//私有结点不加入到Public服务列表中
|
||||
if strings.HasPrefix(s, "_") == false && nodeInfoList[i].Private == false {
|
||||
@@ -284,17 +282,17 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
}
|
||||
}
|
||||
|
||||
return discoveryInfo, nodeInfoList, rpcMode,nil
|
||||
return discoveryInfo, nodeInfoList, rpcMode, nil
|
||||
}
|
||||
|
||||
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)
|
||||
return fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
}
|
||||
|
||||
var globalCfg interface{}
|
||||
var globalCfg interface{}
|
||||
publicService := map[string]interface{}{}
|
||||
nodeService := map[string]interface{}{}
|
||||
|
||||
@@ -304,12 +302,12 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
continue
|
||||
}
|
||||
|
||||
if filepath.Ext(f.Name())!= ".json" {
|
||||
if filepath.Ext(f.Name()) != ".json" {
|
||||
continue
|
||||
}
|
||||
|
||||
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 {
|
||||
continue
|
||||
}
|
||||
@@ -317,7 +315,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
if currGlobalCfg != nil {
|
||||
//不允许重复的配置global配置
|
||||
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", f.Name())
|
||||
}
|
||||
globalCfg = currGlobalCfg
|
||||
}
|
||||
@@ -325,21 +323,21 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
//保存公共配置
|
||||
for _, s := range cls.localNodeInfo.ServiceList {
|
||||
for {
|
||||
splitServiceName := strings.Split(s,":")
|
||||
splitServiceName := strings.Split(s, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
s = splitServiceName[0]
|
||||
}
|
||||
//取公共服务配置
|
||||
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())
|
||||
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
|
||||
}
|
||||
|
||||
//取指定结点配置的服务
|
||||
nodeServiceCfg,ok := mapNodeService[localNodeId]
|
||||
nodeServiceCfg, ok := mapNodeService[localNodeId]
|
||||
if ok == false {
|
||||
break
|
||||
}
|
||||
@@ -348,8 +346,8 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
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())
|
||||
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())
|
||||
}
|
||||
nodeService[s] = nodeCfg
|
||||
break
|
||||
@@ -359,7 +357,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
|
||||
//组合所有的配置
|
||||
for _, s := range cls.localNodeInfo.ServiceList {
|
||||
splitServiceName := strings.Split(s,":")
|
||||
splitServiceName := strings.Split(s, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
s = splitServiceName[0]
|
||||
}
|
||||
@@ -367,16 +365,16 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
//先从NodeService中找
|
||||
var serviceCfg interface{}
|
||||
var ok bool
|
||||
serviceCfg,ok = nodeService[s]
|
||||
serviceCfg, ok = nodeService[s]
|
||||
if ok == true {
|
||||
cls.localServiceCfg[s] =serviceCfg
|
||||
cls.localServiceCfg[s] = serviceCfg
|
||||
continue
|
||||
}
|
||||
|
||||
//如果找不到从PublicService中找
|
||||
serviceCfg,ok = publicService[s]
|
||||
serviceCfg, ok = publicService[s]
|
||||
if ok == true {
|
||||
cls.localServiceCfg[s] =serviceCfg
|
||||
cls.localServiceCfg[s] = serviceCfg
|
||||
}
|
||||
}
|
||||
cls.globalCfg = globalCfg
|
||||
@@ -387,23 +385,22 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
func (cls *Cluster) parseLocalCfg() {
|
||||
rpcInfo := NodeRpcInfo{}
|
||||
rpcInfo.nodeInfo = cls.localNodeInfo
|
||||
rpcInfo.client = rpc.NewLClient(rpcInfo.nodeInfo.NodeId,&cls.callSet)
|
||||
rpcInfo.client = rpc.NewLClient(rpcInfo.nodeInfo.NodeId, &cls.callSet)
|
||||
|
||||
cls.mapRpc[cls.localNodeInfo.NodeId] = &rpcInfo
|
||||
|
||||
for _, serviceName := range cls.localNodeInfo.ServiceList {
|
||||
splitServiceName := strings.Split(serviceName,":")
|
||||
splitServiceName := strings.Split(serviceName, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
serviceName = splitServiceName[0]
|
||||
templateServiceName := splitServiceName[1]
|
||||
//记录模板
|
||||
if _, ok := cls.mapTemplateServiceNode[templateServiceName]; ok == false {
|
||||
cls.mapTemplateServiceNode[templateServiceName]=map[string]struct{}{}
|
||||
cls.mapTemplateServiceNode[templateServiceName] = map[string]struct{}{}
|
||||
}
|
||||
cls.mapTemplateServiceNode[templateServiceName][serviceName] = struct{}{}
|
||||
}
|
||||
|
||||
|
||||
if _, ok := cls.mapServiceNode[serviceName]; ok == false {
|
||||
cls.mapServiceNode[serviceName] = make(map[string]struct{})
|
||||
}
|
||||
@@ -427,7 +424,7 @@ func (cls *Cluster) InitCfg(localNodeId string) error {
|
||||
cls.mapTemplateServiceNode = map[string]map[string]struct{}{}
|
||||
|
||||
//加载本地结点的NodeList配置
|
||||
discoveryInfo, nodeInfoList,rpcMode, err := cls.readLocalClusterConfig(localNodeId)
|
||||
discoveryInfo, nodeInfoList, rpcMode, err := cls.readLocalClusterConfig(localNodeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -463,24 +460,24 @@ func (cls *Cluster) GetNodeIdByTemplateService(templateServiceName string, rpcCl
|
||||
defer cls.locker.RUnlock()
|
||||
|
||||
mapServiceName := cls.mapTemplateServiceNode[templateServiceName]
|
||||
for serviceName := range mapServiceName {
|
||||
mapNodeId, ok := cls.mapServiceNode[serviceName]
|
||||
if ok == true {
|
||||
for nodeId, _ := range mapNodeId {
|
||||
pClient,retire := GetCluster().getRpcClient(nodeId)
|
||||
if pClient == nil || pClient.IsConnected() == false {
|
||||
continue
|
||||
}
|
||||
for serviceName := range mapServiceName {
|
||||
mapNodeId, ok := cls.mapServiceNode[serviceName]
|
||||
if ok == true {
|
||||
for nodeId := range mapNodeId {
|
||||
pClient, retire := GetCluster().getRpcClient(nodeId)
|
||||
if pClient == nil || pClient.IsConnected() == false {
|
||||
continue
|
||||
}
|
||||
|
||||
//如果需要筛选掉退休的,对retire状态的结点略过
|
||||
if filterRetire == true && retire == true {
|
||||
continue
|
||||
}
|
||||
//如果需要筛选掉退休的,对retire状态的结点略过
|
||||
if filterRetire == true && retire == true {
|
||||
continue
|
||||
}
|
||||
|
||||
rpcClientList = append(rpcClientList,pClient)
|
||||
}
|
||||
}
|
||||
}
|
||||
rpcClientList = append(rpcClientList, pClient)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, rpcClientList
|
||||
}
|
||||
@@ -490,8 +487,8 @@ func (cls *Cluster) GetNodeIdByService(serviceName string, rpcClientList []*rpc.
|
||||
defer cls.locker.RUnlock()
|
||||
mapNodeId, ok := cls.mapServiceNode[serviceName]
|
||||
if ok == true {
|
||||
for nodeId, _ := range mapNodeId {
|
||||
pClient,retire := GetCluster().getRpcClient(nodeId)
|
||||
for nodeId := range mapNodeId {
|
||||
pClient, retire := GetCluster().getRpcClient(nodeId)
|
||||
if pClient == nil || pClient.IsConnected() == false {
|
||||
continue
|
||||
}
|
||||
@@ -501,7 +498,7 @@ func (cls *Cluster) GetNodeIdByService(serviceName string, rpcClientList []*rpc.
|
||||
continue
|
||||
}
|
||||
|
||||
rpcClientList = append(rpcClientList,pClient)
|
||||
rpcClientList = append(rpcClientList, pClient)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user