java.net.SocketException:当我从Java连接到MongoDB 4.0时,未实现未连接的套接字

时间:2018-10-29 12:34:01

标签: java spring sockets sslsocketfactory mongodb4.0

当我尝试使用ssl=true从Java连接MongoDB 4.0时,出现以下错误:

Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.0.1.182:27017, type=UNKNOWN, state=CONNECTING, exceptio
n={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketException: Unconnected sockets not implemented}, caused by {java.lang.UnsupportedOperationException}}]; nested
 exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.0.1.182:27017, type=UNKNOWN, state=C
ONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketException: Unconnected sockets not implemented}, caused by {java.lang.UnsupportedOperationE
xception}}]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:580)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
        ... 28 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SMSTrackRepository': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.0.1.182:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketException: Unconnected sockets not implemented}, caused by {java.lang.UnsupportedOperationException}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.0.1.182:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketException: Unconnected sockets not implemented}, caused by {java.lang.UnsupportedOperationException}}]

SSLSocketFactory创建:

SSLSocketFactory createSSLSocketFactory() throws SAXException, IOException, DocumentException,
        ParserConfigurationException, NoSuchAlgorithmException, NullPointerException, KeyStoreException,
        UnrecoverableKeyException, CertificateException, KeyManagementException {

    SSLContext sslcontext = SSLContext.getInstance("TLS");

    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(new FileInputStream(new File("/home/ubuntu/keystore.jks")), "password".toCharArray());

    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, "password".toCharArray());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(ks);

    sslcontext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    return sslSocketFactory;
}

database-connection.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.10.2.xsd">

    <mongo:mongo-client id="mongo" host="${db.host}"
        port="${db.port}">

        <mongo:client-options connect-timeout="${db.connectTimeout}"
            connections-per-host="${db.connectionPerHost}"
            threads-allowed-to-block-for-connection-multiplier="${db.threadsAllowedTblockForConnectionMultiplier}"
            ssl="true" ssl-socket-factory-ref="sslsocketfactoryMongo" />
    </mongo:mongo-client>

    <mongo:db-factory dbname="${db.name}" host="${db.host}"
        mongo-ref="mongo" port="${db.port}" />
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoConverter" ref="mappingConverter" />
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

    </bean>
    <bean id="readConverter"
        class="com.dpdocter.tokenstore.OAuth2AuthenticationReadConverter" />

    <mongo:mapping-converter id="mappingConverter">
        <mongo:custom-converters>
            <mongo:converter ref="readConverter" />
        </mongo:custom-converters>
    </mongo:mapping-converter>
    <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
        <constructor-arg ref="mongoDbFactory" />
        <constructor-arg ref="mappingConverter" />
    </bean>

    <bean id="sslsocketfactoryMongo" class="com.dpdocter.beans.SSLFactoryMongo"></bean>

</beans>

0 个答案:

没有答案