@@ -1,10 +1,22 @@
|
||||
package internal
|
||||
|
||||
import "github.com/tal-tech/go-zero/core/discov"
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/discov"
|
||||
"github.com/tal-tech/go-zero/core/netx"
|
||||
)
|
||||
|
||||
const (
|
||||
allEths = "0.0.0.0"
|
||||
envPodIp = "POD_IP"
|
||||
)
|
||||
|
||||
func NewRpcPubServer(etcdEndpoints []string, etcdKey, listenOn string, opts ...ServerOption) (Server, error) {
|
||||
registerEtcd := func() error {
|
||||
pubClient := discov.NewPublisher(etcdEndpoints, etcdKey, listenOn)
|
||||
pubListenOn := figureOutListenOn(listenOn)
|
||||
pubClient := discov.NewPublisher(etcdEndpoints, etcdKey, pubListenOn)
|
||||
return pubClient.KeepAlive()
|
||||
}
|
||||
server := keepAliveServer{
|
||||
@@ -27,3 +39,25 @@ func (ags keepAliveServer) Start(fn RegisterFn) error {
|
||||
|
||||
return ags.Server.Start(fn)
|
||||
}
|
||||
|
||||
func figureOutListenOn(listenOn string) string {
|
||||
fields := strings.Split(listenOn, ":")
|
||||
if len(fields) == 0 {
|
||||
return listenOn
|
||||
}
|
||||
|
||||
host := fields[0]
|
||||
if len(host) > 0 && host != allEths {
|
||||
return listenOn
|
||||
}
|
||||
|
||||
ip := os.Getenv(envPodIp)
|
||||
if len(ip) == 0 {
|
||||
ip = netx.InternalIp()
|
||||
}
|
||||
if len(ip) == 0 {
|
||||
return listenOn
|
||||
}
|
||||
|
||||
return strings.Join(append([]string{ip}, fields[1:]...), ":")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user