mysqlsharding配置_数据库分库分表配置sharding-jdbc
@Bean(name = "shardingDataSource", destroyMethod = "close")
@Qualifier("shardingDataSource")publicDataSource getShardingDataSource() {//配置真实数据源
Map dataSourceMap = new HashMap<>(3);//配置第⼀个数据源
DruidDataSource dataSource1 =createDefaultDruidDataSource();
dataSource1.setDriverClassName("sql.jdbc.Driver");
dataSource1.setUrl("jdbc:mysql://localhost:3306/db0");
dataSource1.setUsername("root");
dataSource1.setPassword("root");
dataSourceMap.put("db0", dataSource1);//配置第⼆个数据源
DruidDataSource dataSource2 =createDefaultDruidDataSource();
dataSource2.setDriverClassName("sql.jdbc.Driver");
dataSource2.setUrl("jdbc:mysql://localhost:3306/db1");
dataSource2.setUsername("root");
dataSource2.setPassword("root");mysql下载后为啥localhost打不开
dataSource2.setName("db1-0001");
dataSourceMap.put("db1", dataSource2);//配置第三个数据源
DruidDataSource dataSource3 =createDefaultDruidDataSource();
dataSource3.setDriverClassName("sql.jdbc.Driver");
dataSource3.setUrl("jdbc:mysql://localhost:3306/db2");
dataSource3.setUsername("root");
dataSource3.setPassword("root");
dataSourceMap.put("db2", dataSource3);//配置Order表规则
TableRuleConfiguration orderTableRuleConfig = newTableRuleConfiguration();
orderTableRuleConfig.setLogicTable("t_order");
orderTableRuleConfig.setActualDataNodes("db${0..2}.t_order_${0..1}");//orderTableRuleConfig.setActualDataNodes("db0.t_ord 配置分库策略(Groovy表达式配置db规则)
orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "db${user_id
% 3}"));//配置分表策略(Groovy表达式配置表路由规则)
orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id",
"t_order_${order_id % 2}"));//配置分⽚规则
ShardingRuleConfiguration shardingRuleConfig = newShardingRuleConfiguration();
DataSource dataSource = null;try{
dataSource= ateDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), newProperties());
}catch(SQLException e) {
e.printStackTrace();
}returndataSource;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论