Skip to content

Commit

Permalink
feat(tags): support inject default tags from env (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
whalecold authored Dec 13, 2023
1 parent a730df4 commit 0ac4fe9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func main() {
| serverAddr | 127.0.0.1 | nacos server address |
| serverPort | 8848 | nacos server port |
| namespace | | the namespaceId of nacos |
| NACOS_ENV_TAGS | "" | support inject default tags|

### More Info

Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func main() {
| serverAddr | 127.0.0.1 | nacos 服务器地址 |
| serverPort | 8848 | nacos 服务器端口 |
| namespace | | nacos 中的 namespace Id |
| NACOS_ENV_TAGS | "" | 支持注入默认 nacos tags|

### 更多信息

Expand Down
24 changes: 24 additions & 0 deletions nacos/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,43 @@ package nacos
import (
"os"
"strconv"
"strings"

"github.com/cloudwego/kitex/pkg/klog"
)

const (
NACOS_ENV_SERVER_ADDR = "serverAddr"
NACOS_ENV_PORT = "serverPort"
NACOS_ENV_TAGS = "NACOS_ENV_TAGS"
NACOS_ENV_NAMESPACE_ID = "namespace"
NACOS_DEFAULT_SERVER_ADDR = "127.0.0.1"
NACOS_DEFAULT_PORT = 8848
NACOS_DEFAULT_REGIONID = "cn-hangzhou"
)

// Tags providers the default tags to inject nacos.
var Tags map[string]string

func init() {
Tags = parseTags(os.Getenv(NACOS_ENV_TAGS))
}

func parseTags(tags string) map[string]string {
if len(tags) == 0 {
return nil
}
out := map[string]string{}
parts := strings.Split(tags, ",")
for _, part := range parts {
tag := strings.Split(part, "=")
if len(tag) == 2 {
out[tag[0]] = tag[1]
}
}
return out
}

// NacosPort Get Nacos port from environment variables
func NacosPort() int64 {
portText := os.Getenv(NACOS_ENV_PORT)
Expand Down
7 changes: 7 additions & 0 deletions nacos/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ func TestEnvFunc(t *testing.T) {
assert.Equal(t, "127.0.0.1", NacosAddr())
assert.Equal(t, "public", NacosNameSpaceId())
}

func TestParseTags(t *testing.T) {
assert.Equal(t, parseTags("k1=v1,k2=v2"), map[string]string{
"k1": "v1",
"k2": "v2",
})
}
20 changes: 19 additions & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ func (n *nacosRegistry) Register(info *registry.Info) error {
return fmt.Errorf("parse registry info addr error: %w", err)
}
}

_, e := n.cli.RegisterInstance(vo.RegisterInstanceParam{
Ip: host,
Port: uint64(p),
ServiceName: info.ServiceName,
Weight: float64(info.Weight),
Enable: true,
Healthy: true,
Metadata: info.Tags,
Metadata: mergeTags(info.Tags, nacos.Tags),
GroupName: n.opts.group,
ClusterName: n.opts.cluster,
Ephemeral: true,
Expand Down Expand Up @@ -171,3 +172,20 @@ func (n *nacosRegistry) Deregister(info *registry.Info) error {
}
return nil
}

// should not modify the source data.
func mergeTags(ts ...map[string]string) map[string]string {
if len(ts) == 0 {
return nil
}
if len(ts) == 1 {
return ts[0]
}
tags := map[string]string{}
for _, t := range ts {
for k, v := range t {
tags[k] = v
}
}
return tags
}
26 changes: 26 additions & 0 deletions registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,29 @@ func TestNacosMultipleInstancesWithDefaultNacosRegistry(t *testing.T) {
}
}
}

func TestMergeTags(t *testing.T) {
t1 := map[string]string{
"k1": "v1",
"k2": "v2",
}
t2 := map[string]string{
"k3": "v3",
"k4": "v4",
}
merged := mergeTags(t1, t2)
assert.Equal(t, merged, map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
assert.Equal(t, t1, map[string]string{
"k1": "v1",
"k2": "v2",
})
assert.Equal(t, t2, map[string]string{
"k3": "v3",
"k4": "v4",
})
}

0 comments on commit 0ac4fe9

Please sign in to comment.