ExConsulUrl

Simple client to retrieve the IP and port that a service is running on from HashiCorp’s Consul.

Installation

If available in Hex, the package can be installed as:

  1. Add ex_consul_url to your list of dependencies in mix.exs:
def deps do
  [{:ex_consul_url, "~> 0.1.0"}]
end
  1. Ensure ex_consul_url is started before your application:
def application do
  [applications: [:ex_consul_url]]
end

Usage

Given a service and it’s Consul tags, retrieve the IP and port it runs on. For example:

iex> ExConsulUrl.url_for("consul://production.my-service")
"http://127.0.0.1:8080"

iex> ExConsulUrl.url_for("my-service", "production")
"127.0.0.1:8080"

iex> ExConsulUrl.url_for!("service-not-there", "production")
# Throws ServiceNotFound error

Note: If the service and tags map to multiple IP/port pairs, the first one is always chosen.