Skip to content

Commit

Permalink
feature external network
Browse files Browse the repository at this point in the history
  • Loading branch information
David_zdw committed May 22, 2024
1 parent c295221 commit 5430ba6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 40 deletions.
1 change: 1 addition & 0 deletions cluster/gate/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func (g *Gate) registerServiceInstance() {
Alias: g.opts.name,
State: cluster.Work,
Endpoint: g.rpc.Endpoint().String(),
MetaMap: g.opts.registry.GetMetaMap(),
}
if g.opts.promServer.Enable() {
metricsPort, err := strconv.Atoi(g.opts.promServer.GetMetricsPort())
Expand Down
9 changes: 9 additions & 0 deletions registry/consul/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type options struct {
metricsEnable bool

failuresBeforeCritical int

metaMap map[string]string
}

func defaultOptions() *options {
Expand Down Expand Up @@ -134,3 +136,10 @@ func WithHeartbeatCheckInterval(interval int) Option {
func WithDeregisterCriticalServiceAfter(after int) Option {
return func(o *options) { o.deregisterCriticalServiceAfter = after }
}

// WithMetaMap 设置metaMap
func WithMetaMap(metaMap map[string]string) Option {
return func(o *options) {
o.metaMap = metaMap
}
}
42 changes: 2 additions & 40 deletions registry/consul/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"context"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/symsimmy/due/config"
"github.com/symsimmy/due/encoding/json"
"github.com/symsimmy/due/env"
"github.com/symsimmy/due/log"
"github.com/symsimmy/due/registry"
"net"
"net/url"
"os"
"strconv"
"time"
)
Expand All @@ -22,25 +20,11 @@ const (
metaFieldKind = "kind"
metaFieldAlias = "alias"
metaFieldState = "state"
metaFieldWanIp = "wan"
metaFieldRegion = "region"
metaFieldWsPort = "wsPort"
metaFieldTcpPort = "tcpPort"
)

const (
routeKvFormat = "route/%v/%v"
hostIpEnvName = "HOST_IP"
wanIpEnvName = "WAN_IP"
regionEnvName = "REGION"
)

const (
wsPortConfigName = "config.network.ws.server.addr"
tcpPortConfigName = "config.network.tcp.server.addr"

wanIpConfigName = "config.wan.ip"
regionConfigName = "config.wan.region"
)

type registrar struct {
Expand Down Expand Up @@ -80,20 +64,7 @@ func (r *registrar) register(ctx context.Context, ins *registry.ServiceInstance)
return err
}

wanIpConfig := config.Get(wanIpConfigName).String()
regionConfig := config.Get(regionConfigName).String()
if len(wanIpConfig) != 0 {
os.Setenv(wanIpEnvName, wanIpConfig)
}
if len(regionConfig) != 0 {
os.Setenv(regionEnvName, regionConfig)
}

overwriteHost := env.Get(hostIpEnvName, host).String()
wanIp := env.Get(wanIpEnvName).String()
region := env.Get(regionEnvName).String()
wsPort := config.Get(wsPortConfigName).String()
tcpPort := config.Get(tcpPortConfigName).String()

registration := &api.AgentServiceRegistration{
ID: ins.ID,
Expand All @@ -111,17 +82,8 @@ func (r *registrar) register(ctx context.Context, ins *registry.ServiceInstance)
registration.Meta[metaFieldKind] = string(ins.Kind)
registration.Meta[metaFieldAlias] = ins.Alias
registration.Meta[metaFieldState] = string(ins.State)
if wanIp != "" {
registration.Meta[metaFieldWanIp] = wanIp
}
if region != "" {
registration.Meta[metaFieldRegion] = region
}
if len(wsPort) != 0 {
registration.Meta[metaFieldWsPort] = wsPort
}
if len(tcpPort) != 0 {
registration.Meta[metaFieldTcpPort] = tcpPort
for key, value := range ins.MetaMap {
registration.Meta[key] = value
}

for _, event := range ins.Events {
Expand Down
4 changes: 4 additions & 0 deletions registry/consul/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewRegistry(opts ...Option) *Registry {
return r
}

func (r *Registry) GetMetaMap() map[string]string {
return r.opts.metaMap
}

// Register 注册服务实例
func (r *Registry) Register(ctx context.Context, ins *registry.ServiceInstance) error {
if r.err != nil {
Expand Down
2 changes: 2 additions & 0 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

type Registry interface {
// GetMetaMap 获取meta
GetMetaMap() map[string]string
// Register 注册服务实例
Register(ctx context.Context, ins *ServiceInstance) error
// Deregister 解注册服务实例
Expand Down

0 comments on commit 5430ba6

Please sign in to comment.