云上的MongoDB- Jmeter连接不工作

q5lcpyga  于 2022-12-03  发布在  Go
关注(0)|答案(1)|浏览(153)

I am trying to connect to a cloud server that is running a mongodb instance. I am getting below exception when trying to connect from Jmeter.
com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.internal.connection.SocketChannelStream.open(SocketChannelStream.java:63) ~[mongo-java-driver-3.9.1.jar:?] at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.9.1.jar:?] at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.9.1.jar:?] at java.lang.Thread.run(Thread.java:831) ~[?:?] Caused by: java.net.SocketTimeoutException: Connect timed out at sun.nio.ch.SocketChannelImpl.finishTimedConnect(SocketChannelImpl.java:1143) ~[?:?] at sun.nio.ch.SocketChannelImpl.blockingConnect(SocketChannelImpl.java:1181) ~[?:?] at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:97) ~[?:?] at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.9.1.jar:?] at com.mongodb.internal.connection.SocketChannelStream.initializeSocketChannel(SocketChannelStream.java:72) ~[mongo-java-driver-3.9.1.jar:?] at com.mongodb.internal.connection.SocketChannelStream.open(SocketChannelStream.java:60) ~[mongo-java-driver-3.9.1.jar:?] ... 3 more
I am able to connect to the Cloud server using VPN and SSL public certificate, but not through JMeter. Can someone please help ?
I tried connecting using a JMeter connection script :

import com.mongodb.*

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import org.bson.Document;

import java.util.Arrays;

try {
    MongoClientSettings settings = MongoClientSettings.builder()
        .applyToClusterSettings {builder -> 
            builder.hosts(Arrays.asList(new ServerAddress(vars.get("mongoHost"),vars.get("mongoPort").toInteger())))}
        .build();
    
    MongoClient mongoClient = MongoClients.create(settings);
    
    MongoDatabase database = mongoClient.getDatabase(vars.get("databaseName"));
    MongoCollection<Document> collection = database.getCollection(vars.get("collectionName"));
    
    vars.putObject("collection", collection);
    
    return "Connected to " + vars.get("collectionName");
}
catch (Exception e) {
    SampleResult.setSuccessful(false);
    SampleResult.setResponseCode("500");
    SampleResult.setResponseMessage("Exception: " + e);
}

All the fieldnames have been verified, but I am not sure if I have to enter ssl auth details in the connection url.

8cdiaqws

8cdiaqws1#

I think you need to add at least the next line to your script so MongoClientSettings would attempt to create an SSL connection:

.applyToSslSettings(builder -> builder.enabled(true))

The certificate needs to be put into a Java Keystore and JMeter needs to be configured to use the keystore via special system properties .
More information: MongoDB Driver Tutorials > Connect to MongoDB > TLS/SSL

相关问题