36755

I need to use multiple LDAP provider. How can I check LDAP server availability?

<h3>Question</h3>

We have multiple LDAP/domain servers.(ex. LDAP://server1.com:389/DC=server1,DC=COM, LDAP://server2.com:389/DC=server2,DC=COM) I need to use one of them by checking availabilty.

try { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "LDAP://server1.com:389/DC=server1,DC=COM"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, username); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = new InitialDirContext(env); } catch(NamingException ex) { }
<h3>Answer1:</h3>

You can perform a simple anonymous search to see if the LDAP server is up and running. If you get a Connection refused exception, then the server would be down and you can switch to the next one in your list, perform the same operation again.

Usually there would be load balancers sitting in front of a string of domain controllers, the requests are routed to the load balancer which would identify the right DC and proxies the request to it. The LB takes care of periodically checking the availability of DCs in its list, remove them if they went down, check them back again when they are on etc.,. You may want to check with your IT department about existence of such an LB in your environment.


<h3>Answer2:</h3>

You can just use multiple ldap server URLs in the PROVIDER_URL environment property like this:

Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // Specify list of space-separated URLs env.put(Context.PROVIDER_URL, "ldap://notthere:389/o=JNDITutorial " + "ldap://localhost:389/o=JNDITutorial " + "ldap://remotehost/o=JNDITutorial " + "ldap://thirdhost:389/o=JNDITutorial"); // Create initial context DirContext ctx = new InitialDirContext(env); // See which server was used System.out.println(ctx.getEnvironment().get(Context.PROVIDER_URL)); // do something useful with ctx ....

Whichever URL is successful, that will be used in the context

来源:https://stackoverflow.com/questions/14459280/i-need-to-use-multiple-ldap-provider-how-can-i-check-ldap-server-availability

Recommend