8、ShardingSphere 之 Sharding-Proxy 實現分庫分表

1 創建兩個數據庫edudb1、edudb2

2 進入conf目錄配置conf-sharding.yaml

schemaName: sharding_db

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/edudb1?serverTimezone=UTC&useSSL=false
    username: root
    password: chengwen
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  ds_1:
    url: jdbc:mysql://127.0.0.1:3306/edudb2?serverTimezone=UTC&useSSL=false
    username: root
    password:
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50

shardingRule:
  tables:
    t_order:
      actualDataNodes: ds_${0..1}.t_order_${1..2}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_${order_id % 2 + 1}
      keyGenerator:
        type: SNOWFLAKE
        column: order_id
  bindingTables:
    - t_order
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds_${user_id % 2}
  defaultTableStrategy:
    none:

3 啓動Sharding-Proxy服務

進入sharding-proxy的bin目錄啓動服務

chengwen@Mchengwen bin % ./start.sh   
Starting the Sharding-Proxy ...
Please check the STDOUT file: /Users/chengwen/apache-shardingsphere-incubating-4.0.1-sharding-proxy-bin/logs/stdout.log

啓動日誌出現端口號3307 和ACTIVE表示成功

[INFO ] 11:10:49.004 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x4ebfb303] REGISTERED
[INFO ] 11:10:49.006 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x4ebfb303] BIND: 0.0.0.0/0.0.0.0:3307
[INFO ] 11:10:49.008 [nioEventLoopGroup-2-1] i.n.handler.logging.LoggingHandler - [id: 0x4ebfb303, L:/0.0.0.0:3307] ACTIVE

4 通過Navicat for MySQL 連接sharding-proxy 服務端

在這裏插入圖片描述

4.1 查看數據庫

show DATABASES;
sharding_db

4.2 向數據庫sharding_db中創建表

create table if not exists ds_0.t_order(
order_id bigint not null,
user_id int not null,
status varchar(50),
primary key(order_id)
);

4.3 向t_order表中添加數據

insert into t_order(order_id,user_id,status) values (11,1,'init');

4.4 查看添加的結果

select * from t_order;

在這裏插入圖片描述

5 打開本地3306數據庫,發現已經在對應的分庫創建好了對應的分表並添加好了數據

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章