本来自SpringCloud H.SR7 + sharding-jdbc 4.1.1-读写分离(主从复制)之后继续讨论shardingshphere。
Sharding-Proxy是ShardingSphere的第二个产品。它定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供MySQL/PostgreSQL版本,它可以使用任何兼容MySQL/PostgreSQL协议的访问客户端(如:MySQL Command Client, MySQL Workbench, Navicat等)操作数据,对DBA更加友好。
向应用程序完全透明,可直接当做MySQL/PostgreSQL使用。
适用于任何兼容MySQL/PostgreSQL协议的的客户端。


Sharding-Proxy独立应用,使用安装服务,进行分库分表或者读写分离配置,启动使用。
通过官网首页http://shardingsphere.apache.org/ 的下载栏目进行下载安装包。下载完之后一定要进入lib文件目录把后缀名都改为.jar,可能是下载出问题或者是官方打包出问题,有部分包没有正确的后缀名结束。
Proxy启动
- 1.下载Sharding-Proxy的最新发行版。
- 2.如果使用docker,可以执行docker pull shardingsphere/sharding-proxy获取镜像。详细信息请参考Docker镜像。
- 3.解压缩后修改conf/server.yaml和以config-前缀开头的文件,如:conf/config-xxx.yaml文件,进行分片规则、读写分离规则配置. 配置方式请参考配置手册。
- 4.Linux操作系统请运行bin/start.sh,Windows操作系统请运行bin/start.bat启动Sharding-Proxy。如需配置启动端口、配置文件位置,可参考快速入门 进行启动。
- 5.使用任何PostgreSQL的客户端连接。如: psql -U root -h 127.0.0.1 -p 3307
在启动前还是先需要进行配置的,先打开server.yaml文件,在conf目录下面,然后出了server.yaml之外仔细看一下其他文件名:config-sharding.yaml,config-master_slave.yml,这些名字都非常明确了一个是做分库分表,一个是做主从复制。

然后把最后两段的yml配置的注释去掉,也就是如下代码所示:
authentication:
users:
root:
password: root #用户名和密码都是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.
# LOCAL: Proxy will run with LOCAL transaction.
# XA: Proxy will run with XA transaction.
# BASE: Proxy will run with B.A.S.E transaction.
proxy.transaction.type: LOCAL
proxy.opentracing.enabled: false
proxy.hint.enabled: false
query.with.cipher.column: true
sql.show: false
allow.range.query.with.inline.sharding: false
接下来我们打开config-sharding.yaml,仔细看文档的话,不难发现他会要你把驱动包拷贝到lib文件夹下面,所以我们首先干的就是把mysql的驱动包拷贝到lib下面。
然后继续改配置:
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://localhost:3306/edu_db_1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
course:
actualDataNodes: ds_0.course_$->{1..2}
tableStrategy:
inline:
shardingColumn: user_id
algorithmExpression: course_$->{user_id % 2 + 1}
keyGenerator:
type: SNOWFLAKE
column: cid
bindingTables:
- course
defaultTableStrategy:
none:
接下来双击start.up启动sharding-proxy,其默认端口是3307,接下来演示如何修改端口
cd C:\develop\server\apache-shardingsphere-4.1.1-sharding-proxy-bin\bin
start.bat 3308
这样就可以切换为3308端口

接下来通过sharding-proxy启动端口进行连接。
进入MYSQL的安装目录,注意是MYSQL哈,然后再进入bin目录,执行:
mysql -P3308 -uroot -p
接下来介绍读写分离
schemaName: master_slave_db
dataSources:
master_ds:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_master?serverTimezone=UTC&useSSL=false
username: root
password:
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_0:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false
username: root
password:
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false
username: root
password:
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds
masterDataSourceName: master_ds
slaveDataSourceNames:
- slave_ds_0
- slave_ds_1
这段其实就是直接摘自官方的配置没有进行修改,其实跟前面的读写分离差不多,那么久不准备继续重复啰嗦了。
本文介绍Sharding-Proxy的配置与使用方法,包括如何进行分库分表和读写分离配置,以及如何在不同操作系统下启动Sharding-Proxy。通过详细步骤,帮助读者掌握Sharding-Proxy的基本操作。
3398

被折叠的 条评论
为什么被折叠?



