当前位置:首页 >热点 >Spring Cloud 微服务系列之 ShardingSphere 它提供了「数据库代理」功能

Spring Cloud 微服务系列之 ShardingSphere 它提供了「数据库代理」功能

2024-06-30 16:20:59 [百科] 来源:避面尹邢网

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

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

1. 简介

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

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

Spring Cloud 微服务系列之 ShardingSphere 它提供了「数据库代理」功能

2. 下载代理数据库

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

Spring Cloud 微服务系列之 ShardingSphere 它提供了「数据库代理」功能

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

Spring Cloud 微服务系列之 ShardingSphere 它提供了「数据库代理」功能

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_PERMITTEDprops:  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_dbdataSources:  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_dbdataSources:  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: 123456rules:- !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/linuxsh start.sh 3307# windowsstart.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

责任编辑:武晓燕 来源: 小虎哥的技术博客 微服务数据库应用程序

(责任编辑:百科)

    推荐文章
    热点阅读