diff --git a/cmd/cmd.go b/cmd/cmd.go index 811d16f0..2665126e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -55,6 +55,11 @@ var ( Value: ":0", Usage: "Bind address for the server. 127.0.0.1:8080", }, + cli.StringFlag{ + Name: "server_advertise", + EnvVar: "MICRO_SERVER_ADVERTISE", + Usage: "Used instead of the server_address when registering with discovery. 127.0.0.1:8080", + }, cli.StringSliceFlag{ Name: "server_metadata", EnvVar: "MICRO_SERVER_METADATA", @@ -186,6 +191,7 @@ func Setup(c *cli.Context) error { server.Version(c.String("server_version")), server.Id(c.String("server_id")), server.Address(c.String("server_address")), + server.Advertise(c.String("server_advertise")), server.Metadata(metadata), ) diff --git a/server/options.go b/server/options.go index 633c28be..214d7733 100644 --- a/server/options.go +++ b/server/options.go @@ -13,6 +13,7 @@ type options struct { metadata map[string]string name string address string + advertise string id string version string } @@ -71,6 +72,10 @@ func (o options) Address() string { return o.address } +func (o options) Advertise() string { + return o.advertise +} + func (o options) Metadata() map[string]string { return o.metadata } @@ -99,6 +104,12 @@ func Address(a string) Option { } } +func Advertise(a string) Option { + return func(o *options) { + o.advertise = a + } +} + func Broker(b broker.Broker) Option { return func(o *options) { o.broker = b diff --git a/server/rpc_server.go b/server/rpc_server.go index 1f15cd8a..f99a948b 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -114,10 +114,19 @@ func (s *rpcServer) Subscribe(sb Subscriber) error { func (s *rpcServer) Register() error { // parse address for host, port config := s.Config() - var host string + var advt, host string var port int - parts := strings.Split(config.Address(), ":") + // check the advertise address first + // if it exists then use it, otherwise + // use the address + if len(config.Advertise()) > 0 { + advt = config.Advertise() + } else { + advt = config.Address() + } + + parts := strings.Split(advt, ":") if len(parts) > 1 { host = strings.Join(parts[:len(parts)-1], ":") port, _ = strconv.Atoi(parts[len(parts)-1]) @@ -177,9 +186,19 @@ func (s *rpcServer) Register() error { func (s *rpcServer) Deregister() error { config := s.Config() - var host string + var advt, host string var port int - parts := strings.Split(config.Address(), ":") + + // check the advertise address first + // if it exists then use it, otherwise + // use the address + if len(config.Advertise()) > 0 { + advt = config.Advertise() + } else { + advt = config.Address() + } + + parts := strings.Split(advt, ":") if len(parts) > 1 { host = strings.Join(parts[:len(parts)-1], ":") port, _ = strconv.Atoi(parts[len(parts)-1])