consul

package module
v0.0.0-...-4b260bf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2017 License: MIT Imports: 8 Imported by: 1

README

consul

TL;DR

Fetch IP:port of a service from the local Consul datacentre. Load balancing is performed if there is more than one service running under the same name.

Description

Package consul currently contains a single function (ServiceHostPort) which takes a service name and uses Consul's DNS server to fetch its SRV record if one exists. An SRV record contains both IP address and port of the service.

If there is more than one service registered with the same name, the response for such DNS request will contain multiple IP:port pairs and one will be chosen at random.

Installation

consul can be imported with your vendoring tool:

go get github.com/fresh8/consul

Once imported into your project start referencing it by adding the line

import "github.com/fresh8/consul"

Examples

addr, err := consul.ServiceHostPort("redis")
if err == nil {
  fmt.Printf("Redis is at %s", addr) // → Redis is at 172.16.0.1:32280
}

Tests

In the package root directory run go test to run all tests.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoCatalog is returned when there is no catalog in Consul client
	ErrNoCatalog = errors.New("no Consul catalog in client")

	// ErrNoService is returned when there are no services registered for that
	// service name and tag
	ErrNoService = errors.New("no service registered under this service name and tag")

	// Consul client
	Client        *api.Client
	ConsulCatalog Catalog
)

Functions

func ServiceHostPort

func ServiceHostPort(service string) (string, error)

ServiceHostPort looks up a service by just service name or tag.service from local Consul agent

func Setup

func Setup() error

func TagServiceHostPort

func TagServiceHostPort(service, tag string) (hostPort string, err error)

TagServiceHostPort looks up a service by service name and tag from local Consul agent

func TagServiceHostPortMulti

func TagServiceHostPortMulti(service, tag string) (hostPort []string, err error)

TagServiceHostPortMulti looks up a service by service name and tag from local Consul agent

Types

type Catalog

Catalog is an interface generated for "github.com/hashicorp/consul/api".Catalog.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL