use of com.ctrip.framework.dal.cluster.client.database.ConnectionString in project dal by ctripcorp.
the class ClusterDynamicDataSource method buildHostAndPort.
private HostAndPort buildHostAndPort(Cluster cluster) {
if (cluster.getRouteStrategyConfig().multiMaster()) {
StringBuilder hosts = new StringBuilder();
StringBuilder hostsWithPorts = new StringBuilder();
Set<Integer> ports = new HashSet<>();
cluster.getDatabases().forEach(database -> {
ConnectionString connStr = database.getConnectionString();
if (hosts.length() > 0)
hosts.append(",");
if (hostsWithPorts.length() > 0)
hostsWithPorts.append(",");
hosts.append(connStr.getPrimaryHost());
hostsWithPorts.append(connStr.getPrimaryHost()).append(":").append(connStr.getPrimaryPort());
ports.add(connStr.getPrimaryPort());
});
if (ports.size() == 1)
return new HostAndPort(null, hosts.toString(), ports.iterator().next());
else
return new HostAndPort(null, hostsWithPorts.toString(), 0);
} else {
ConnectionString connStr = cluster.getMasterOnShard(clusterInfo.getShardIndex()).getConnectionString();
return new HostAndPort(null, connStr.getPrimaryHost(), connStr.getPrimaryPort());
}
}
use of com.ctrip.framework.dal.cluster.client.database.ConnectionString in project dal by ctripcorp.
the class TagDataSourceIdentity method init.
private void init(Database database, String tag) {
String role = database.isMaster() ? MASTER : SLAVE;
ConnectionString connString = database.getConnectionString();
id = String.format(ID_FORMAT, database.getClusterName(), database.getShardIndex(), role, connString.getPrimaryHost(), tag);
}
use of com.ctrip.framework.dal.cluster.client.database.ConnectionString in project dal by ctripcorp.
the class ClusterDataSource method createInnerDataSource.
protected MultiHostDataSource createInnerDataSource() {
try {
List<Database> databases = cluster.getDatabases();
Map<HostSpec, DataSourceConfigure> dataSourceConfigs = new HashMap<>();
databases.forEach(database -> {
ConnectionString connString = database.getConnectionString();
HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone());
DataSourceIdentity id = new ClusterDataSourceIdentity(database);
DataSourceConfigure config = provider.getDataSourceConfigure(id);
dataSourceConfigs.put(host, config);
});
MultiHostClusterProperties properties = new MultiHostClusterPropertiesAdapter(cluster.getRouteStrategyConfig(), cluster.getClusterName());
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format(CAT_LOG_NAME_MULTI_HOST, getClusterName()), "");
return new MultiHostDataSource(buildShardMeta(dataSourceConfigs.keySet()), dataSourceConfigs, properties);
} catch (Throwable t) {
LOGGER.logEvent(DalLogTypes.DAL_DATASOURCE, String.format(CAT_LOG_NAME_MULTI_HOST_FAIL, getClusterName()), "");
throw t;
}
}
use of com.ctrip.framework.dal.cluster.client.database.ConnectionString in project dal by ctripcorp.
the class ClusterDynamicDataSource method createCustomDataSource.
protected DataSource createCustomDataSource() {
CustomDataSourceFactory dataSourceFactory = getCustomDataSourceFactory();
List<Database> databases = cluster.getDatabases();
Set<HostSpec> hostsInfos = new HashSet<>();
databases.forEach(database -> {
ConnectionString connString = database.getConnectionString();
HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone());
hostsInfos.add(host);
});
return dataSourceFactory.createDataSource(hostsInfos, getProperties(databases.get(0)));
}
use of com.ctrip.framework.dal.cluster.client.database.ConnectionString in project dal by ctripcorp.
the class ClusterDataSourceIdentity method init.
protected void init() {
String role = database.isMaster() ? MASTER : SLAVE;
ConnectionString connString = database.getConnectionString();
id = String.format(ID_FORMAT, database.getClusterName(), database.getShardIndex(), role, connString.getPrimaryHost());
this.connectionString = new ClusterConnectionStringImpl(id, database);
}
Aggregations