Search in sources :

Example 1 with MultiServerCluster

use of com.mongodb.internal.connection.MultiServerCluster in project LinkAgent by shulieTech.

the class MongoDBMongoClientDelegateConstructorInterceptor method check.

/**
 * 判断是否影子库
 */
private void check(Object baseCluster, Mongo businessMongo) {
    List<ServerAddress> addressList;
    if (baseCluster instanceof SingleServerCluster) {
        addressList = ((SingleServerCluster) baseCluster).getSettings().getHosts();
    } else {
        addressList = ((MultiServerCluster) baseCluster).getSettings().getHosts();
    }
    for (Map.Entry<String, ShadowDatabaseConfig> entry : GlobalConfig.getInstance().getShadowDatasourceConfigs().entrySet()) {
        String businessUrl = entry.getValue().getUrl();
        for (ServerAddress address : addressList) {
            if (businessUrl != null && businessUrl.contains(address.toString())) {
                if (!MongoClientHolder.mongoClientMap.containsKey(address.toString())) {
                    MongoClientHolder.mongoIntegerMap.put(businessMongo, 2);
                    break;
                } else {
                    throw new PressureMeasureError("配置了相同地址的的影子库!");
                }
            }
        }
    }
    for (ServerAddress address : addressList) {
        ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(address.toString());
        if (shadowDatabaseConfig == null) {
            continue;
        }
        if (!shadowDatabaseConfig.isShadowTable()) {
            continue;
        }
        Map<String, String> map = shadowDatabaseConfig.getBusinessShadowTables();
        if (null != map && map.size() > 0) {
            MongoClientHolder.mongoIntegerMap.put(businessMongo, 1);
            MongoClientHolder.mongoTableMappingMap.put(businessMongo, map);
            break;
        }
    }
}
Also used : SingleServerCluster(com.mongodb.internal.connection.SingleServerCluster) MultiServerCluster(com.mongodb.internal.connection.MultiServerCluster) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) ServerAddress(com.mongodb.ServerAddress) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) Map(java.util.Map)

Aggregations

ServerAddress (com.mongodb.ServerAddress)1 MultiServerCluster (com.mongodb.internal.connection.MultiServerCluster)1 SingleServerCluster (com.mongodb.internal.connection.SingleServerCluster)1 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)1 Map (java.util.Map)1