Jmeter 无法连接到 MongoDB 并抛出错误
Jmeter fails to connect to MongoDB and throws error
我正在尝试使用以下代码连接到 Mongo 数据库:
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
try {
String mongoUser = vars.get("mongouser");
String userDB = vars.get("userdb");
char[] password = vars.get("password").toCharArray();
MongoCredential credential = MongoCredential.createCredential(mongoUser, userDB, password);
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);
}
但失败并出现错误:
Response code: 500
Response message: Exception: groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.ServerAddress#<init>.
Cannot resolve which method to invoke for [null, class java.lang.Integer] due to overlapping prototypes between:
[class java.lang.String, int]
[class java.net.InetAddress, int]
您需要使用InetAddress
builder.hosts(Arrays.asList(new ServerAddress(InetAddress.getByName(vars.get("mongoHost")),vars.get("mongoPort").toInteger())))}
.build();
我正在尝试使用以下代码连接到 Mongo 数据库:
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
try {
String mongoUser = vars.get("mongouser");
String userDB = vars.get("userdb");
char[] password = vars.get("password").toCharArray();
MongoCredential credential = MongoCredential.createCredential(mongoUser, userDB, password);
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);
}
但失败并出现错误:
Response code: 500
Response message: Exception: groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.ServerAddress#<init>.
Cannot resolve which method to invoke for [null, class java.lang.Integer] due to overlapping prototypes between:
[class java.lang.String, int]
[class java.net.InetAddress, int]
您需要使用InetAddress
builder.hosts(Arrays.asList(new ServerAddress(InetAddress.getByName(vars.get("mongoHost")),vars.get("mongoPort").toInteger())))}
.build();