12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package internal
- import (
- "os"
- "strings"
- "git.i2edu.net/i2/go-zero/core/discov"
- "git.i2edu.net/i2/go-zero/core/netx"
- )
- const (
- allEths = "0.0.0.0"
- envPodIp = "POD_IP"
- )
- // NewRpcPubServer returns a Server.
- func NewRpcPubServer(etcdEndpoints []string, etcdKey, listenOn string, opts ...ServerOption) (Server, error) {
- registerEtcd := func() error {
- pubListenOn := figureOutListenOn(listenOn)
- pubClient := discov.NewPublisher(etcdEndpoints, etcdKey, pubListenOn)
- return pubClient.KeepAlive()
- }
- server := keepAliveServer{
- registerEtcd: registerEtcd,
- Server: NewRpcServer(listenOn, opts...),
- }
- return server, nil
- }
- type keepAliveServer struct {
- registerEtcd func() error
- Server
- }
- func (ags keepAliveServer) Start(fn RegisterFn) error {
- if err := ags.registerEtcd(); err != nil {
- return err
- }
- 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:]...), ":")
- }
|