mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化服务发现
This commit is contained in:
35
README.md
35
README.md
@@ -947,20 +947,45 @@ origin引擎默认使用读取所有结点配置的进行确认结点有哪些Se
|
|||||||
"Private": false,
|
"Private": false,
|
||||||
"remark": "//以_打头的,表示只在本机进程,不对整个子网开发",
|
"remark": "//以_打头的,表示只在本机进程,不对整个子网开发",
|
||||||
"ServiceList": ["_TestService1", "TestService9", "TestService10"],
|
"ServiceList": ["_TestService1", "TestService9", "TestService10"],
|
||||||
"DiscoveryService": [
|
"AllowDiscovery": [
|
||||||
{
|
{
|
||||||
"MasterNodeId": "nodeid_1",
|
"MasterNodeId": "nodeid_1",
|
||||||
"NetworkName":"networkname1"
|
"NetworkName":"networkname1",
|
||||||
"DiscoveryService": ["TestService8"]
|
"NodeIdList":[".*server"],
|
||||||
|
"ServiceList": ["TestService8"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
DiscoveryService:在当前nodeid为nodeid_test的结点中,只发现 MasterNodeId为nodeid_1或NetworkName为networkname1网络中的TestService8服务。
|
以上,如果是使用Etcd发现模式,则表示可以发现网络名networkname1,NodeId为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使用
|
第八章:HttpService使用
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/duanhf2012/origin/v2/event"
|
"github.com/duanhf2012/origin/v2/event"
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
"github.com/duanhf2012/origin/v2/rpc"
|
"github.com/duanhf2012/origin/v2/rpc"
|
||||||
@@ -24,10 +26,14 @@ const (
|
|||||||
Discard NodeStatus = 1 //丢弃
|
Discard NodeStatus = 1 //丢弃
|
||||||
)
|
)
|
||||||
|
|
||||||
type DiscoveryService struct {
|
|
||||||
MasterNodeId string //要筛选的主结点Id,如果不配置或者配置成0,表示针对所有的主结点
|
|
||||||
NetworkName string //如果是etcd,指定要筛选的网络名中的服务,不配置,表示所有的网络
|
// AllowDiscovery 允许发现的网络服务
|
||||||
ServiceList []string //只发现的服务列表
|
type AllowDiscovery struct {
|
||||||
|
MasterNodeId string // 支持正则表达式
|
||||||
|
NetworkName string // 支持正则表达式
|
||||||
|
NodeIdList []string // 支持正则表达式
|
||||||
|
ServiceList []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeInfo struct {
|
type NodeInfo struct {
|
||||||
@@ -38,7 +44,7 @@ type NodeInfo struct {
|
|||||||
CompressBytesLen int //超过字节进行压缩的长度
|
CompressBytesLen int //超过字节进行压缩的长度
|
||||||
ServiceList []string //所有的有序服务列表
|
ServiceList []string //所有的有序服务列表
|
||||||
PublicServiceList []string //对外公开的服务列表
|
PublicServiceList []string //对外公开的服务列表
|
||||||
DiscoveryService []DiscoveryService //筛选发现的服务,如果不配置,不进行筛选
|
AllowDiscovery []AllowDiscovery //允许发现的网络服务
|
||||||
status NodeStatus
|
status NodeStatus
|
||||||
Retire bool
|
Retire bool
|
||||||
}
|
}
|
||||||
@@ -462,30 +468,80 @@ func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo, bool) {
|
|||||||
return nodeInfo.nodeInfo, true
|
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
|
canDiscovery := true
|
||||||
|
// 筛选允许的服务
|
||||||
splitServiceName := strings.Split(serviceName, ":")
|
splitServiceName := strings.Split(serviceName, ":")
|
||||||
if len(splitServiceName) == 2 {
|
if len(splitServiceName) == 2 {
|
||||||
serviceName = splitServiceName[0]
|
serviceName = splitServiceName[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(cls.GetLocalNodeInfo().DiscoveryService); i++ {
|
// 先筛选允许的网络,有配置才会检测
|
||||||
masterNodeId := cls.GetLocalNodeInfo().DiscoveryService[i].MasterNodeId
|
if len(cls.GetLocalNodeInfo().AllowDiscovery) > 0 {
|
||||||
//无效的配置,则跳过
|
allowNetwork := false
|
||||||
if masterNodeId == rpc.NodeIdNull && len(cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList) == 0 {
|
for i := 0; i < len(cls.GetLocalNodeInfo().AllowDiscovery); i++ {
|
||||||
continue
|
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
|
// 如果配置了网络及Master结点,则匹配之
|
||||||
if masterNodeId == fromMasterNodeId || masterNodeId == rpc.NodeIdNull {
|
if fromNetworkName!="" {
|
||||||
for _, discoveryService := range cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList {
|
matchNetWork, _ := regexp.MatchString(networkName, fromNetworkName)
|
||||||
if discoveryService == serviceName {
|
if !matchNetWork {
|
||||||
return true
|
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
|
return canDiscovery
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -292,7 +292,7 @@ func (ed *EtcdDiscoveryService) setNodeInfo(networkName string, nodeInfo *rpc.No
|
|||||||
//筛选关注的服务
|
//筛选关注的服务
|
||||||
var discoverServiceSlice = make([]string, 0, 24)
|
var discoverServiceSlice = make([]string, 0, 24)
|
||||||
for _, pubService := range nodeInfo.PublicServiceList {
|
for _, pubService := range nodeInfo.PublicServiceList {
|
||||||
if cluster.CanDiscoveryService(networkName, pubService) == true {
|
if cluster.CanDiscoveryService(networkName, "",nodeInfo.NodeId,pubService) == true {
|
||||||
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -548,7 +548,7 @@ func (dc *OriginDiscoveryClient) setNodeInfo(masterNodeId string, nodeInfo *rpc.
|
|||||||
//筛选关注的服务
|
//筛选关注的服务
|
||||||
var discoverServiceSlice = make([]string, 0, 24)
|
var discoverServiceSlice = make([]string, 0, 24)
|
||||||
for _, pubService := range nodeInfo.PublicServiceList {
|
for _, pubService := range nodeInfo.PublicServiceList {
|
||||||
if cluster.CanDiscoveryService(masterNodeId, pubService) == true {
|
if cluster.CanDiscoveryService("",masterNodeId, nodeInfo.NodeId,pubService) == true {
|
||||||
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
discoverServiceSlice = append(discoverServiceSlice, pubService)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user