# 快速入门

# ShardingSphere-JDBC

# 1. 引入 maven 依赖

    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>${shardingsphere.version}</version>
    </dependency>
1
2
3
4
5

注意:请将 ${shardingsphere.version} 更改为实际的版本号,这里使用的4.0.1

# 2. 数据分片

spring.shardingsphere.datasource.names=ds

spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://127.0.0.1:3306/sharding?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&allowMasterDownConnections=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.ds.username=root
spring.shardingsphere.datasource.ds.password=root

spring.shardingsphere.sharding.tables.zm_xtc_user.actualDataNodes=ds.zm_xtc_user_${0..1}
# 行表达式分片策略
spring.shardingsphere.sharding.tables.zm_xtc_user.tableStrategy.inline.shardingColumn=user_id
spring.shardingsphere.sharding.tables.zm_xtc_user.tableStrategy.inline.algorithmExpression=zm_xtc_user_${user_id % 2}

# 开发环境配置 
spring.shardingsphere.props.sql.show=true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 3. 读写分离

##数据源配置
spring:
  shardingsphere:
    datasource:           #数据源配置信息
      names: master,slave0,slave1,slave2
      master:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/sharding?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&allowMasterDownConnections=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&allowPublicKeyRetrieval=true
        username: root
        password: root
      slave0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/sharding?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&allowMasterDownConnections=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&allowPublicKeyRetrieval=true
        username: root
        password: root
      slave1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url:jdbc:mysql://127.0.0.1:3306/sharding?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&allowMasterDownConnections=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&allowPublicKeyRetrieval=true
        username: root
        password: root
      slave2:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/sharding?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&allowMasterDownConnections=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&allowPublicKeyRetrieval=true
        username: root
        password: root
        
    masterslave:        #主从关系配置  
      name: ms
      master-data-source-name: master
      slave-data-source-names: slave0,slave1,slave2
    props:
      sql: 
        show: true    # 开发环境打开SQL显示,其他的环境需要关闭 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 切分策略

数据水平切分后我们希望是一劳永逸或者是易于水平扩展的,所以推荐采用mod 2^n这种一致性Hash

以统一订单库为例,我们分库分表的方案是32*32的,即通过 UserId后四位 mod 32分到32个库中, 同时再将 UserId后四位 Div 32 Mod 32将每个库分为32个表, 共计分为1024张表。线上部署情况为8个集群(主从),每个集群4个库。

Last Updated: 2 years ago