Search in sources :

Example 1 with ConnectionString

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());
    }
}
Also used : ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString)

Example 2 with ConnectionString

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);
}
Also used : ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString)

Example 3 with ConnectionString

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;
    }
}
Also used : HashMap(java.util.HashMap) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Database(com.ctrip.framework.dal.cluster.client.database.Database) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) DataSourceConfigure(com.ctrip.platform.dal.dao.configure.DataSourceConfigure)

Example 4 with ConnectionString

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)));
}
Also used : Database(com.ctrip.framework.dal.cluster.client.database.Database) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) CustomDataSourceFactory(com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)

Example 5 with ConnectionString

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);
}
Also used : DalConnectionString(com.ctrip.platform.dal.dao.configure.DalConnectionString) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) DalConnectionString(com.ctrip.platform.dal.dao.configure.DalConnectionString) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString)

Aggregations

ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)6 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)3 Database (com.ctrip.framework.dal.cluster.client.database.Database)3 DalMetadataException (com.ctrip.framework.dal.cluster.client.exception.DalMetadataException)1 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)1 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)1 DalConnectionString (com.ctrip.platform.dal.dao.configure.DalConnectionString)1 DataSourceConfigure (com.ctrip.platform.dal.dao.configure.DataSourceConfigure)1 HashMap (java.util.HashMap)1