在MongoClient添加连接配置信息主要是将配置信息添加到url来实现的
MongoClient的都有哪些配置 ,在MongoClients的create方法中有这样一个方法,方法传的是mongodb的连接地址,然后会创建一个ConnectionString对象,这个对象里保存着MongoClient的所有的配置信息,像最大连接数、连接超时时间等信息
public static MongoClient create(String connectionString) {
return create(new ConnectionString(connectionString));
}
这里是对传来的ConnectionString地址进行解析成unprocessedConnectionString
String unprocessedConnectionString;
if (isMongoDBProtocol) {
unprocessedConnectionString = connectionString.substring("mongodb://".length());
} else {
unprocessedConnectionString = connectionString.substring("mongodb+srv://".length());
}
这两行代码是将unprocessedConnectionString和txtRecordsQueryParameters的连接信息进行解析封装成map
Map<String, List<String>> connectionStringOptionsMap = this.parseOptions(unprocessedConnectionString);
Map<String, List<String>> txtRecordsOptionsMap = this.parseOptions(txtRecordsQueryParameters);
这里将前两个map进行整合成参数map
Map<String, List<String>> combinedOptionsMaps = this.combineOptionsMaps(txtRecordsOptionsMap, connectionStringOptionsMap);
然后调用this.translateOptions(combinedOptionsMaps)方法,给ConnectionString对象的连接信息参数赋值
this.translateOptions(combinedOptionsMaps);
while(var4.hasNext()) {
String key = (String)var4.next();
String value = this.getLastValue(optionsMap, key);
if (value != null) {
if (key.equals("maxpoolsize")) {
this.maxConnectionPoolSize = this.parseInteger(value, "maxpoolsize");
} else if (key.equals("minpoolsize")) {
this.minConnectionPoolSize = this.parseInteger(value, "minpoolsize");
} else if (key.equals("maxidletimems")) {
this.maxConnectionIdleTime = this.parseInteger(value, "maxidletimems");
} else if (key.equals("maxlifetimems")) {
this.maxConnectionLifeTime = this.parseInteger(value, "maxlifetimems");
} else if (key.equals("waitqueuetimeoutms")) {
this.maxWaitTime = this.parseInteger(value, "waitqueuetimeoutms");
} else if (key.equals("connecttimeoutms")) {
this.connectTimeout = this.parseInteger(value, "connecttimeoutms");
} else if (key.equals("sockettimeoutms")) {
this.socketTimeout = this.parseInteger(value, "sockettimeoutms");
} else if (key.equals("tlsallowinvalidhostnames")) {
this.sslInvalidHostnameAllowed = this.parseBoolean(value, "tlsAllowInvalidHostnames");
tlsAllowInvalidHostnamesSet = true;
} else if (key.equals("sslinvalidhostnameallowed")) {
this.sslInvalidHostnameAllowed = this.parseBoolean(value, "sslinvalidhostnameallowed");
tlsAllowInvalidHostnamesSet = true;
} else if (key.equals("tlsinsecure")) {
this.sslInvalidHostnameAllowed = this.parseBoolean(value, "tlsinsecure");
tlsInsecureSet = true;
} else if (key.equals("ssl")) {
this.initializeSslEnabled("ssl", value);
} else if (key.equals("tls")) {
this.initializeSslEnabled("tls", value);
} else if (key.equals("replicaset")) {
this.requiredReplicaSetName = value;
} else if (key.equals("readconcernlevel")) {
this.readConcern = new ReadConcern(ReadConcernLevel.fromString(value));
} else if (key.equals("serverselectiontimeoutms")) {
this.serverSelectionTimeout = this.parseInteger(value, "serverselectiontimeoutms");
} else if (key.equals("localthresholdms")) {
this.localThreshold = this.parseInteger(value, "localthresholdms");
} else if (key.equals("heartbeatfrequencyms")) {
this.heartbeatFrequency = this.parseInteger(value, "heartbeatfrequencyms");
} else if (key.equals("appname")) {
this.applicationName = value;
} else if (key.equals("retrywrites")) {
this.retryWrites = this.parseBoolean(value, "retrywrites");
} else if (key.equals("retryreads")) {
this.retryReads = this.parseBoolean(value, "retryreads");
} else if (key.equals("uuidrepresentation")) {
this.uuidRepresentation = this.createUuidRepresentation(value);
} else if (key.equals("directconnection")) {
this.directConnection = this.parseBoolean(value, "directconnection");
}
}
}
从上面的参数中找到需要的配置参数然后加到mongodb的连接地址中即可生效,例如
mongodb://admin:Abc123++@127.0.0.1:27017/?authSource=local&serverselectiontimeoutms=1000