Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(consul): use registry info tag #101

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion consul/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,21 @@ func (c *consulRegistry) Register(info *registry.Info) error {
return fmt.Errorf("getting service id failed, err: %w", err)
}

tags, err := convTagMapToSlice(info.Tags)
if err == nil {
for _, tag := range c.opts.AdditionInfo.Tags {
if !inArray(tag, tags) {
tags = append(tags, tag)
}
}
}

svcInfo := &api.AgentServiceRegistration{
ID: svcID,
Name: info.ServiceName,
Address: host,
Port: port,
Tags: c.opts.AdditionInfo.Tags,
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
Tags: tags,
Meta: c.opts.AdditionInfo.Meta,
Weights: &api.AgentWeights{
Passing: info.Weight,
Expand Down
3 changes: 2 additions & 1 deletion consul/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ func (c *consulResolver) Resolve(_ context.Context, desc string) (discovery.Resu
if svc == nil || svc.Address == "" {
continue
}
tags := splitTags(svc.Tags)
eps = append(eps, discovery.NewInstance(
defaultNetwork,
net.JoinHostPort(svc.Address, fmt.Sprintf("%d", svc.Port)),
svc.Weights.Passing,
svc.Meta,
tags,
))
}

Expand Down
71 changes: 70 additions & 1 deletion consul/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,37 @@
package consul

import (
"errors"
"fmt"
"net"
"strconv"
"strings"

"github.com/cloudwego/hertz/pkg/app/server/registry"
"github.com/cloudwego/hertz/pkg/common/utils"
)

const kvJoinChar = ":"

var errIllegalTagChar = errors.New("illegal tag character")

func parseAddr(addr net.Addr) (host string, port int, err error) {
host, portStr, err := net.SplitHostPort(addr.String())
if err != nil {
return "", 0, fmt.Errorf("calling net.SplitHostPort failed, addr: %s, err: %w", addr.String(), err)
}

if host == "" || host == "::" {
return "", 0, fmt.Errorf("empty host")
detectHost := utils.LocalIP()
if detectHost == utils.UNKNOWN_IP_ADDR {
return "", 0, fmt.Errorf("get local ip error")
}

host, _, err = net.SplitHostPort(detectHost)

if err != nil {
return "", 0, fmt.Errorf("empty host")
}
}

port, err = strconv.Atoi(portStr)
Expand All @@ -50,3 +66,56 @@ func getServiceId(info *registry.Info) (string, error) {
}
return fmt.Sprintf("%s:%s:%d", info.ServiceName, host, port), nil
}

// convTagMapToSlice Tags map be convert to slice.
// Keys must not contain `:`.
func convTagMapToSlice(tagMap map[string]string) ([]string, error) {
svcTags := make([]string, 0, len(tagMap))
for k, v := range tagMap {
var tag string
if strings.Contains(k, kvJoinChar) {
return svcTags, errIllegalTagChar
}
if v == "" {
tag = k
} else {
tag = fmt.Sprintf("%s%s%s", k, kvJoinChar, v)
}
svcTags = append(svcTags, tag)
}
return svcTags, nil
}

// splitTags Tags characters be separated to map.
func splitTags(tags []string) map[string]string {
n := len(tags)
tagMap := make(map[string]string, n)
if n == 0 {
return tagMap
}

for _, tag := range tags {
if tag == "" {
continue
}
strArr := strings.SplitN(tag, kvJoinChar, 2)
if len(strArr) == 2 {
key := strArr[0]
tagMap[key] = strArr[1]
}
if len(strArr) == 1 {
tagMap[strArr[0]] = ""
}
}

return tagMap
}

func inArray(needle string, haystack []string) bool {
for _, k := range haystack {
if needle == k {
return true
}
}
return false
}
Loading