1. 首页 > 国产数据库教程 > TiDB教程 > 正文

tidb-099-TiDB与ShardingSphere集成

3.1.2 配置ShardingSphere-JDBC

// application.yml
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:shardingsphere-config.yaml

// shardingsphere-config.yaml
mode:
  type: Memory

rules:
  - !SHARDING
    tables:
      t_order:
        actualDataNodes: tidb_ds.t_order_${0..1}
        tableStrategy:
          standard:
            shardingColumn: order_id
            shardingAlgorithmName: t_order_inline
        keyGenerateStrategy:
          column: order_id
          keyGeneratorName: snowflake
    shardingAlgorithms:
      t_order_inline:
        type: INLINE
        props:
          algorithm-expression: t_order_${order_id % 2}
    keyGenerators:
      snowflake:
        type: SNOWFLAKE
        props:
          worker-id: 123

dataSources:
  tidb_ds:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://192.168.1.20:4000/test?useSSL=false
    username: root
    password: password
    maxPoolSize: 10

3.2 使用ShardingSphere-Proxy集成TiDB

3.2.1 安装ShardingSphere-Proxy

# 下载ShardingSphere-Proxy
wget https://archive.apache.org/dist/shardingsphere/5.3.2/apache-shardingsphere-5.3.2-shardingsphere-proxy-bin.tar.gz

# 解压
tar -xzf apache-shardingsphere-5.3.2-shardingsphere-proxy-bin.tar.gz
cd apache-shardingsphere-5.3.2-shardingsphere-proxy-bin

# 复制MySQL驱动
cp mysql-connector-java-8.0.30.jar lib/

3.2.2 配置ShardingSphere-Proxy

# conf/server.yaml
mode:
  type: Cluster
  repository:
    type: ZooKeeper
    props:
      namespace: shardingsphere
      server-lists: 192.168.1.10:2181,192.168.1.11:2181,192.168.1.12:2181
      retryIntervalMilliseconds: 500
      timeToLiveSeconds: 60
      maxRetries: 3
      operationTimeoutMilliseconds: 500

rules:
  - !AUTHORITY
    users:
      - user: root@%:root
      - user: sharding@127.0.0.1:sharding
    provider:
      type: ALL_PRIVILEGES_PERMITTED

# conf/config-sharding.yaml
dataSources:
  tidb_ds:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver学习交流加群风哥QQ113257174
    jdbcUrl: jdbc:mysql://192.168.1.20:4000/test?useSSL=false
    username: root
    password: password
    maxPoolSize: 10

rules:
  - !SHARDING
    tables:
      t_order:
        actualDataNodes: tidb_ds.t_order_${0..1}
        tableStrategy:
          standard:
            shardingColumn: order_id
            shardingAlgorithmName: t_order_inline
        keyGenerateStrategy:
          column: order_id
          keyGeneratorName: snowflake
    shardingAlgorithms:
      t_order_inline:
        type: INLINE
        props:
          algorithm-expression: t_order_${order_id % 2}
    keyGenerators:
      snowflake:
        type: SNOWFLAKE
        props:
          worker-id: 123

3.2.3 启动ShardingSphere-Proxy

# 启动ShardingSphere-Proxy
bin/start.sh

# 查看日志
tail -f logs/stdout.log

# 连接ShardingSphere-Proxy
mysql -h 192.168.1.30 -P 3307 -u root -p

3.3 配置读写分离

# conf/config-readwrite-splitting.yaml
dataSources:
  primary_ds:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://192.168.1.20:4000/test?useSSL=false
    username: root
    password: password
  replica_ds_1:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://192.168.1.21:4000/test?useSSL=false
    username: root
    password: password

rules:
  - !READWRITE_SPLITTING
    dataSources:
      tidb_ds:
        writeDataSourceName: primary_ds
        readDataSourceNames:
          - replica_ds_1
        loadBalancerName: round_robin
    loadBalancers:
      round_robin:
        type: ROUND_ROBIN

更多视频教程www.fgedu.net.cn

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息