天天看點

ShardingSphere5.0.0-beta的proxy分庫分表demoShardingSphere-5.0.0.beta-proxy分庫分表demo

ShardingSphere-5.0.0.beta-proxy分庫分表demo

示例在linux環境

安裝proxy

下載下傳proxy安裝包和mysql驅動包

  • proxy: wget https://mirrors.bfsu.edu.cn/apache/shardingsphere/5.0.0-beta/apache-shardingsphere-5.0.0-beta-shardingsphere-proxy-bin.tar.gz
  • mysql驅動: wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.31/mysql-connector-java-5.1.31.jar

解壓和驅動包放置

tar -zxvf apache-shardingsphere-5.0.0-beta-shardingsphere-proxy-bin.tar.gz
           

如下目錄結果:

ShardingSphere5.0.0-beta的proxy分庫分表demoShardingSphere-5.0.0.beta-proxy分庫分表demo

mysql驅動移動到lib目錄下,如果使用postgresql資料庫,那就把相應postgresql驅動放在lib目錄下

cp mysql-connector-java-5.1.31.jar  apache-shardingsphere-5.0.0-beta-shardingsphere-proxy-bin/lib
           

修改conf下配置

conf/server.yaml

檔案最後一行追加以下内容,這裡配置root和sharding使用者,密碼都是123456

rules:
  - !AUTHORITY
    users:
      - [email protected]%:123456
      - [email protected]:123456
    provider:
      type: NATIVE
           

conf/config-sharding.yaml 分庫分表

最後一行追加以下内容

#對外暴露的資料庫名稱
schemaName: t_order

dataSources:
  ds_0:
    url: jdbc:mysql://8.142.97.56:3306/demo_write_ds?serverTimezone=UTC&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
    maintenanceIntervalMilliseconds: 30000
  ds_1:
    url: jdbc:mysql://8.142.97.56:3306/demo_read_ds_0?serverTimezone=UTC&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
    maintenanceIntervalMilliseconds: 30000

rules:
- !SHARDING
  tables:
    t_order:
      actualDataNodes: ds_${0..1}.t_order_${0..3}
      tableStrategy:
        standard:
          shardingColumn: order_id
          shardingAlgorithmName: t_order_inline
      keyGenerateStrategy:
        column: order_id
        keyGeneratorName: snowflake
  defaultDatabaseStrategy:
    standard:
      shardingColumn: user_id
      shardingAlgorithmName: database_inline
  defaultTableStrategy:
    none:

  shardingAlgorithms:
    database_inline:
      type: INLINE
      props:
        algorithm-expression: ds_${user_id % 2}
    t_order_inline:
      type: INLINE
      props:
        algorithm-expression: t_order_${order_id % 4}
           
  • 兩個庫、每個庫四個表,使用user_id分庫,order_id分表,字段類型盡量使Integer、Long,謹慎使用String類型
  • ShardingSphere5.0.0-beta配置相對4.X有些變化:shardingRule改rules
  • sql腳本如下
CREATE TABLE `t_order_0` (
  `order_id` bigint(32) NOT NULL COMMENT '訂單id',
  `item` varchar(32) DEFAULT NULL COMMENT '商品名稱',
  `user_id` bigint(20) DEFAULT NULL COMMENT '使用者id',
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='訂單表1';

CREATE TABLE `t_order_1` (
  `order_id` bigint(32) NOT NULL COMMENT '訂單id',
  `item` varchar(32) DEFAULT NULL COMMENT '商品名稱',
  `user_id` bigint(20) DEFAULT NULL COMMENT '使用者id',
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='訂單表2';

CREATE TABLE `t_order_2` (
  `order_id` bigint(32) NOT NULL COMMENT '訂單id',
  `item` varchar(32) DEFAULT NULL COMMENT '商品名稱',
  `user_id` bigint(20) DEFAULT NULL COMMENT '使用者id',
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='訂單表1';

CREATE TABLE `t_order_3` (
  `order_id` bigint(32) NOT NULL COMMENT '訂單id',
  `item` varchar(32) DEFAULT NULL COMMENT '商品名稱',
  `user_id` bigint(20) DEFAULT NULL COMMENT '使用者id',
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='訂單表2';
           

實際效果

通過navicat遠端連結

ShardingSphere5.0.0-beta的proxy分庫分表demoShardingSphere-5.0.0.beta-proxy分庫分表demo

總結

  • Sharding-Proxy的定位是透明化的資料庫代理,它封裝了資料庫二進制協定,用于完成對異構語言的支援。目前相容MySQL和POSTGRESQL,可以使用任何相容MySQL或PG協定的用戶端進行通路
  • ShardingSphere-jdbc是直接對原生jdbc的改寫
  • ShardingSphere-proxy代理後,屏蔽了底層的分庫分表資訊,就等同于使用一個mysql資料庫一樣友善,如果Java開發:生産中大部分都是使用ShardingSphere-jdbc;
ShardingSphere-jdbc優點:jdbc版是jar提供服務,輕量、小而美,沒有其他運維工作, 相對沒有proxy這層網絡開銷、性能高,一般在運維工作中使用proxy,友善查詢資料,而不要各種跨庫跨表操作,缺點:目前jdbc僅支援Java語言開發, 其他語言開發需要使用proxy

繼續閱讀