docker-compose 搭建 Sharding-Proxy 5.4.0 分库分表代理服务

感谢: 程序员一枚 提供搭建方式

项目地址: https://gitee.com/dromara/RuoYi-Cloud-Plus/tree/2.X/

1.在 mysql 创建两个库

创建两个库 data-center_0 data-center_1 分别执行如何sql

CREATE TABLE `t_order_0` (
  `order_id` bigint(20) UNSIGNED NOT NULL COMMENT '主键ID',
  `user_id` bigint(20) UNSIGNED NOT NULL COMMENT '用户ID',
  `total_money` int(10) UNSIGNED NOT NULL COMMENT '订单总金额',
  PRIMARY KEY (`order_id`),
  KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单总表';
 
CREATE TABLE `t_order_1` (
  `order_id` bigint(20) UNSIGNED NOT NULL COMMENT '主键ID',
  `user_id` bigint(20) UNSIGNED NOT NULL COMMENT '用户ID',
  `total_money` int(10) UNSIGNED NOT NULL COMMENT '订单总金额',
  PRIMARY KEY (`order_id`),
  KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单总表';
 
CREATE TABLE `t_order_item_0` (
  `order_item_id` bigint(20) UNSIGNED NOT NULL COMMENT '子订单ID',
  `order_id` bigint(20) UNSIGNED NOT NULL COMMENT '主键ID',
  `user_id` bigint(20) UNSIGNED NOT NULL COMMENT '用户ID',
  `money` int(10) UNSIGNED NOT NULL COMMENT '子订单金额',
  PRIMARY KEY (`order_item_id`),
  KEY `idx_order_id` (`order_id`) USING BTREE,
  KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单子表';
 
CREATE TABLE `t_order_item_1` (
  `order_item_id` bigint(20) UNSIGNED NOT NULL COMMENT '子订单ID',
  `order_id` bigint(20) UNSIGNED NOT NULL COMMENT '主键ID',
  `user_id` bigint(20) UNSIGNED NOT NULL COMMENT '用户ID',
  `money` int(10) UNSIGNED NOT NULL COMMENT '子订单金额',
  PRIMARY KEY (`order_item_id`),
  KEY `idx_order_id` (`order_id`) USING BTREE,
  KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单子表';
 

2.更改配置文件

官网所有配置文件下载地址: shardingsphere 配置文件

更改 config-sharding.yaml 配置文件内的数据库连接地址与用户名密码 并配置分库分表规则

######################################################################################################
#
# Here you can configure the rules for the proxy.
# This example is configuration of sharding rule.
#
######################################################################################################

databaseName: data-center_db

dataSources:
  ds_0:
    url: jdbc:mysql://localhost:3306/data-center_0?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_1:
    url: jdbc:mysql://localhost:3306/data-center_1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !SHARDING
 tables: # 数据分片规则配置
   t_order: # 订单逻辑表名称
     actualDataNodes: ds_${0..1}.t_order_${0..1}
     databaseStrategy: # 配置分库策略
       standard:
         shardingColumn: user_id
         shardingAlgorithmName: database_user_inline
     tableStrategy: # 分表策略
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_inline
     keyGenerateStrategy:
       column: order_id
       keyGeneratorName: snowflake
#      auditStrategy:
#        auditorNames:
#          - sharding_key_required_auditor
#        allowHintDisable: true
   t_order_item:  # 子订单逻辑表名称
     actualDataNodes: ds_${0..1}.t_order_item_${0..1}
     databaseStrategy: # 配置分库策略
       standard:
         shardingColumn: user_id
         shardingAlgorithmName: database_user_inline
     tableStrategy:  # 分表策略
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_item_inline
     keyGenerateStrategy:
       column: order_item_id
       keyGeneratorName: snowflake
 bindingTables:  # 绑定表规则列表
   - t_order,t_order_item
#  defaultDatabaseStrategy:
#    standard:
#      shardingColumn: user_id
#      shardingAlgorithmName: database_user_inline
#  defaultTableStrategy:
#    none:
#  defaultAuditStrategy:
#    auditorNames:
#      - sharding_key_required_auditor
#    allowHintDisable: true

 # 分片算法配置
 shardingAlgorithms:
   database_user_inline:
     type: INLINE
     props:
       algorithm-expression: ds_${user_id % 2}
   t_order_inline:  # 订单表分片算法名称
     type: INLINE
     props:
       algorithm-expression: t_order_${order_id % 2}
       allow-range-query-with-inline-sharding: true
   t_order_item_inline:  # 子订单表分片算法名称
     type: INLINE
     props:
       algorithm-expression: t_order_item_${order_id % 2}
       allow-range-query-with-inline-sharding: true

 # 分布式序列算法配置
 keyGenerators:
   snowflake:
     type: SNOWFLAKE
     props:
       worker-id: 1

#  auditors:
#    sharding_key_required_auditor:
#      type: DML_SHARDING_CONDITIONS

# - !BROADCAST
#  tables:
#    - t_address

更改 server.yaml 配置代理登录用户与权限和一些其他系统配置

######################################################################################################
#
# If you want to configure governance, authorization and proxy properties, please refer to this file.
#
######################################################################################################

# mode:
#  type: Cluster
#  repository:
#    type: ZooKeeper
#    props:
#      namespace: governance_ds
#      server-lists: localhost:2181
#      retryIntervalMilliseconds: 500
#      timeToLiveSeconds: 60
#      maxRetries: 3
#      operationTimeoutMilliseconds: 500

authority:
 users:
   - user: root@%
     password: root
   - user: sharding
     password: sharding
 privilege:
   type: ALL_PERMITTED

transaction:
 defaultType: XA
 providerType: Atomikos

sqlParser:
 sqlCommentParseEnabled: false
 sqlStatementCache:
   initialCapacity: 2000
   maximumSize: 65535
 parseTreeCache:
   initialCapacity: 128
   maximumSize: 1024

logging:
 loggers:
 - loggerName: ShardingSphere-SQL
   additivity: true
   level: INFO
   props:
     enable: false

sqlFederation:
 sqlFederationEnabled: false
 executionPlanCache:
   initialCapacity: 2000
   maximumSize: 65535

props:
 system-log-level: INFO
 max-connections-size-per-query: 1
 kernel-executor-size: 16  # Infinite by default.
 proxy-frontend-flush-threshold: 128  # The default value is 128.
 # sql-show is the same as props in logger ShardingSphere-SQL, and its priority is lower than logging rule
 sql-show: false
 check-table-metadata-enabled: false
   # Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
   # The default value is -1, which means set the minimum value for different JDBC drivers.
 proxy-backend-query-fetch-size: -1
 proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
 proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
 proxy-default-port: 3307 # Proxy default port.
 proxy-netty-backlog: 1024 # Proxy netty backlog.
 cdc-server-port: 33071 # CDC server port
 proxy-frontend-ssl-enabled: false
 proxy-frontend-ssl-cipher: ''
 proxy-frontend-ssl-version: TLSv1.2,TLSv1.3

其他配置文件根据实际需求自行修改即可

3.服务搭建

version: '3'

services:
  shardingproxy:
    image: apache/shardingsphere-proxy:5.4.0
    container_name: shardingsphere-proxy
    command: server /data
    ports:
      - "3307:3307"
    volumes:
      - /docker/shardingproxy/conf:/opt/shardingsphere-proxy/conf
      - /docker/shardingproxy/ext-lib:/opt/shardingsphere-proxy/ext-lib
    environment:
      - JVM_OPTS="-Djava.awt.headless=true"
    network_mode: "host"
docker-compose up -d shardingproxy

4.测试连接

测试连接连接自建的两个库

docker-compose 搭建 Sharding-Proxy 5.4.0 分库分表代理服务_第1张图片

测试连接 shardingproxy 代理服务 表信息显示正确

docker-compose 搭建 Sharding-Proxy 5.4.0 分库分表代理服务_第2张图片

最后运行 demo

运行 demo 提供的 controller 代码查看数据库内数据即可

代理服务连接方式: 把他当成一个 mysql 正常连接即可

@RequiredArgsConstructor
@RestController
@RequestMapping("/sharding")
public class TestShardingController {

    private final ShardingOrderMapper torderMapper;

    @GetMapping("/page")
    public R<Page<ShardingOrder>> page() {
        Page<ShardingOrder> page = new Page<>();
        page.setCurrent(3L);
        QueryWrapper<ShardingOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_id");
        torderMapper.selectPage(page,queryWrapper);
        //List records = page.getRecords();
        //System.out.println(page.getTotal());
//        for(ShardingOrder order : page.getRecords()){
//            System.out.print(order.getTotalMoney()+" ");
//        }
        return R.ok(page);
    }

    @GetMapping("/insert")
    public R<Void> insert() {
        for(Long i = 1L; i <= 100L; i++){
            ShardingOrder torder = new ShardingOrder();
            torder.setUserId(i);
            torder.setTotalMoney(100 + Integer.parseInt(i+""));
            torderMapper.insert(torder);
        }

        return R.ok("分库分表数据批量插入成功!");

    }

}

你可能感兴趣的:(数据库,docker,docker,sharding,分库分表)