Search in sources :

Example 6 with ConnectionString

use of com.ctrip.framework.dal.cluster.client.database.ConnectionString in project dal by ctripcorp.

the class DatabaseShardImpl method initReadStrategy.

public void initReadStrategy() {
    ClusterRouteStrategyConfig config = databaseShardConfig.getClusterConfig().getRouteStrategyConfig();
    if (config == null || config.multiMaster()) {
        return;
    }
    routeStrategy = (ReadStrategy) config.generate();
    List<Database> databases = new ArrayList<>();
    databases.addAll(masters);
    databases.addAll(slaves);
    Set<HostSpec> hostSpecs = new HashSet<>();
    databases.forEach(database -> {
        ConnectionString connString = database.getConnectionString();
        HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone(), database.isMaster());
        hostToDataBase.putIfAbsent(host, database);
        hostSpecs.add(host);
    });
    try {
        routeStrategy.init(hostSpecs, new CaseInsensitiveProperties());
    } catch (DalMetadataException error) {
        throw new DalMetadataException(databaseShardConfig.getClusterConfig().getClusterName() + error.getMessage());
    }
}
Also used : CaseInsensitiveProperties(com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties) ClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig) DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) 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)

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