Marco Pietro Cirillo
Marco Pietro Cirillo

Reputation: 864

Load Balancing LDAP Connection in Spring

I have two LDAP servers that I wish to use in a round-robin fashion. This code as-written seems to always choose the first server. How can I get it to choose evenly?

private static LdapTemplate createLdapTemplate(String[] urls, String username, String password) {

    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrls(urls);
    contextSource.setBase(LDAP_SEARCH_BASE);

    // username is the same for both LDAP servers
    contextSource.setUserDn(username);
    contextSource.setPassword(password);

    contextSource.setPooled(true);
    contextSource.afterPropertiesSet();

    return new LdapTemplate(contextSource);
}

and I use the LDAP template like so:

SearchControls searchControls = new SearchControls();
searchControls.setTimeLimit(5000);

List ldapResultList = ldapTemplate.search("", filter.encode(), searchControls, (ContextMapper) o -> {
    // do things with result...
});

Upvotes: 0

Views: 1557

Answers (1)

Kraal
Kraal

Reputation: 2877

Check spring-ldap reference documentation:

If fail-over functionality is desired, more than one URL can be specified

Therefor you won't be able to achieve load balancing this way.

If you want to load balance between different LDAP server, what you should do instead, is to use a single LDAP server URL pointing to a loadbalancer (such as HaProxy) placed in front of your LDAP servers and use it with mode tcp, and balance roundrobin.

Upvotes: 1

Related Questions