Sharding-JDBC实战(一)

2019-01-23  本文已影响0人  杭城码龙

本文讲解sharding-jdbc的分库分表功能,基于eclipse开发工具,以maven项目来讲解,jdk是1.7的版本。

sharding-jdbc以jar包形式提供服务,所以要先引入maven依赖。

1.最精简版pom.xml文件:
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hello.dba</groupId>
  <artifactId>sharding-jdbc-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
  <dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>3.1.0</version>
</dependency>
  <dependency>
   <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
  </dependencies>
</project> 
2. 规则配置

配置是整个sharding-jdbc的核心,是sharding-jdbc中唯一与应用开发者打交道的模块。
sharding-jdbc可以通过Java、YAML、Spring命名空间和Spring Boot Starter四种方式配置。本文讲解java的配置方式,代码如下:

 // 配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        
        // 配置第一个数据源
           //org.apache.commons.dbcp.BasicDataSource
        BasicDataSource dataSource1 = new BasicDataSource();
        dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource1.setUrl("jdbc:mysql://10.0.0.1:3306/ds0");
        dataSource1.setUsername("dba_01");
        dataSource1.setPassword("123456");
        dataSourceMap.put("ds0", dataSource1);
        
        // 配置第二个数据源
            //org.apache.commons.dbcp.BasicDataSource
        BasicDataSource dataSource2 = new BasicDataSource();
        dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource2.setUrl("jdbc:mysql://10.0.0.2:3306/ds1");
        dataSource2.setUsername("dba_01");
        dataSource2.setPassword("123456");
        dataSourceMap.put("ds1", dataSource2);
        
        // 配置Order表规则
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        orderTableRuleConfig.setActualDataNodes("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);
        
        // 获取数据源对象
           //javax.sql.DataSource
        DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap<String, Object>(), new Properties());
3.数据库表信息

第一个DB(ds0):


image.png

第二个DB(ds1):


image.png

备注:表结构需要有user_id和order_id字段,一个是分库字段,一个是分表字段

上一篇下一篇

猜你喜欢

热点阅读