fix the consul setup code

This commit is contained in:
Asim Aslam 2019-08-06 19:43:46 +01:00
parent eae32176c4
commit edb0fe4b16

View File

@ -97,24 +97,33 @@ func configure(c *consulRegistry, opts ...registry.Option) {
} }
// check if there are any addrs // check if there are any addrs
if len(c.opts.Addrs) > 0 { var addrs []string
addr, port, err := net.SplitHostPort(c.opts.Addrs[0])
// iterate the options addresses
for _, address := range c.opts.Addrs {
// check we have a port
addr, port, err := net.SplitHostPort(address)
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
port = "8500" port = "8500"
addr = c.opts.Addrs[0] addr = address
config.Address = fmt.Sprintf("%s:%s", addr, port) addrs = append(addrs, fmt.Sprintf("%s:%s", addr, port))
} else if err == nil { } else if err == nil {
config.Address = fmt.Sprintf("%s:%s", addr, port) addrs = append(addrs, fmt.Sprintf("%s:%s", addr, port))
} }
} }
// set the addrs
if len(addrs) > 0 {
c.Address = addrs
config.Address = c.Address[0]
}
if config.HttpClient == nil { if config.HttpClient == nil {
config.HttpClient = new(http.Client) config.HttpClient = new(http.Client)
} }
// requires secure connection? // requires secure connection?
if c.opts.Secure || c.opts.TLSConfig != nil { if c.opts.Secure || c.opts.TLSConfig != nil {
config.Scheme = "https" config.Scheme = "https"
// We're going to support InsecureSkipVerify // We're going to support InsecureSkipVerify
config.HttpClient.Transport = newTransport(c.opts.TLSConfig) config.HttpClient.Transport = newTransport(c.opts.TLSConfig)
@ -125,11 +134,13 @@ func configure(c *consulRegistry, opts ...registry.Option) {
config.HttpClient.Timeout = c.opts.Timeout config.HttpClient.Timeout = c.opts.Timeout
} }
// set address // set the config
c.Address = c.opts.Addrs
c.config = config c.config = config
// remove client
c.client = nil
// setup the client
c.Client() c.Client()
} }
@ -384,20 +395,28 @@ func (c *consulRegistry) Client() *consul.Client {
return c.client return c.client
} }
if len(c.Address) == 0 { for _, addr := range c.Address {
tmp, _ := consul.NewClient(c.config) // set the address
return tmp c.config.Address = addr
}
c.config.Address = c.Address[0] // create a new client
tmpClint, _ := consul.NewClient(c.config) tmpClient, _ := consul.NewClient(c.config)
_, err := tmpClint.Agent().Host()
// test the client
_, err := tmpClient.Agent().Host()
if err != nil { if err != nil {
c.Address = c.Address[1:] continue
return c.Client()
} }
c.client = tmpClint // set the client
c.client = tmpClient
return c.client
}
// set the default
c.client, _ = consul.NewClient(c.config)
// return the client
return c.client return c.client
} }