概念
ShardingSphere-Proxy是一个独立的应用,需要安装服务,只需配置即可完成分库分表和读写分离配置。通过配置文件,使得开发者可以像使用mysql数据库一样使用底层的各种分表分库以及读写分离的内容。
配置文件介绍
server.xml,配置全局的信息
authentication:
users:
root: #用户名
password: root #密码
sharding:
password: sharding
authorizedSchemas: sharding_db #只能访问的逻辑数据库
props:
max.connections.size.per.query: 1
acceptor.size: 16 #用于设置接收客户端请求的工作线程数,默认是CPU核数*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 #默认为local事务
proxy.opentracing.enabled: false #是否开启链路追踪功能,默认不开启
query.with.cipher.column: true
sql.show: false #sql打印
config-sharding.yaml,配置数据源,数据库的分库分表
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/edu_db_1?serverTimezone=UTC&useSSL=false
username: root
password: 123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0}.t_order_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
bindingTables:
- t_order
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${0}
defaultTableStrategy:
none:
config-master_slave.yaml 读写分离配置
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,需要导入MySQL的驱动包,根据自己的版本导入。
小总结
1.ShardingSphere-JDBC:简化了对于分库分表以后数据库的操作,但是还得在java配置文件中配置。
2.ShardingSphere-proxy:将JDBC的配置独立开,形成一个新的服务。使得开发者像是操作一个数据库那样,底层帮我们去完成分库分表的CRUD操作等。