Module: Couchbase::DNS

Defined in:
lib/couchbase/dns.rb

Class Method Summary collapse

Class Method Details

.locate(name, bootstrap_protocol = :http) ⇒ Object

Note:

This is experimental interface. It might change in future (e.g. service identifiers)

Locate bootstrap nodes from a DNS SRV record.

The DNS SRV records need to be configured on a reachable DNS server. An example configuration could look like the following:

_cbmcd._tcp.example.com.  0  IN  SRV  20  0  11210 node2.example.com.
_cbmcd._tcp.example.com.  0  IN  SRV  10  0  11210 node1.example.com.
_cbmcd._tcp.example.com.  0  IN  SRV  30  0  11210 node3.example.com.

_cbhttp._tcp.example.com.  0  IN  SRV  20  0  8091 node2.example.com.
_cbhttp._tcp.example.com.  0  IN  SRV  10  0  8091 node1.example.com.
_cbhttp._tcp.example.com.  0  IN  SRV  30  0  8091 node3.example.com.

Now if “example.com” is passed in as the argument, the three nodes configured will be parsed and put in the returned URI list. Note that the priority is respected (in this example, node1 will be the first one in the list, followed by node2 and node3). As of now, weighting is not supported.

Examples:

Initialize connection using DNS SRV records

nodes = Couchbase::DNS.locate('example.com', :http)
if nodes.empty?
  nodes = ["example.com:8091"]
end
Couchbase.connect(:node_list => nodes)

Parameters:

  • name

    the DNS name where SRV records configured

  • bootstrap_protocol (Symbol) (defaults to: :http)

    the desired protocol for bootstrapping. See bootstrap_protocols option to Bucket#new. Allowed values :http, :cccp

Returns:

  • a list of ordered boostrap URIs by their weight.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/couchbase/dns.rb', line 57

def locate(name, bootstrap_protocol = :http)
  service = case bootstrap_protocol
            when :http
              "_cbhttp"
            when :cccp
              "_cbmcd"
            else
              raise ArgumentError, "unknown bootstrap protocol: #{bootstrap_transports}"
            end
  hosts = []
  Resolv::DNS.open do |dns|
    resources = dns.getresources("#{service}._tcp.#{name}", Resolv::DNS::Resource::IN::SRV)
    hosts = resources.sort_by(&:priority).map { |res| "#{res.target}:#{res.port}" }
  end
  hosts
end