Skip to content

Commit

Permalink
fix: instance strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
如漫 committed Apr 11, 2024
1 parent a15babc commit 24e81b8
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions clients/naming_client/naming_grpc/naming_grpc_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package naming_grpc

import (
"context"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -221,8 +222,12 @@ func (proxy *NamingGrpcProxy) Subscribe(serviceName, groupName string, clusters
//过滤不符合当前节点标签的实例
if len(subscribeServiceResponse.ServiceInfo.Hosts) != 0 {
tag := os.Getenv("ALICLOUD_SERVICE_TAG")
tagMapList := make([]model.Instance, 0)
backUpMapList := make([]model.Instance, 0)
if tag == "" {
tag = "base"
}
fmt.Printf("[NamingGrpcProxy.Subscribe] instance tag: %v\n", tag)
tagMapList := make([]model.Instance, 0) //标签节点列表
backUpMapList := make([]model.Instance, 0) //普通节点列表

for _, host := range subscribeServiceResponse.ServiceInfo.Hosts {
// 如果没有metadata, 认为是普通实例
Expand All @@ -232,25 +237,28 @@ func (proxy *NamingGrpcProxy) Subscribe(serviceName, groupName string, clusters
}

instanceTag, ok := host.Metadata["alicloud.service.tag"]
fmt.Printf("[NamingGrpcProxy.Subscribe] host: %v, metadata : %v\n", host, instanceTag)
if !ok || instanceTag == "base" || instanceTag == "" { //普通节点,加入到backUp列表中
backUpMapList = append(backUpMapList, host)
continue
}

if instanceTag == tag { // 节点标签和当前标签能够匹配,直接加入列表
tagMapList = append(tagMapList, host)
} else if (tag == "base" && instanceTag == "") || (tag == "" && instanceTag == "base") || (tag == "" && instanceTag == "") { //兼容普通节点的情况
if instanceTag == tag {
tagMapList = append(tagMapList, host)
}
}

if len(tagMapList) != 0 {
if tag != "base" && len(tagMapList) != 0 {
fmt.Printf("[NamingGrpcProxy.Subscribe] change host list, tag: %v, list: %v\n", tag, tagMapList)
subscribeServiceResponse.ServiceInfo.Hosts = tagMapList
} else if len(backUpMapList) != 0 {
}
if (tag == "base" || tag == "") && len(backUpMapList) != 0 {
fmt.Printf("[NamingGrpcProxy.Subscribe] change host list, tag: %v, list: %v\n", tag, tagMapList)
subscribeServiceResponse.ServiceInfo.Hosts = backUpMapList
}
}

fmt.Printf("[NamingGrpcProxy.Subscribe] final service info: %v\n", subscribeServiceResponse.ServiceInfo)
return subscribeServiceResponse.ServiceInfo, nil
}

Expand Down

0 comments on commit 24e81b8

Please sign in to comment.