diff --git a/.changelog/16217.txt b/.changelog/16217.txt new file mode 100644 index 000000000..0ebbc293b --- /dev/null +++ b/.changelog/16217.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Fixed a bug where clients used the serf advertise address to connect to servers when using Consul auto-discovery +``` diff --git a/client/client.go b/client/client.go index da1390501..45473875f 100644 --- a/client/client.go +++ b/client/client.go @@ -2944,23 +2944,29 @@ DISCOLOOP: mErr.Errors = append(mErr.Errors, err) continue } - var peers []string - if err := c.connPool.RPC(region, addr, "Status.Peers", rpcargs, &peers); err != nil { + + // Query the members from the region that Consul gave us, and + // extract the client-advertise RPC address from each member + var membersResp structs.ServerMembersResponse + if err := c.connPool.RPC(region, addr, "Status.Members", rpcargs, &membersResp); err != nil { mErr.Errors = append(mErr.Errors, err) continue } - - // Successfully received the Server peers list of the correct - // region - for _, p := range peers { - addr, err := net.ResolveTCPAddr("tcp", p) - if err != nil { - mErr.Errors = append(mErr.Errors, err) - continue + for _, member := range membersResp.Members { + if addrTag, ok := member.Tags["rpc_addr"]; ok { + if portTag, ok := member.Tags["port"]; ok { + addr, err := net.ResolveTCPAddr("tcp", + fmt.Sprintf("%s:%s", addrTag, portTag)) + if err != nil { + mErr.Errors = append(mErr.Errors, err) + continue + } + srv := &servers.Server{Addr: addr} + nomadServers = append(nomadServers, srv) + } } - srv := &servers.Server{Addr: addr} - nomadServers = append(nomadServers, srv) } + if len(nomadServers) > 0 { break DISCOLOOP }