简体   繁体   English

在Spring中进行负载平衡LDAP连接

[英]Load Balancing LDAP Connection in Spring

I have two LDAP servers that I wish to use in a round-robin fashion. 我有两个希望以循环方式使用的LDAP服务器。 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: 我像这样使用LDAP模板:

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

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

Check spring-ldap reference documentation : 查看spring-ldap参考文档

If fail-over functionality is desired, more than one URL can be specified 如果需要故障转移功能,则可以指定多个URL

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 . 如果要在不同的LDAP服务器之间进行负载平衡,则应该使用指向位于LDAP服务器前面的负载均衡器(例如HaProxy )的单个LDAP服务器URL,并将其与mode tcp一起使用,然后使用balance roundrobin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM