一、什么shardingsphere-proxy
Sharding-Proxy是ShardingSphere的第二个产品。 它定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供MySQL版本,它可以使用任何兼容MySQL协议的访问客户端(如:MySQL Command Client, MySQL Workbench等)操作数据,对DBA更加友好。用户可以通过shardingsphere-proxy服务,模拟一个类似MySQL的数据服务,基于该服务进一步去操作实体的MySQL,将分库分表等比较繁琐的功能进行闭盒操作。
二、基于docker安装,并配置shardingsphere-proxy
- 部署说明
- 因为是中间件类型的软件,所以采用Docker部署,
docker-compose 便于编排。- ShardingSphere-Proxy作为代理,本质就是Java程序解析应用端的SQL并分发,需要根据自己的并发体量选择适当配置的机器。
- 脚本
- 先部署一个简单的服务以获取原生的配置文件
docker-compose up -d 起容器
之后可以删除容器,里面的yaml文件不够
拷贝镜像中的conf目录到数据卷
docker container create --name test01 apache/shardingsphere-proxy
docker container cp test01:/opt/shardingsphere-proxy/conf /docker/shardingproxy
docker container rm -f test01
- 目录说明
- conf:存放配置文件 ext-lib:
- ShardingSphere-Proxy的扩展类库,如数据库连接的jar包。
├── conf
│   ├── config-database-discovery.yaml
│   ├── config-encrypt.yaml
│   ├── config-readwrite-splitting.yaml
│   ├── config-shadow.yaml
│   ├── config-sharding.yaml
│   ├── logback.xml
│   └── server.yaml
├── docker-compose.yml
└── ext-lib
    └── mysql-connector-java-8.0.11.jar
- 使用docker-compose部署完整的服务
version: "3"
services:
  ShardingSphereProxy:
    image: apache/shardingsphere-proxy
    container_name: shardingsphere-proxy
    network_mode: "host"
    restart: always
    command: server /data
    ports:
      - 3307:3307
    volumes:
      - ./conf:/opt/shardingsphere-proxy/conf
      - ./ext-lib:/opt/shardingsphere-proxy/ext-lib
    environment:
        JVM_OPTS: "-Djava.awt.headless=true"
        TZ: Asia/Shanghai
- 补充配置文件
- 数据分片配置:config-sharding.yaml (shardig服务会扫描config*.yaml文件)
######################################################################################################
# 
# 用于配置:数据分片规则
# 
######################################################################################################
 
schemaName: data-center_db
 
dataSources:
  ds_0:
    url: jdbc:mysql://192.168.0.60:3306/data-center_0?serverTimezone=UTC&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_1:
    url: jdbc:mysql://192.168.0.60:3306/data-center_1?serverTimezone=UTC&useSSL=false
    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 
    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
  
  # 分片算法配置
  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
- 代理相关配置:server.yaml
######################################################################################################
# 
# 用于配置:数据接入迁移&弹性伸缩、分布式治理模式、权限、代理属性.
# 
######################################################################################################
 
#scaling:
#  blockQueueSize: 10000 # 数据传输通道队列大小
#  workerThread: 40 # 工作线程池大小,允许同时运行的迁移任务线程数
#  clusterAutoSwitchAlgorithm:
#    type: IDLE
#    props:
#      incremental-task-idle-minute-threshold: 30
#  dataConsistencyCheckAlgorithm:
#    type: DEFAULT
#
#mode:
#  type: Cluster
#  repository:
#    type: ZooKeeper
#    props:
#      namespace: governance_ds
#      server-lists: localhost:2181
#      retryIntervalMilliseconds: 500
#      timeToLiveSeconds: 60
#      maxRetries: 3
#      operationTimeoutMilliseconds: 500
#  overwrite: false
#
rules:
  - !AUTHORITY
    users:
    # shardingsphere-proxy服务的用户名及密码
      - root@%:root
      - sharding@:sharding
    provider:
      type: ALL_PRIVILEGES_PERMITTED
  - !TRANSACTION
    defaultType: XA
    providerType: Atomikos
 
props:
  max-connections-size-per-query: 1
  kernel-executor-size: 16  # Infinite by default.
  proxy-frontend-flush-threshold: 128  # The default value is 128.
#  proxy-opentracing-enabled: false
#  proxy-hint-enabled: false
  sql-show: true
#  check-table-metadata-enabled: false
#  show-process-list-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
  check-duplicate-table-enabled: true
#  sql-comment-parse-enabled: false
#  proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
#    # Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
#    # if client connections are more than proxy-frontend-netty-executor-size, especially executing slow SQL.
#  proxy-backend-executor-suitable: OLAP
#  proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
#  sql-federation-enabled: false
 
三、集成springboot服务
- 表结构
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='订单子表';

- navicat中配置sharding,注意端口3307
  
- 代码
Data
@TableName("data-center_db")//与代理的数据库名称一致
public class TOrder implements Serializable {
    private Long orderId;
    private Long userId;
    private Integer totalMoney;
}
@Mapper
@DS("sharding")//指定数据源
public interface OrderMapper extends BaseMapper<TOrder> {
}
spring:
  datasource:
    dynamic:
      seata: false
      # 设置默认的数据源或者数据源组,默认值即为 master
      primary: master
      datasource:
        # 主库数据源 @DS
        sharding:
          type: com.zaxxer.hikari.HikariDataSource
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://192.168.1.8:3307/data-center_db?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
          username: root
          password: root
 @Test
    public void test01(){
        Page<TOrder> page=new Page<>();
        page.setCurrent(3L);
        QueryWrapper<TOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("order_id");
        orderMapper.selectPage(page,queryWrapper);
        System.out.println(page.getTotal());
        page.getRecords().forEach(a-> System.out.println(a.getTotalMoney()));
    }
    @Test
    public void insert()
    {
        for(Long i =1L;i<5L;i++){
            TOrder tOrder=new TOrder();
            tOrder.setUserId(i);
            tOrder.setTotalMoney(100+Integer.parseInt(i+""));
            orderMapper.insert(tOrder);
        }
    }



















