driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java
@Override
    public List<String> resolveHostFromSrvRecords(final String srvHost) {
        String srvHostDomain = srvHost.substring(srvHost.indexOf('.') + 1);
        List<String> srvHostDomainParts = asList(srvHostDomain.split("\\."));
        List<String> hosts = new ArrayList<String>();
        InitialDirContext dirContext = createDnsDirContext();
        try {
            Attributes attributes = dirContext.getAttributes("_mongodb._tcp." + srvHost, new String[]{"SRV"});
            Attribute attribute = attributes.get("SRV");
            if (attribute == null) {
                throw new MongoConfigurationException("No SRV records available for host " + srvHost);
            }
            NamingEnumeration<?> srvRecordEnumeration = attribute.getAll();
            while (srvRecordEnumeration.hasMore()) {
                String srvRecord = (String) srvRecordEnumeration.next();
                String[] split = srvRecord.split(" ");
                String resolvedHost = split[3].endsWith(".") ? split[3].substring(0, split[3].length() - 1) : split[3];
                String resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1);
                if (!sameParentDomain(srvHostDomainParts, resolvedHostDomain)) {
                    throw new MongoConfigurationException(
                            format("The SRV host name '%s'resolved to a host '%s 'that is not in a sub-domain of the SRV host.",
                                    srvHost, resolvedHost));
                }
                hosts.add(resolvedHost + ":" + split[2]);
            }

            if (hosts.isEmpty()) {
                throw new MongoConfigurationException("Unable to find any SRV records for host " + srvHost);
            }
        } catch (NamingException e) {
            throw new MongoConfigurationException("Unable to look up SRV record for host " + srvHost, e);
        } finally {
            try {
                dirContext.close();
            } catch (NamingException e) {
                // ignore
            }
        }
        return hosts;
    }
@Override
    public List<String> resolveHostFromSrvRecords(final String srvHost) {
        String srvHostDomain = srvHost.substring(srvHost.indexOf('.') + 1);
        List<String> srvHostDomainParts = asList(srvHostDomain.split("\\."));
        List<String> hosts = new ArrayList<String>();
        InitialDirContext dirContext = createDnsDirContext();
        try {
            Attributes attributes = dirContext.getAttributes("_mongodb._tcp." + srvHost, new String[]{"SRV"});
            Attribute attribute = attributes.get("SRV");
            if (attribute == null) {
                throw new MongoConfigurationException("No SRV records available for host " + srvHost);
            }
            NamingEnumeration<?> srvRecordEnumeration = attribute.getAll();
            while (srvRecordEnumeration.hasMore()) {
                String srvRecord = (String) srvRecordEnumeration.next();
                String[] split = srvRecord.split(" ");
                String resolvedHost = split[3].endsWith(".") ? split[3].substring(0, split[3].length() - 1) : split[3];
                String resolvedHostDomain = resolvedHost.substring(resolvedHost.indexOf('.') + 1);
                if (!sameParentDomain(srvHostDomainParts, resolvedHostDomain)) {
                    throw new MongoConfigurationException(
                            format("The SRV host name '%s'resolved to a host '%s 'that is not in a sub-domain of the SRV host.",
                                    srvHost, resolvedHost));
                }
                hosts.add(resolvedHost + ":" + split[2]);
            }

            if (hosts.isEmpty()) {
                throw new MongoConfigurationException("Unable to find any SRV records for host " + srvHost);
            }
        } catch (NamingException e) {
            throw new MongoConfigurationException("Unable to look up SRV record for host " + srvHost, e);
        } finally {
            try {
                dirContext.close();
            } catch (NamingException e) {
                // ignore
            }
        }
        return hosts;
    }
@ Override public List < String > resolveHostFromSrvRecords ( final String srvHost ) { String srvHostDomain = srvHost . substring ( srvHost . indexOf ( ' ' ) + 1 ) ; List < String > srvHostDomainParts = asList ( srvHostDomain . split ( "\\." ) ) ; List < String > hosts = new ArrayList < String > ( ) ; InitialDirContext dirContext = createDnsDirContext ( ) ; try { Attributes attributes = dirContext . getAttributes ( "_mongodb._tcp." + srvHost , new String [ ] { "SRV" } ) ; Attribute attribute = attributes . get ( "SRV" ) ; if ( attribute == null ) { throw new MongoConfigurationException ( "No SRV records available for host " + srvHost ) ; } NamingEnumeration < ? > srvRecordEnumeration = attribute . getAll ( ) ; while ( srvRecordEnumeration . hasMore ( ) ) { String srvRecord = ( String ) srvRecordEnumeration . next ( ) ; String [ ] split = srvRecord . split ( " " ) ; String resolvedHost = split [ 3 ] . endsWith ( "." ) ? split [ 3 ] . substring ( 0 , split [ 3 ] . length ( ) - 1 ) : split [ 3 ] ; String resolvedHostDomain = resolvedHost . substring ( resolvedHost . indexOf ( ' ' ) + 1 ) ; if ( ! sameParentDomain ( srvHostDomainParts , resolvedHostDomain ) ) { throw new MongoConfigurationException ( format ( "The SRV host name '%s'resolved to a host '%s 'that is not in a sub-domain of the SRV host." , srvHost , resolvedHost ) ) ; } hosts . add ( resolvedHost + ":" + split [ 2 ] ) ; } if ( hosts . isEmpty ( ) ) { throw new MongoConfigurationException ( "Unable to find any SRV records for host " + srvHost ) ; } } catch ( NamingException e ) { throw new MongoConfigurationException ( "Unable to look up SRV record for host " + srvHost , e ) ; } finally { try { dirContext . close ( ) ; } catch ( NamingException e ) { // ignore } } return hosts ; }
/*
The format of SRV record is
priority weight port target.
e.g.
0 5 5060 example.com.

The priority and weight are ignored, and we just concatenate the host (after removing the ending '.') and port with a
':' in between, as expected by ServerAddress.

It's required that the srvHost has at least three parts (e.g. foo.bar.baz) and that all of the resolved hosts have a parent
domain equal to the domain of the srvHost.
/ * The format of SRV record is priority weight port target . e . g . 0 5 5060 example . com .