一、官方相关文档介绍
二、安装
1、拉取官网镜像
docker pull apache/shardingsphere-proxy
2、获取/conf中的配置到本地路径,用于挂载
运行容器
docker run -d apache/shardingsphere-proxy
PS C:\Windows\system32> docker run -d apache/shardingsphere-proxy
cd2477174763df3f121326a89ba0d67f8cc0893417256e4e35c56dbe89239255
复制容器中conf中所有的配置文件到本地,用于后面得挂载。使用命令docker update 容器ID:容器中对应路径 本地路径
docker cp cd2477174763df3f121326a89ba0d67f8cc0893417256e4e35c56dbe89239255:/opt/shardingsphere-proxy/conf D:\docker\sharding-sphere-proxy
删除容器
docker rm -f cd2477174763df3f121326a89ba0d67f8cc0893417256e4e35c56dbe89239255
3、运行Docker容器
运行容器并挂载配置地址和扩展包地址
docker run --name sharding-sphere-proxy -d -v D:\docker\sharding-sphere-proxy\conf:/opt/shardingsphere-proxy/conf -v D:\docker\sharding-sphere-proxy\ext-lib:/opt/shardingsphere-proxy/ext-lib -e PORT=3308 -p 7009:3308 apache/shardingsphere-proxy:latest
PS C:\Windows\system32> docker run --name sharding-sphere-proxy -d -v D:\docker\sharding-sphere-proxy\conf:/opt/shardingsphere-proxy/conf -v D:\docker\sharding-sphere-proxy\ext-lib:/opt/shardingsphere-proxy/ext-lib -e PORT=3308 -p 7009:3308 apache/shardingsphere-proxy:latest
568560a9c957d8c9b7b15e33e83968e8ad5c064333730623ba93316b44937b97
三、实现数据分片(样例)
1、复制mysql依赖到扩展包中
下载地址:mysql-connector-java-5.1.47.jar或者mysql-connector-java-8.0.11.jar
下载并复制
2、配置文件说明,具体可看官网配置手册
3、权限配置(server.yaml)
rules:
- !AUTHORITY
users:
- root@%:root # <username>@<hostname>:<password>
- sharding@:sharding
provider:
type: ALL_PRIVILEGES_PERMITTED
4、数据分片配置(config-sharding.yaml)
sql脚本
CREATE DATABASE cyun_sharding_sphere_proxy CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE cyun_sharding_sphere_proxy;
-- auto-generated definition
create table t_sys_log_0
(
id varchar(32) not null comment '日志ID'
primary key,
application_name varchar(32) not null comment '服务名称',
log_type int(2) default 0 not null comment '日志类型(0-未知、1-登录日志、2-操作日志、3-定时任务日志)',
operate_type int(2) null comment '操作类型(0-未知、1-查询、2-添加、3-更新、4-删除、5-导入、6-导出)',
log_content varchar(1000) null comment '日志描述信息',
ip varchar(100) null comment 'IP',
method varchar(500) null comment '请求java方法',
request_url varchar(255) null comment '请求路径',
request_param longtext null comment '请求参数',
request_type varchar(10) null comment '请求类型',
cost_time bigint null comment '耗时(单位:毫秒)',
create_by varchar(32) null comment '创建人',
create_time datetime null comment '创建时间',
update_by varchar(32) null comment '更新人',
update_time datetime null comment '更新时间'
)
comment '系统日志表' charset = utf8mb4;
-- auto-generated definition
create table t_sys_log_1
(
id varchar(32) not null comment '日志ID'
primary key,
application_name varchar(32) not null comment '服务名称',
log_type int(2) default 0 not null comment '日志类型(0-未知、1-登录日志、2-操作日志、3-定时任务日志)',
operate_type int(2) null comment '操作类型(0-未知、1-查询、2-添加、3-更新、4-删除、5-导入、6-导出)',
log_content varchar(1000) null comment '日志描述信息',
ip varchar(100) null comment 'IP',
method varchar(500) null comment '请求java方法',
request_url varchar(255) null comment '请求路径',
request_param longtext null comment '请求参数',
request_type varchar(10) null comment '请求类型',
cost_time bigint null comment '耗时(单位:毫秒)',
create_by varchar(32) null comment '创建人',
create_time datetime null comment '创建时间',
update_by varchar(32) null comment '更新人',
update_time datetime null comment '更新时间'
)
comment '系统日志表' charset = utf8mb4;
配置config-sharding.yaml
schemaName: sharding_proxy_db
dataSources:
ds_0:
url: jdbc:mysql://101.101.101.101:7000/cyun_sharding_sphere_proxy?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !SHARDING
tables:
t_sys_log:
actualDataNodes: ds_0.t_sys_log_${0..1}
tableStrategy:
standard:
shardingColumn: log_type
shardingAlgorithmName: log_type_inline
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
bindingTables:
- t_sys_log
defaultDatabaseStrategy:
none:
defaultTableStrategy:
none:
shardingAlgorithms:
log_type_inline:
type: INLINE
props:
algorithm-expression: t_sys_log_${log_type % 2}
5、重启服务
四、使用数据库工具连接
使用DBeaver工具进行连接
五、问题总结
1、使用工具连接时报错
工具错误截图
sharding sphere proxy容器报错截图
问题原因: 创建连接时,未填写Sharding Sphere Proxy代理的数据库。
2、使用工具查看表结构报错
工具错误截图
sharding sphere proxy容器报错截图
该错误不影响使用。