Spring Cloud 微服务系列之 ShardingSphere-Proxy 数据库代理

数据库 其他数据库
ShardingSphere-Proxy是在数据库和应用程序之间起到了一个桥梁的作用,对于应用程序来说,它不需要感知ShardingSphere-Proxy的存在,依然可以使用原来的方式操作数据库。

1. 简介

ShardingSphere-Proxy是ShardingSphere分布式数据库中间件的一部分,它提供了「数据库代理」功能。通过引入ShardingSphere-Proxy,可以在无需改动应用程序代码的情况下,实现分库分表的数据库分片、读写分离、逻辑表达式分片等功能。ShardingSphere-Proxy独立运行于应用程序和数据库之间,充当数据库的代理,自动将请求路由至相应的数据库节点。

官网地址:https://shardingsphere.apache.org

2. 下载代理数据库

官网下载(5.4.0版本):https://shardingsphere.apache.org/document/current/cn/downloads/

官网下载很慢,网盘下载(推荐):「apache-shardingsphere-5.4.0-shardingsphere-proxy-bin.tar.gz」来自UC网盘分享https://drive.uc.cn/s/cc1882af6a9a4

3. 配置MySQL驱动

下载 mysql-connector-java-8.0.11.jar,并将其放入 ext-lib 或 lib 目录下。

mysql-connector-java-8.0.11.jar包下载地址:来自UC网盘分享https://drive.uc.cn/s/f9b1c5d7c0f64

4. 配置 server.yaml

conf目录下server.yaml配置文件,主要配置代理数据库的用户名、密码、权限。

  • 用户名 root
  • 密码 123456
  • 权限 ALL_PERMITTED
authority:
  users:
    - user: root
      password: 123456
  privilege:
    type: ALL_PERMITTED

props:
  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: false
  check-table-metadata-enabled: false

5. 配置 config-sharding.yaml

conf目录下sconfig-sharding.yaml配置文件,主要配置具体的分库分表规则:

  • 代理数据库名称 sharding_db。
  • 逻辑数据源 ds_0 指向 jdbc:mysql://127.0.0.1:3306/sharding_0。
  • 逻辑数据源 ds_1 指向 jdbc:mysql://127.0.0.1:3306/sharding_1。
  • company表的分片规则是id_inline,根据id取模。
  • product表没有配置分片规则,用默认分配规则,根据company_id取模。
  • permission表是广播表,插入(更新)数据的时候每张表都会插入(更新),读取的时候随机一张表读取。
  • 取模算法ds_$->{id % 2} 偶数在ds_0,奇数在ds_1。
databaseName: sharding_db

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/sharding_0?serverTimezone=UTC&useSSL=false
    username: root
    password: "123456"
  ds_1:
    url: jdbc:mysql://127.0.0.1:3306/sharding_1?serverTimezone=UTC&useSSL=false
    username: root
    password: "123456"

rules:
  - !SHARDING
    tables:
      company:
        actualDataNodes: ds_$->{0..1}.company
        databaseStrategy:
          standard:
            shardingColumn: id
            shardingAlgorithmName: id_inline
      product:
        actualDataNodes: ds_$->{0..1}.product
    defaultDatabaseStrategy:
      standard:
        shardingColumn: company_id
        shardingAlgorithmName: database_inline
    shardingAlgorithms:
      database_inline:
        type: INLINE
        props:
          algorithm-expression: ds_$->{company_id % 2}
      id_inline:
        type: INLINE
        props:
          algorithm-expression: ds_$->{id % 2}

  - !BROADCAST
    tables: # 广播表规则列表
      - permission

注意上面是url,而不是jdbcUrl,官方这么说的:

图片图片

否则启动代理数据库会出现如下异常:

Unable to find property 'jdbcUrl' on class: org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration

6. 配置 config-readwrite-splitting.yaml

conf目录下config-readwrite-splitting.yaml配置文件,主要配置数据库的读写分离。

往write_ds数据库写数据的时候会自动同步到read_ds_0、read_ds_1两个库中。读取数据的时候会随机从read_ds_0、read_ds_1选择一个数据源进行读取。

databaseName: readwrite-splitting_db

