org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory Java Examples

The following examples show how to use org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShardingConfig.java    From java-tutorial with MIT License 5 votes vote down vote up
/**
 * 获取数据源对象
 *
 * @throws SQLException
 */
@Bean
public DataSource shardingCreateDataSource() throws SQLException {
    Map<String, DataSource> dataSourceMap = getDataSourceMap();
    ShardingRuleConfiguration shardingRuleConfig = getShardingRuleConfig();
    DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());
    logger.info("------------------- The datasource has been created ---------------------------");
    return dataSource;
}
 
Example #2
Source File: ShardingDatabasesAndTablesConfigurationPrecise.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example #3
Source File: ShardingDatabasesAndTablesConfigurationPrecise.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example #4
Source File: ShardingDatabasesAndTablesConfigurationPrecise.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example #5
Source File: ShardingDatabasesAndTablesConfigurationPrecise.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
    return dataSource;
}
 
Example #6
Source File: DataSharding.java    From blog with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void main(String[] args) {
	// 配置真实数据源
	Map<String, DataSource> dataSourceMap = new HashMap<>();

	// 配置第一个数据源
	BasicDataSource dataSource1 = new BasicDataSource();
	dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
	dataSource1.setUrl("jdbc:mysql://localhost:3306/ds0");
	dataSource1.setUsername("root");
	dataSource1.setPassword("root");
	dataSourceMap.put("ds0", dataSource1);

	// 配置第二个数据源
	BasicDataSource dataSource2 = new BasicDataSource();
	dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
	dataSource2.setUrl("jdbc:mysql://localhost:3306/ds1");
	dataSource2.setUsername("root");
	dataSource2.setPassword("root");
	dataSourceMap.put("ds1", dataSource2);

	// 配置Order表规则
	TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order", "ds${0..1}.t_order${0..1}");

	// 配置分库 + 分表策略
	orderTableRuleConfig.setDatabaseShardingStrategyConfig(
			new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
	orderTableRuleConfig.setTableShardingStrategyConfig(
			new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));

	// 配置分片规则
	ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
	shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);

	// 省略配置order_item表规则...
	// ...

	// 获取数据源对象
	try {
		DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig,
				new Properties());
		Connection conn = dataSource.getConnection();
		String sql = "insert into t_order (user_id,order_id) values (?,?)";
		PreparedStatement preparedStatement = conn.prepareStatement(sql);
		for (int i = 1; i <= 10; i++) {
			preparedStatement.setInt(1, 10 + i);
			preparedStatement.setInt(2, 1001 + i);
			preparedStatement.executeUpdate();
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
 
Example #7
Source File: ShardingSpringBootConfiguration.java    From seata-samples with Apache License 2.0 2 votes vote down vote up
/**
 * Get sharding data source bean.
 *
 * @return data source bean
 * @throws SQLException SQL exception
 */
@Bean
@Conditional(ShardingRuleCondition.class)
public DataSource shardingDataSource() throws SQLException {
    return ShardingDataSourceFactory.createDataSource(dataSourceMap, new ShardingRuleConfigurationYamlSwapper().swap(shardingProperties), propMapProperties.getProps());
}