合并的LdapTemplate在上下文验证期间停顿了几分钟

时间:2018-09-20 22:01:55

标签: grails spring-ldap

我正在尝试在Rest呼叫服务实现期间使用Spring-ldap的LdapTemplate从LDAP源中检索信息,尽管我认为我的配置有效,但是当出现以下情况时,我们会间歇地停顿15分钟服务被击中。日志记录语句确定停滞发生在ldapTemplate.search()调用期间。

我的豆子:

    contextSourceTarget(org.springframework.ldap.core.support.LdapContextSource) {
        urls = ["https://someldapsource.com"]
        userDn = 'uid=someaccount,ou=xxx,cn=users,dc=org,dc=com'
        password = 'somepassword'
        pooled = true
    }
    dirContextValidator(org.springframework.ldap.pool2.validation.DefaultDirContextValidator)
    poolConfig( org.springframework.ldap.pool2.factory.PoolConfig ) {
        testOnBorrow = true
        testWhileIdle = true
    }
    ldapContextSource(org.springframework.ldap.pool2.factory.PooledContextSource, ref('poolConfig')) {
        contextSource = ref('contextSourceTarget')
        dirContextValidator = ref('dirContextValidator')
    }

    ldapTemplate(LdapTemplate, ref('ldapContextSource')) {}

我希望此应用程序可能同时(通过并发该应用程序的其余调用)命中LDAP几次,以从不同的用户中检索数据。这是进行该调用的代码:

    List attrs =['uid', 'otherattr1', 'otherattr2']
// this just returns a Map containing the key value pairs of the attrs passed in here.
    LdapNamedContextMapper mapper = new LdapNamedContextMapper( attrs ) 
    log.debug( "getLdapUser:preLdapSearch")
    List<Map> results = ldapTemplate.search( 
        'cn=grouproot,cn=Groups,dc=org,dc=com',
        'uniquemember=userNameImsearchingfor',
        SearchControls.SUBTREE_SCOPE, 
        attrs as String[], mapper )
    log.debug( "getLdapUser:postLdapSearch" )

不幸的是,似乎在随机时间,preLdapSearch和postLdapSearch日志之间的时间戳差异超过15分钟。显然,这很糟糕,这似乎是一个池管理问题。

所以我打开了org.springframework.ldap和org.apache.commons.pool2软件包的调试日志记录

现在发生这种情况时,我在日志中得到以下内容:

2018-09-20 20:18:46.251 DEBUG appEvent="getLdapUser:preLdapSearch"
2018-09-20 20:35:03.246 DEBUG  A class javax.naming.ServiceUnavailableException - not explicitly configured to be a non-transient exception - encountered; ignoring.
2018-09-20 20:35:03.249 DEBUG  DirContext 'javax.naming.ldap.InitialLdapContext@1f4f37b4' failed validation with an exception.
javax.naming.ServiceUnavailableException: my.ldaphost.com:636; socket closed
at com.sun.jndi.ldap.Connection.readReply(Connection.java:454)
at com.sun.jndi.ldap.LdapClient.getSearchReply(LdapClient.java:638)
at com.sun.jndi.ldap.LdapClient.getSearchReply(LdapClient.java:638)
at com.sun.jndi.ldap.LdapClient.search(LdapClient.java:561)
at com.sun.jndi.ldap.LdapCtx.doSearch(LdapCtx.java:1985)
at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1844)
at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1769)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(ComponentDirContext.java:392)
(LOTS OF STACK TRACE REMOVED)
2018-09-20 20:35:03.249 DEBUG Closing READ_ONLY DirContext='javax.naming.ldap.InitialLdapContext@1f4f37b4'
2018-09-20 20:35:03.249 DEBUG Closed READ_ONLY DirContext='javax.naming.ldap.InitialLdapContext@1f4f37b4'
2018-09-20 20:35:03.249 DEBUG Creating a new READ_ONLY DirContext
2018-09-20 20:35:03.787 DEBUG Created new READ_ONLY DirContext='javax.naming.ldap.InitialLdapContext@5239386d'
2018-09-20 20:35:03.838 DEBUG DirContext 'javax.naming.ldap.InitialLdapContext@5239386d' passed validation.
2018-09-20 20:35:03.890 DEBUG appEvent="getLdapUser:postLdapSearch"

问题:

  1. 如何查找更多信息?我已为org.springframework.ldap和org.apache.commons.pool2打开调试日志记录

  2. 为什么似乎需要15分钟以上的时间才能确定连接陈旧/不可用?我该如何配置以使其更短?

1 个答案:

答案 0 :(得分:1)

底层LDAP系统很有可能出现连接问题。 您可以尝试在连接池设置中添加超时:

  

最大等待-默认值为-1
  逐出运行间隔毫秒-您可以   希望将其设置为控制检查问题的频率

文档:https://docs.spring.io/spring-ldap/docs/current/reference/#pool-configuration

相关问题