dataSources:
  write_ds:
    url: jdbc:mysql://127.0.0.1:3306/demo_write_ds?serverTimeznotallow=UTC&useSSL=false
    username: root
    password: 123456
  read_ds_0:
    url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_0?serverTimeznotallow=UTC&useSSL=false
    username: root
    password: 123456
  read_ds_1:
    url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_1?serverTimeznotallow=UTC&useSSL=false
    username: root
    password: 123456

rules:
- !READWRITE_SPLITTING
  dataSources:
    readwrite_ds:
      writeDataSourceName: write_ds
      readDataSourceNames:
        - read_ds_0
        - read_ds_1
  • 写数据库:write_ds
  • 读数据库:read_ds_0、read_ds_1

7. 执行sql脚本

创建sharding_0和sharding_1两个数据库。两个数据库完全一样,包含如下数据表:

  1. company 企业表,根据id分库
  2. product 商品表,根据企业idcompany_id分库
  3. permission 权限表,广播表不分库
CREATE DATABASE sharding_0 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

CREATE DATABASE sharding_1 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

CREATE TABLE `company`  (
  `id` bigint(20) NOT NULL COMMENT '主键id',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名称',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

CREATE TABLE `permission`  (
  `id` bigint(20) NOT NULL COMMENT '主键id',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名称',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

CREATE TABLE `product`  (
  `id` bigint(20) NOT NULL COMMENT '主键id',
  `company_id` bigint(20) NULL DEFAULT NULL COMMENT '公司id',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名称',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

8. 启动代理数据库

在bin目录下:

# mac/linux
sh start.sh 3307

# windows
start.bat 3307
  • 指定数据库端口 3307

启动日志位置:logs/skywalking-oap-server.log

9. 连接代理数据库

代理数据库对于开发人员来说与普通数据库的操作无异,既可通过命令行,也可通过可视化工具来进行连接和操作。

通过命令连接代理数据库:

mysql -h127.0.0.1 -P3307 -uroot -p123456

通过可视化工具连接代理数据库:

图片图片

10. 分库分表结果

1)company

在代理数据库company表上添加企业数据记录。

图片图片

偶数id的企业在sharding_0数据库,奇数id企业在sharding_1数据库。

图片图片

图片图片

2)product

在代理数据库product表上添加商品数据记录。

company_id为偶数的商品在sharding_0数据库,company_id为奇数的商品sharding_1数据库。保证了一个企业的商品全部在一个库里面。

图片图片

图片图片

3)permission

在代理数据库permission表上添加权限数据记录。

被代理的两个数据库的数据都一样。

图片图片

11. 总结

ShardingSphere-Proxy是在数据库和应用程序之间起到了一个桥梁的作用,对于应用程序来说,它不需要感知ShardingSphere-Proxy的存在,依然可以使用原来的方式操作数据库。也就是说,ShardingSphere-Proxy对于应用程序来说是透明的,不需要额外的代码实现或者调整。

图片图片

Spring Cloud 微服务系列 完整的代码在仓库的sourcecode/spring-cloud-demo目录下。

gitee(推荐):https://gitee.com/cunzaizhe/xiaohuge-blog

github:https://github.com/tigerleeli/xiaohuge-blog

责任编辑:武晓燕 来源: 小虎哥的技术博客
相关推荐

2022-04-07 18:49:56

项目场景数据库

2017-09-05 14:05:11

微服务spring clou路由

2024-02-06 18:05:54

微服务SpringCloud

2021-12-14 06:59:39

微服务Kubernetes架构

2018-06-01 23:08:01

Spring Clou微服务服务器

2019-10-21 16:54:48

数据库设计SQL

2017-06-26 09:06:10

Spring Clou微服务架构

2021-09-06 10:24:12

鸿蒙HarmonyOS应用

2023-12-19 09:33:40

微服务监控

2017-11-20 13:32:54

微服务数据库开发

2017-09-04 16:15:44

服务网关架构

2021-09-03 15:41:00

鸿蒙HarmonyOS应用

2011-03-03 11:07:57

Spring数据库访问ORM

2021-10-19 14:02:12

服务器SpringSecurity

2020-06-30 07:58:39

微服务Spring BootCloud

2017-12-20 15:37:39

Spring Clou微服务架构

2021-12-20 15:44:28

ShardingSph分布式数据库开源

2017-07-03 09:50:07

Spring Clou微服务架构

2017-08-09 15:50:47

Spring Clou微服务架构

2017-08-10 11:15:05

Spring Clou微服务架构
点赞
收藏

51CTO技术栈公众号