我可以将Jmeter成功连接到MongoDB,但是将文档插入到Mongo DB失败。请注意,我能够使用相同的凭据将文档直接插入Mongo。
通过以下代码成功连接到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(InetAddress.getByName(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);
}
在另一个采样器中,我正在尝试将文档添加到MongoDB:
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import static com.mongodb.client.model.Filters.*;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.util.Arrays;
try {
MongoCollection<Document> collection = vars.getObject("collection");
Document document = new Document("EmployeeOID",1111111)
.append("EmployeeName", "Test Automation through Jmeter")
.append("Employee_Type_OID",4)
.append("Rank",0)
.append("Rating",0)
.append("Score",0)
.append("Supervisor_OID",56789)
.append("EmpID","222222T");
collection.insertOne(document);
Document result = collection.find(eq("EmployeeOID",1111111)).first();
if(result !=null){
String ID =result.get("_id");
log.info(ID)
ObjectId objectId = new ObjectId(ID);
vars.put('myID', objectId as String);
log.info (vars.get('myID'))
return ;
}
return "Employee not found";
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}
失败,并显示错误:
响应码:500 响应消息:异常:com.mongodb.MongoTimeoutException:等待连接30000毫秒后超时。群集状态的客户端视图是{java.net。} {type = UNKNOWN,服务器= [{address = localhost:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSocketOpenException:异常打开套接字}}。 ConnectException:连接被拒绝:connect}}]
答案 0 :(得分:0)
Successfully Connected to Mongo by following code
是不正确的,如果您仔细查看错误详细信息:
响应代码:500响应消息:异常: com.mongodb.MongoTimeoutException:等待连接30000毫秒后超时。群集状态的客户端视图为{type = UNKNOWN,servers = [{地址= 本地主机:27017 ,类型=未知,状态= 连接,异常= {com.mongodb.MongoSocketOpenException:异常打开套接字},由{java.net.ConnectException引起:连接被拒绝:连接}}]
您似乎正在尝试通过端口27017连接到本地计算机,但连接被拒绝。
如果您真的想在本地计算机上使用MongoDB实例,请仔细检查:
27017
localhost
可能代表不同的IP地址:loopback,private network,IPv4,IPv6,外部IP地址等,并且您的MongoDB服务器可能正在侦听一个IP地址和JMeter尝试连接到另一个。查看MongoDB文档的IP Binding一章,以了解有关启用远程MongoDB访问的更多信息如果您的MongoDB实例位于另一台计算机上,则意味着mongoHost
JMeter变量的值错误,您可以使用Debug Sampler and View Results Tree listener组合再次检查它。