- 1. ShardingSphere-Proxy基本介绍
- 2. ShardingSphere-Proxy安装
-
- 2.1 下载
- 2.2 解压
- 2.3 配置
-
- 2.3.1 server.yaml
- 2.3.2 config-sharding.yaml
- 2.3.3 启动ShardingSphere
- 2.3.4 连接
- 3. ShardingSphere-Proxy 分表
-
- 3.1 环境搭建
-
- 3.1.1 pom.xml
- 3.1.2 编写业务代码
- 3.1.3 配置文件
- 3.1.5 测试结果
- 4. 说明
- 5.坑
之前使用的ShardingSphere-JDBC的原理架构是这样的:对于每一个应用都需要配置Sharding-JDBC。
上面这样是不是和麻烦呢,来看下ShardingSphere-Proxy的原理图:是不是一目了然
没错,ShardingSphere-Proxy提供透明化的数据库代理端,而不用每个应用服务都要独立的配置ShardingSphere-JDBC
向应用程序完全透明,可直接当做MySQL使用
2. ShardingSphere-Proxy安装 2.1 下载下载ShardingSphere-Proxy最新版
https://shardingsphere.apache.org/document/current/cn/downloads/
2.2 解压把下载之后压缩文件,解压,启动bin目录启动文件就可以了,但需要做相关配置,暂时先不要启动
2.3 配置 2.3.1 server.yaml配置server.yaml,把下面的配置的#注释打开,改为符合自己的配置。
authentication: users: root: password: root sharding: password: sharding authorizedSchemas: sharding_db props: max-connections-size-per-query: 1 acceptor-size: 16 # The default value is available processors count * 2. executor-size: 16 # Infinite by default. proxy-frontend-flush-threshold: 128 # The default value is 128. proxy-transaction-type: LOCAL proxy-opentracing-enabled: false proxy-hint-enabled: false query-with-cipher-column: true sql-show: true check-table-metadata-enabled: false2.3.2 config-sharding.yaml
配置分表规则,config-sharding.yaml,把最下面MySQL的注释改为符合自己的配置
# 只配置分表 schemaName: sharding_db dataSourceCommon: username: root password: 123456 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1 maintenanceIntervalMilliseconds: 30000 dataSources: ds_0: url: jdbc:mysql://127.0.0.1:3306/ss_course_db_proxy_1?serverTimezone=UTC&useSSL=false rules: - !SHARDING tables: t_course: actualDataNodes: ds_${0}.t_course_${1..2} tableStrategy: standard: shardingColumn: cid shardingAlgorithmName: t_course_inline keyGenerateStrategy: column: cid keyGeneratorName: snowflake bindingTables: - t_course defaultDatabaseStrategy: standard: shardingColumn: user_id shardingAlgorithmName: database_inline defaultTableStrategy: none: shardingAlgorithms: database_inline: type: INLINE props: algorithm-expression: ds_0 t_course_inline: type: INLINE props: algorithm-expression: t_course_${cid % 2 + 1} keyGenerators: snowflake: type: SNOWFLAKE props: worker-id: 123
如果需要自己制定ShardingSphere-Proxy创建的数据库名称,需要自己手动改下上面配置文件中的authorizedSchemas、schemaName两个属性
2.3.3 启动ShardingSphere这地方要注意的是:如果后端连接MySQL数据库,需要手动下载MySQL对应版本的驱动包并将其放入lib目录下
启动ShardingSphere-Proxy服务,ShardingSphere-Proxy默认端口号3307。由于我之前配置从数据库的端口是3307,所以此处在启动的时候我把ShardingSphere-Proxy端口设为3308
连接方式和连接MySQL一样,使用MySQL命令行工具进行连接,并进行命令操作,看到只有一个库
在sharding_db数据库创建表,该代理层会根据分库分表规则自动在后端对应的分库中创建表
向表添加一条记录
回到本地3306端口实际数据库中,看到已经创建好了表和添加数据
环境说明:SpringBoot 2.5.7+MyBatisPlus+ShardingSphere-JDBC 5.0.0-alpha+Druid+MySQL 8.0
3.1.1 pom.xml<dependencies> <dependency> <groupId>org.springframework.boot关注打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?