优化服务发现

This commit is contained in:
boyce
2025-12-10 10:13:53 +08:00
parent 593abd263e
commit e0d412810f
4 changed files with 106 additions and 25 deletions

View File

@@ -947,20 +947,45 @@ origin引擎默认使用读取所有结点配置的进行确认结点有哪些Se
"Private": false,
"remark": "//以_打头的表示只在本机进程不对整个子网开发",
"ServiceList": ["_TestService1", "TestService9", "TestService10"],
"DiscoveryService": [
"AllowDiscovery": [
{
"MasterNodeId": "nodeid_1",
"NetworkName":"networkname1"
"DiscoveryService": ["TestService8"]
"NetworkName":"networkname1",
"NodeIdList":[".*server"],
"ServiceList": ["TestService8"]
}
]
}]
}
```
DiscoveryService在当前nodeid为nodeid_test的结点中只发现 MasterNodeId为nodeid_1或NetworkName为networkname1网络中的TestService8服务。
以上如果是使用Etcd发现模式则表示可以发现网络名networkname1NodeId为server结尾服务名为TestService8服务。
AllowDiscovery可以配置发现的规则如果只配置MasterNodeId或NetworkName时(如果使用Etcd则只配置NetworkName,Origin则只配置MasterNodeId),则会筛选指定网络的所有服务,如下:
```
"AllowDiscovery": [
{
"NetworkName":"networkname1",
}
]
```
则以上只匹配networkname1网络名的所有服务支持正则表达式例如可以配置为"NetworkName":".*name1",则可以发现网络名为name1结尾的所有服务。
如果只发现NodeId为server结尾的所有服务可以使用以下配置方式
```
"AllowDiscovery": [
{
"NodeIdList":[".*server"]
}
]
```
筛选服务也是同上。也可以组合配置NetworkName和NodeIdList配置。
**注意**MasterNodeId与NetworkName只配置一个分别在模式为origin或者etcd服务发现类型时。
第八章HttpService使用
-----------------------

View File

@@ -7,6 +7,8 @@ import (
"strings"
"sync"
"regexp"
"github.com/duanhf2012/origin/v2/event"
"github.com/duanhf2012/origin/v2/log"
"github.com/duanhf2012/origin/v2/rpc"
@@ -24,10 +26,14 @@ const (
Discard NodeStatus = 1 //丢弃
)
type DiscoveryService struct {
MasterNodeId string //要筛选的主结点Id如果不配置或者配置成0表示针对所有的主结点
NetworkName string //如果是etcd指定要筛选的网络名中的服务不配置表示所有的网络
ServiceList []string //只发现的服务列表
// AllowDiscovery 允许发现的网络服务
type AllowDiscovery struct {
MasterNodeId string // 支持正则表达式
NetworkName string // 支持正则表达式
NodeIdList []string // 支持正则表达式
ServiceList []string
}
type NodeInfo struct {
@@ -38,7 +44,7 @@ type NodeInfo struct {
CompressBytesLen int //超过字节进行压缩的长度
ServiceList []string //所有的有序服务列表
PublicServiceList []string //对外公开的服务列表
DiscoveryService []DiscoveryService //筛选发现的服务,如果不配置,不进行筛选
AllowDiscovery []AllowDiscovery //允许发现的网络服务
status NodeStatus
Retire bool
}
@@ -462,30 +468,80 @@ func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo, bool) {
return nodeInfo.nodeInfo, true
}
func (cls *Cluster) CanDiscoveryService(fromMasterNodeId string, serviceName string) bool {
func (cls *Cluster) CanDiscoveryService(fromNetworkName string,fromMasterNodeId string,fromNodeId string, serviceName string) bool {
canDiscovery := true
// 筛选允许的服务
splitServiceName := strings.Split(serviceName, ":")
if len(splitServiceName) == 2 {
serviceName = splitServiceName[0]
}
for i := 0; i < len(cls.GetLocalNodeInfo().DiscoveryService); i++ {
masterNodeId := cls.GetLocalNodeInfo().DiscoveryService[i].MasterNodeId
//无效的配置,则跳过
if masterNodeId == rpc.NodeIdNull && len(cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList) == 0 {
continue
}
// 先筛选允许的网络,有配置才会检测
if len(cls.GetLocalNodeInfo().AllowDiscovery) > 0 {
allowNetwork := false
for i := 0; i < len(cls.GetLocalNodeInfo().AllowDiscovery); i++ {
masterNodeId := cls.GetLocalNodeInfo().AllowDiscovery[i].MasterNodeId
networkName := cls.GetLocalNodeInfo().AllowDiscovery[i].NetworkName
nodeIdList := cls.GetLocalNodeInfo().AllowDiscovery[i].NodeIdList
serviceList := cls.GetLocalNodeInfo().AllowDiscovery[i].ServiceList
canDiscovery = false
if masterNodeId == fromMasterNodeId || masterNodeId == rpc.NodeIdNull {
for _, discoveryService := range cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList {
if discoveryService == serviceName {
return true
// 如果配置了网络及Master结点则匹配之
if fromNetworkName!="" {
matchNetWork, _ := regexp.MatchString(networkName, fromNetworkName)
if !matchNetWork {
continue
}
}else if fromMasterNodeId!="" {
matchMasterNode, _ := regexp.MatchString(masterNodeId, fromMasterNodeId)
if !matchMasterNode {
continue
}
}
// 如果配置了
if len(nodeIdList)>0 {
hasNode := false
for _, nodeId := range nodeIdList {
matchNodeId, _ := regexp.MatchString(nodeId, fromNodeId)
if !matchNodeId {
continue
}
hasNode = true
break
}
if !hasNode {
continue
}
}
// 如果配置了服务,则匹配之
if len(serviceList)>0 {
hasService := false
for _, service := range serviceList {
// service按正则表达式匹配serviceName
matched, _ := regexp.MatchString(service, serviceName)
if matched {
hasService = true
break
}
}
if !hasService {
continue
}
}
allowNetwork = true
break
}
if !allowNetwork {
return false
}
}
return canDiscovery
}

View File

@@ -292,7 +292,7 @@ func (ed *EtcdDiscoveryService) setNodeInfo(networkName string, nodeInfo *rpc.No
//筛选关注的服务
var discoverServiceSlice = make([]string, 0, 24)
for _, pubService := range nodeInfo.PublicServiceList {
if cluster.CanDiscoveryService(networkName, pubService) == true {
if cluster.CanDiscoveryService(networkName, "",nodeInfo.NodeId,pubService) == true {
discoverServiceSlice = append(discoverServiceSlice, pubService)
}
}

View File

@@ -548,7 +548,7 @@ func (dc *OriginDiscoveryClient) setNodeInfo(masterNodeId string, nodeInfo *rpc.
//筛选关注的服务
var discoverServiceSlice = make([]string, 0, 24)
for _, pubService := range nodeInfo.PublicServiceList {
if cluster.CanDiscoveryService(masterNodeId, pubService) == true {
if cluster.CanDiscoveryService("",masterNodeId, nodeInfo.NodeId,pubService) == true {
discoverServiceSlice = append(discoverServiceSlice, pubService)
}
}