前言
我们知道,在做了分库分表或其他规则的情况下,数据会分散到多个数据库实例上,这在管理上难免会有一些不便。针对这些不便,我们可以使用ShardingSphere-Proxy进行方便的管理。
一. Sharding-Proxy简介
Sharding-Proxy是一个分布式数据库中间件,定位为透明化的数据库代理端。作为开发人员可以完全把它当成数据库,而它具体的分片规则在Sharding-Proxy中配置。目前提供 MySQL 和 PostgreSQL(兼容 openGauss 等基于 PostgreSQL 的数据库)版本,它可以使用任何兼容 MySQL/PostgreSQL 协议的访问客户端(如:MySQL Command Client, MySQL Workbench, Navicat 等)操作数据,对 DBA 更加友好。
二. Proxy与Sharding-JDBC的区别
ShardingSphere-JDBC 是一个 Jar 包,底层通过重写 JDBC 组件完成 SQL 解析、路由、改写、执行等流程;需要在项目中添加对应功能的配置文件,对应用有一定侵入性;
ShardingSphere-Proxy 是一个进程服务,大部分情况下定位为辅助开发运维的效率工具。它将自己伪装为数据库,应用对接后对代码是无侵入的;对 SQL 的执行逻辑同 ShardingSphere-JDBC 一致,两者复用同一内核;
应用通过 ShardingSphere-JDBC 是直接操作数据库,相当于只有一次网络 IO;而应用连接 ShardingSphere-Proxy 是一次网络 IO,ShardingSphere-Proxy 再操作数据库,又会发生一次网络 IO;
ShardingSphere-JDBC 适用于 Java 开发的高性能的轻量级 OLTP 应用,ShardingSphere-Proxy 适用于 OLAP 应用以及对分片数据库进行管理和运维的场景。
三. Sharding-Proxy应用
3.1 数据分片SQL
如下sql是对t_order的分片,一共分为两片分别是t_order_0 、t_order_1
CREATE DATABASE /*!32312 IF NOT EXISTS*/db_0 /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */;
USE db_0;
DROP TABLE IF EXISTS t_order_0;
CREATE TABLE t_order_0 (
oid VARCHAR(100) COLLATE utf8_bin NOT NULL COMMENT '订单号(主键)',
money DOUBLE DEFAULT NULL COMMENT '支付金额',
status INT(11) DEFAULT NULL COMMENT '状态0,未支付1,已支付',
user_id VARCHAR(100) COLLATE utf8_bin,
PRIMARY KEY (oid)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
DROP TABLE IF EXISTS t_order_1;
CREATE TABLE t_order_1 (
oid VARCHAR(100) COLLATE utf8_bin NOT NULL COMMENT '订单号(主键)',
money DOUBLE DEFAULT NULL COMMENT '支付金额',
status INT(11) DEFAULT NULL COMMENT '状态0,未支付1,已支付',
user_id VARCHAR(100) COLLATE utf8_bin,
PRIMARY KEY (oid)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
3.2 Sharding-Proxy安装
前提条件:使用二进制发布包启动 Proxy,需要环境具备 Java JRE 8 或更高版本。
下载连接:https://archive.apache.org/dist/shardingsphere/4.1.1/
安装很简单,只需要解压即可。
3.3 Sharding-Proxy配置
解压安装完毕后,需要进行一些基本配置,配置信息如下:
3.3.1 服务配置
配置文件:conf/server.yaml
主要配置proxy的账户和权限
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
######################################################################################################
#
# If you want to configure orchestration, authorization and proxy properties, please refer to this file.
#
######################################################################################################
#
#orchestration:
# orchestration_ds:
# orchestrationType: registry_center,config_center,distributed_lock_manager
# instanceType: zookeeper
# serverLists: localhost:2181
# namespace: orchestration
# props:
# overwrite: false
# retryIntervalMilliseconds: 500
# timeToLiveSeconds: 60
# maxRetries: 3
# operationTimeoutMilliseconds: 500
#
authentication:
users:
root:
password: 123
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
3.3.2 分片配置
配置文件:conf/conf-*.yaml
主要配置分片信息,与sharding-jdbc的配置规则一模一样
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/db_0?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: oid
algorithmExpression: t_order_${oid % 2}
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
defaultTableStrategy:
none:
3.4 启动
启动脚本如下所示:
bin/start.bat
3.5 DBA运维测试
连接proxy
mysql -P3307 -uroot -p123
3.6 应用测试
业务开发只需要连接proxy数据库,因为proxy已经屏蔽了分片的底层逻辑。数据源配置文件如下:
spring:
datasource:
druid:
username: root
password: 123
url: jdbc:mysql://localhost:3307/sharding_db?useSSL=false&characterEncoding=utf-8
四. 小结
相信大家看完对 ShardingSphere-Proxy 有了更深入的认识。首先要明白 ShardingSphere-Proxy 的定位是协助开发运维的产品,掌握 ShardingSphere-JDBC 和 ShardingSphere-Proxy 有哪些区别,以及理解两者的优缺点和实现方式是怎么样的。在这个基础上去阅读两者的源码,也就更容易理解了。
》》》MySQL数据库教程,mysql安装到mysql高级一套通关