Skip to content

Commit

Permalink
fix(consul): use registry info tag (#101)
Browse files Browse the repository at this point in the history
Signed-off-by: rogerogers <rogers@rogerogers.com>
  • Loading branch information
rogerogers authored Jan 13, 2024
1 parent de04e4f commit ead1ccd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
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,
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
}

0 comments on commit ead1ccd

Please sign in to comment.