MongoDB Replica Set connections cannot resolve (#776 & #777)

This commit is contained in:
Andrew 2018-02-20 14:59:29 -05:00 committed by Luck
parent 31e436868d
commit 9c92522564
7 changed files with 53 additions and 24 deletions

View File

@ -328,6 +328,10 @@ data:
# The prefix to use for all LuckPerms collections. Change this if you want to use different
# collections for different servers. The default is no prefix.
mongodb_collection_prefix: ''
# MongoDB ClientConnectionURI for use with replicasets and custom connection options
# See https://docs.mongodb.com/manual/reference/connection-string/
mongodb_connection_URI: ''
# This option controls how frequently LuckPerms will perform a sync task.
# A sync task will refresh all data from the storage, and ensure that the most up-to-date data is
@ -456,4 +460,4 @@ default-assignments:
# - group.default
# give:
# - group.member
# set-primary-group: member
# set-primary-group: member

View File

@ -270,6 +270,10 @@ data:
# The prefix to use for all LuckPerms collections. Change this if you want to use different
# collections for different servers. The default is no prefix.
mongodb_collection_prefix: ''
# MongoDB ClientConnectionURI for use with replicasets and custom connection options
# See https://docs.mongodb.com/manual/reference/connection-string/
mongodb_connection_URI: ''
# This option controls how frequently LuckPerms will perform a sync task.
# A sync task will refresh all data from the storage, and ensure that the most up-to-date data is
@ -399,4 +403,4 @@ default-assignments:
# - group.default
# give:
# - group.member
# set-primary-group: member
# set-primary-group: member

View File

@ -404,7 +404,12 @@ public class ConfigKeys {
* The prefix for any MongoDB collections
*/
public static final ConfigKey<String> MONGODB_COLLECTION_PREFIX = EnduringKey.wrap(StringKey.of("data.mongodb_collection_prefix", ""));
/**
* MongoDB ClientConnectionURI to override default connection options
*/
public static final ConfigKey<String> MONGODB_CONNECTION_URI = EnduringKey.wrap(StringKey.of("data.mongodb_connection_URI", ""));
/**
* The name of the storage method being used
*/

View File

@ -158,7 +158,8 @@ public class StorageFactory {
return new MongoDao(
this.plugin,
this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES),
this.plugin.getConfiguration().get(ConfigKeys.MONGODB_COLLECTION_PREFIX)
this.plugin.getConfiguration().get(ConfigKeys.MONGODB_COLLECTION_PREFIX),
this.plugin.getConfiguration().get(ConfigKeys.MONGODB_CONNECTION_URI)
);
case YAML:
return new YamlDao(this.plugin, "yaml-storage");

View File

@ -28,6 +28,7 @@ package me.lucko.luckperms.common.storage.dao.mongodb;
import com.google.common.base.Strings;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
@ -77,35 +78,41 @@ public class MongoDao extends AbstractDao {
private MongoClient mongoClient;
private MongoDatabase database;
private final String prefix;
private final String connectionURI;
public MongoDao(LuckPermsPlugin plugin, StorageCredentials configuration, String prefix) {
public MongoDao(LuckPermsPlugin plugin, StorageCredentials configuration, String prefix, String connectionURI) {
super(plugin, "MongoDB");
this.configuration = configuration;
this.prefix = prefix;
this.connectionURI = connectionURI;
}
@Override
public void init() {
MongoCredential credential = null;
if (!Strings.isNullOrEmpty(this.configuration.getUsername())) {
credential = MongoCredential.createCredential(
this.configuration.getUsername(),
this.configuration.getDatabase(),
Strings.isNullOrEmpty(this.configuration.getPassword()) ? null : this.configuration.getPassword().toCharArray()
);
}
String[] addressSplit = this.configuration.getAddress().split(":");
String host = addressSplit[0];
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017;
ServerAddress address = new ServerAddress(host, port);
if (credential == null) {
this.mongoClient = new MongoClient(address);
if (!Strings.isNullOrempty(this.connectionURI)) {
this.mongoClient = new MongoClient(new MongoClientURI(this.connectionURI));
} else {
this.mongoClient = new MongoClient(address, credential, MongoClientOptions.builder().build());
}
MongoCredential credential = null;
if (!Strings.isNullOrEmpty(this.configuration.getUsername())) {
credential = MongoCredential.createCredential(
this.configuration.getUsername(),
this.configuration.getDatabase(),
Strings.isNullOrEmpty(this.configuration.getPassword()) ? null : this.configuration.getPassword().toCharArray()
);
}
String[] addressSplit = this.configuration.getAddress().split(":");
String host = addressSplit[0];
int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017;
ServerAddress address = new ServerAddress(host, port);
if (credential == null) {
this.mongoClient = new MongoClient(address);
} else {
this.mongoClient = new MongoClient(address, credential, MongoClientOptions.builder().build());
}
}
this.database = this.mongoClient.getDatabase(this.configuration.getDatabase());
}

View File

@ -328,6 +328,10 @@ data:
# The prefix to use for all LuckPerms collections. Change this if you want to use different
# collections for different servers. The default is no prefix.
mongodb_collection_prefix: ''
# MongoDB ClientConnectionURI for use with replicasets and custom connection options
# See https://docs.mongodb.com/manual/reference/connection-string/
mongodb_connection_URI: ''
# This option controls how frequently LuckPerms will perform a sync task.
# A sync task will refresh all data from the storage, and ensure that the most up-to-date data is
@ -456,4 +460,4 @@ default-assignments:
# - group.default
# give:
# - group.member
# set-primary-group: member
# set-primary-group: member

View File

@ -277,6 +277,10 @@ data {
# The prefix to use for all LuckPerms collections. Change this if you want to use different
# collections for different servers. The default is no prefix.
mongodb_collection_prefix=""
# MongoDB ClientConnectionURI for use with replicasets and custom connection options
# See https://docs.mongodb.com/manual/reference/connection-string/
mongodb_connection_URI=""
# This option controls how frequently LuckPerms will perform a sync task.
# A sync task will refresh all data from the storage, and ensure that the most up-to-date data is