bitbucket.org/streamingbolaget/trackdown
Trackdown #
Package trackdown provides an API for Consul service discovery. * Easy service discovery * Optionally returns only healthy services * Simple API & configuration * Does not provide load balancing
Example
~~~ package main import ( “bitbucket.org/streamingbolaget/trackdown” “fmt” ) var ( service *trackdown.Resource pool *trackdown.ServiceManager err error ) func main() { // Track down healthy snowflake services. if pool, err = trackdown.Service(“snowflake”, trackdown.Healthy); err != nil { panic(err) } // Get the first service from the pool. if service, err = pool.Current(); err != nil { panic(err) } for i := 0; i < 10; i++ { // Request id from snowflake. resp, err := http.Get(fmt.Sprintf(“http://%s:%d”, service.Addr, service.Port)) if err != nil { // Get the next service from the pool. This blocks until we get a // new service or a timeout happens. if service, err = pool.Next(); err != nil { fmt.Println(“pool error:”, err) return } continue } defer resp.Body.Close() b, _ := ioutil.ReadAll(resp.Body) fmt.Printf(“id: %s\n”, b) time.Sleep(time.Second * 5) } } ~~~
Options
Trackdown uses option functions. It will fall back to sensible defaults if no options are provided.
Healthy
trackdown.Healthy
Healthy only returns services where the health check associated with the service report a status of “passing”. This ignores the serfHealth check and wont return a service even if the serfHealth check reports “passing” as its status.
Timeout
trackdown.Timeout(timeout string)
Timeout overrides the deafult timeout of 10m. Expects a string value in the format “15s” – Sets a Timeout of 15 seconds.
Tag
trackdown.Tag(tag string)
Filter the returned services after the supplied tag.
Datacenter
trackdown.Datacenter(datacenter string)