|
| 1 | +// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | +// info@rabbitmq.com. |
| 15 | + |
| 16 | +package com.rabbitmq.client; |
| 17 | + |
| 18 | +import javax.naming.NamingEnumeration; |
| 19 | +import javax.naming.NamingException; |
| 20 | +import javax.naming.directory.Attributes; |
| 21 | +import javax.naming.directory.DirContext; |
| 22 | +import javax.naming.directory.InitialDirContext; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Hashtable; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +/** |
| 30 | + * {@link AddressResolver} that resolves addresses against a DNS SRV request. |
| 31 | + * SRV records contain the hostname and the port for a given service. |
| 32 | + * They also support priorities, to give precedence to a given host |
| 33 | + * over other hosts. |
| 34 | + * Note the hosts returned by the SRV query must be resolvable by |
| 35 | + * the DNS servers of the underlying platform (or the default ones |
| 36 | + * specified for this Java process). This class does not issue a |
| 37 | + * query for A records after the SRV query. |
| 38 | + * This implementation returns the highest-priority records first. |
| 39 | + * This behavior can be changed by overriding the {@code sort} method. |
| 40 | + * |
| 41 | + * This implementation uses internally the {@code com.sun.jndi.dns.DnsContextFactory} |
| 42 | + * class for the DNS query. |
| 43 | + * |
| 44 | + * The first returned address is used when automatic recovery is NOT enabled |
| 45 | + * at the {@link ConnectionFactory} level. |
| 46 | + * When automatic recovery is enabled, a random address will be picked up |
| 47 | + * from the returned list of {@link Address}es. |
| 48 | + * |
| 49 | + */ |
| 50 | +public class DnsSrvRecordAddressResolver implements AddressResolver { |
| 51 | + |
| 52 | + /** |
| 53 | + * the SRV service information. |
| 54 | + * e.g. _sip._tcp.example.com or rabbitmq.service.consul |
| 55 | + */ |
| 56 | + private final String service; |
| 57 | + |
| 58 | + /** |
| 59 | + * URLs of the DNS servers. |
| 60 | + * e.g. dns://server1.example.com/example.com |
| 61 | + * Default to {@code dns:}, that is the DNS server of the |
| 62 | + * underlying platform. |
| 63 | + */ |
| 64 | + private final String dnsUrls; |
| 65 | + |
| 66 | + public DnsSrvRecordAddressResolver(String service) { |
| 67 | + this(service, "dns:"); |
| 68 | + } |
| 69 | + |
| 70 | + public DnsSrvRecordAddressResolver(String service, String dnsUrls) { |
| 71 | + this.service = service; |
| 72 | + this.dnsUrls = dnsUrls; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public List<Address> getAddresses() throws IOException { |
| 77 | + List<SrvRecord> records = lookupSrvRecords(service, dnsUrls); |
| 78 | + records = sort(records); |
| 79 | + |
| 80 | + List<Address> addresses = new ArrayList<Address>(); |
| 81 | + for (SrvRecord record : records) { |
| 82 | + addresses.add(new Address(record.getHost(), record.getPort())); |
| 83 | + } |
| 84 | + |
| 85 | + return addresses; |
| 86 | + } |
| 87 | + |
| 88 | + protected List<SrvRecord> lookupSrvRecords(String service, String dnsUrls) throws IOException { |
| 89 | + Hashtable<String, String> env = new Hashtable<String, String>(); |
| 90 | + env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); |
| 91 | + env.put("java.naming.provider.url", dnsUrls); |
| 92 | + |
| 93 | + List<SrvRecord> records = new ArrayList<SrvRecord>(); |
| 94 | + try { |
| 95 | + DirContext ctx = new InitialDirContext(env); |
| 96 | + Attributes attributes = ctx.getAttributes(service, new String[] { "SRV" }); |
| 97 | + NamingEnumeration<?> servers = attributes.get("srv").getAll(); |
| 98 | + while (servers.hasMore()) { |
| 99 | + records.add(mapSrvRecord((String) servers.next())); |
| 100 | + } |
| 101 | + } catch(NamingException e) { |
| 102 | + throw new IOException("Error during DNS SRV query", e); |
| 103 | + } |
| 104 | + |
| 105 | + return records; |
| 106 | + } |
| 107 | + |
| 108 | + protected SrvRecord mapSrvRecord(String srvResult) { |
| 109 | + return SrvRecord.fromSrvQueryResult(srvResult); |
| 110 | + } |
| 111 | + |
| 112 | + protected List<SrvRecord> sort(List<SrvRecord> records) { |
| 113 | + Collections.sort(records); |
| 114 | + return records; |
| 115 | + } |
| 116 | + |
| 117 | + public static class SrvRecord implements Comparable<SrvRecord> { |
| 118 | + |
| 119 | + private final int priority; |
| 120 | + private final int weight; |
| 121 | + private final int port; |
| 122 | + private final String host; |
| 123 | + |
| 124 | + public SrvRecord(int priority, int weight, int port, String host) { |
| 125 | + this.priority = priority; |
| 126 | + this.weight = weight; |
| 127 | + this.port = port; |
| 128 | + int lastDotIndex = host.lastIndexOf("."); |
| 129 | + if(lastDotIndex > 0) { |
| 130 | + this.host = host.substring(0, lastDotIndex); |
| 131 | + } else { |
| 132 | + this.host = host; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public int getPriority() { |
| 137 | + return priority; |
| 138 | + } |
| 139 | + |
| 140 | + public int getWeight() { |
| 141 | + return weight; |
| 142 | + } |
| 143 | + |
| 144 | + public int getPort() { |
| 145 | + return port; |
| 146 | + } |
| 147 | + |
| 148 | + public String getHost() { |
| 149 | + return host; |
| 150 | + } |
| 151 | + |
| 152 | + public static SrvRecord fromSrvQueryResult(String srvResult) { |
| 153 | + String[] fields = srvResult.split(" "); |
| 154 | + return new SrvRecord( |
| 155 | + Integer.parseInt(fields[0]), |
| 156 | + Integer.parseInt(fields[1]), |
| 157 | + Integer.parseInt(fields[2]), |
| 158 | + fields[3] |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public int compareTo(SrvRecord o) { |
| 164 | + return (this.priority < o.getPriority()) ? -1 : ((this.priority == o.getPriority()) ? 0 : 1); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments