百尺竿头更进一步 – Amazon Aurora 的读写能力扩展之 ShardingSphere-Proxy 篇

数据库
开源
Amazon Aurora
0
0
{"value":"### **1.前言**\n[Amazon Aurora](https://aws.amazon.com/cn/rds/aurora/?trk=cndc-detail) 是亚马逊云科技自研的一项关系数据库服务,它在提供和开源数据库 MySQL、PostgreSQL 的完好兼容性同时,也能够提供和商业数据库媲美的性能和可用性。性能方面,Aurora MySQL 能够支持到与开源标准 MySQL 同等配置下五倍的吞吐量,Aurora PostgreSQL 能够支持与开源标准 PostgreSQL 同等配置下三倍的吞吐量的提升。在扩展性的角度,Aurora 在存储与计算、横向与纵向方面都进行了功能的增强和创新。Aurora 支持多达128TB 的存储容量,而且支持10GB 为单位的存储层动态收缩。计算方面,Aurora 提供多个读副本的可扩展性配置支持一个区域内多达15个读副本的扩展,提供多主的架构来支持同一个区域内4个写节点的扩展,提供 Serverless [无服务器](https://aws.amazon.com/cn/serverless/?trk=cndc-detail)化的架构实例级别的秒级纵向扩展,提供全球数据库来实现数据库的低延迟跨区域扩展。\n\n随着用户数据量的增长,Aurora 已经提供了很好的扩展性,那是否可以进一步处理更多的数据量、支持更多的并发访问呢?您可以考虑利用分库分表的方式,来支持底层多个 Aurora 集群的配置。基于此,包含这篇博客在内的系列博客会进行相应的介绍,旨在为您进行分库分表时选择使用代理或者 JDBC 提供参考。\n\n本篇博客会聚焦如何使用 ShardingSphere-Proxy,一个开源的分库分表中间件工具,来进行数据库集群的构建,会涵盖分库分表、读写分离、动态配置等方面。\n\n### **2.ShardingSphere-Proxy 介绍**\n[Apache ShardingSphere](https://shardingsphere.apache.org/index_zh.html) 是一套开源的分布式数据库解决方案组成的生态圈,它由 JDBC、Proxy 和 Sidecar(规划中)这3款既能够独立部署,又支持混合部署配合使用的产品组成。\n\n作为中间件,ShardingSphere-Proxy 的定位是透明化的数据库代理端。它采用 Apache2.0协议,持续迭代版本,最新版本为5.1.0,目前支持 MySQL 和 PostgreSQL 版本。它对应用程序透明,兼容 MySQL/PostgreSQL 协议的客户端。MySQL 命令行 mysql,MySQL workbench 等都可以直接访问 ShardingSphere-Proxy。\n\nShardingSphere-Proxy 下层可以连接不同的数据库,这些数据库可以是同构也可以是异构的。用户可以有两种方式指定底层数据库的分库分表或者读写分离规则:1)根据 yaml 配置文件静态指定2)利用 ShardingSphere 提供的增强性的 DistSQL 语言来指定。因为 DistSQL 支持动态创建规则不需要重启 Proxy 本身,它成为 ShardingSphere-Proxy 未来发展的重点。\n\n作为数据库代理,是否能够提供连接池增强用户并发访问的连接处理是需要考量的一方面,ShardingSphere-Proxy 在添加数据源并进行初始化时,会支持为每个数据库配置一个 Hikari 连接池。Hikari 是业界广泛使用的连接池,对性能损耗较小,而且被 SpringBoot 采用为缺省连接池。ShardingSphere-Proxy 的连接池可以支持用户配置最大连接数、最大空闲时间以及缓存相关的信息等。除 Hikari 连接池外,ShardingSphere-Proxy 也支持其它连接池的配置。\n\n和现有 SQL 的语法兼容性也是用户衡量数据库代理的关键因素,因为这涉及到是否更改应用代码。以 MySQL 为例,ShardingSphere 支持大部分的 MySQL 语法,但也有少量不支持的语法,比如 optimize 表、资源组的管理、用户的创建和GRANT权限管理等。具体可以查阅 ShardingSphere 的最新文档。\n\n下面会分享我对 ShardingSphereProxy 连接 Aurora 的几个维度的实验测试:1)分库分表 2)动态扩展 3)读写分离 4)多表 join 5)故障恢复。\n\n### **3.环境构建**\n#### **3.1 Aurora 集群搭建**\n首先根据 [Aurora 集群创建指南](https://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/Aurora.CreateInstance.html)创建三套 Aurora MySQL集群,机型为 db.r5.2xlarge,每套集群有一个写节点一个读节点。\n\n![image.png](https://dev-media.amazoncloud.cn/8dee62e5764d46939df454eb4e120134_image.png)\n\n#### **3.2 ShardingSphere-Proxy搭建**\n在与 Aurora 相同的可用区下启动一台 EC2节点,机型为 r5.8xlarge. 然后在上面安装 ShardingSphere-Proxy。\n\n##### **3.2.1 下载安装包**\n直接下载二进制安装包,进行解压。下载最新版本5.1.0,它对 DistSQL 支持较好。\n\n```\\nwget https://dlcdn.apache.org/shardingsphere/5.1.0/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin.tar.gz\\ntar -xvf apache-shardingsphere-5.1.0-shardingsphere-proxy-bin.tar.gz\\n```\n\nSharingSphereProxy 自带的库里包含对 PostgreSQL 的 JDBC driver,但不包含 MySQL 的 driver。因为创建的集群是 MySQL,需要将 MySQL 的 JDBC driver 拷贝到 lib 目录。\n\n```\\nwget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar\\ncp mysql-connector-java-5.1.47.jar apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/lib/\\n```\n\n##### **3.2.2 配置 Proxy 的服务端**\n在 ShardingSphere-Proxy 的根目录下,有个配置文件目录为 conf,里面有一个文件是 server.yaml,用来配置 ShardingSphere-Proxy 自己作为代理对外提供服务的信息以及元信息存放等。下面是一个配置示例,里面配置了用户权限信息,特定属性信息,以及元信息以集群模式存放在 zookeeper 里。\n\n```\\nrules:\\n - !AUTHORITY\\n users: //访问Proxy的用户名和密码信息\\n - root@%:root\\n - sharding@:sharding\\n provider: //控制用户对schema的登陆权限\\n type: ALL_PRIVILEGES_PERMITTED\\n - !TRANSACTION //事务类型配置,支持本地事务、XA两阶段事务、BASE柔性事务\\n defaultType: XA\\n providerType: Atomikos\\n\\nprops: //特定属性配置\\n max-connections-size-per-query: 1\\n proxy-hint-enabled: true //为强制路由使用,默认值为false\\n\\nmode: //元信息存放的配置,shardingsphereProxy支持三种模式:内存、单机和集群\\n type: Cluster\\n repository:\\n type: ZooKeeper //可以设置为zookeeper、etcd等\\n props:\\n namespace: shardingproxy\\n server-lists: localhost:2181\\n retryIntervalMilliseconds: 500\\n timeToLiveSeconds: 60\\n maxRetries: 3\\n operationTimeoutMilliseconds: 500\\n overwrite: false\\n```\n\n#### **3.3 启动 Proxy**\n直接在 ShardingSphereProxy 根目录下的 bin 对应着启动和停止脚本。运行时的日志在目录 logs 下。启动 Proxy\n\n```\\nbin/start.sh\\nbin/stop.sh\\n```\n\n#### **3.4 验证连接**\n如无特殊配置,ShardingSphereProxy 默认使用3307端口。使用3.2.2中配置的用户名和密码登录 proxy。在 EC2上运行 mysql 命令行工具进行连接,连接成功。注意这里没有任何数据库,因为我们没有使用 YAML 配置文件预先配置数据源。\n\n```\\n[ec2-user@ip-111-22-3-123 bin]\$ mysql -h 127.0.0.1 -uroot --port 3307 -proot\\nWelcome to the MariaDB monitor. Commands end with ; or \\\\g.\\nYour MySQL connection id is 1\\nServer version: 5.7.22-ShardingSphere-Proxy 5.1.0 \\n\\nCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.\\n\\nType 'help;' or '\\\\h' for help. Type '\\\\c' to clear the current input statement.\\n\\nMySQL [(none)]> show databases;\\nEmpty set (0.01 sec)\\n```\n\n### **4.功能测试**\n#### **4.1 DistSQL 创建分片规则和数据分片测试**\n本节来验证 ShardingSphere 的基本的分库分表能力。ShardingSphereProxy 支持两种方式创建分片规则和读写分离规则,YAML 和 DistSQL。DistSQL 扩展了 SQL 语法,可以支持在线创建数据源、创建和更改建表规则,较为灵活,本文只介绍 DistSQL 的用例。\n\n##### **4.1.1 创建数据库**\n连接到 ShardingSphere-Proxy,去创建数据库,作为逻辑的分布式数据库。\n\n```\\nMySQL [(none)]> create database distsql_sharding_db;\\nQuery OK, 0 rows affected (0.90 sec)\\n```\n\n在各个 Aurora 集群上创建数据库,作为数据库源进行连接。其中,rshard1,rshard2,rshard3是我自己定义的连接 Aurora 数据库的alias。\n\n```\\nalias rshard1=’mysql -h \$dbname -u\$username -p\$password’\\n```\n\n\n```\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard1 -e \\"create database dist_ds\\";\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard2 -e \\"create database dist_ds;\\"\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard3 -e \\"create database dist_ds;\\"\\n```\n\n#### **4.1.2 创建数据源**\n在 ShadingSphereProxy 中运行下面 DistSQL 语句创建3个数据源,分别指向3个不同 Aurora 集群\n\n```\\nMySQL [distsql_sharding_db]> add resource ds_0(url=\\"jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/dist_ds?serverTimezone=UTC&useSSL=false\\",user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.03 sec)\\n\\nMySQL [distsql_sharding_db]> add resource ds_1(url=\\"jdbc:mysql://aurora-2-07-7-shard2.cluster-12345678.us-east-1.rds.amazonaws.com:3306/dist_ds?serverTimezone=UTC&useSSL=false\\",user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.06 sec)\\n\\nMySQL [distsql_sharding_db]> add resource ds_2(url=\\"jdbc:mysql://aurora-2-07-7-shard3.cluster-12345678.us-east-1.rds.amazonaws.com:3306/dist_ds?serverTimezone=UTC&useSSL=false\\",user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.05 sec)\\n```\n\n#### **4.1.3 创建分片规则**\n这里指明t_order表的分片规则,注意分片规则的表名和后续要创建的表表名一致。具体规则为:对底层的3个数据源(Aurora 集群)按照 order_id 对表进行 hash 分片,分成6份。另外,对 order_id 采用值自动生成的策略,采用策略为 snowflake 算法。ShardingSphere 支持两种分布式主键生成策略:UUID 和雪花算法 SNOWFLAKE。使用雪花算法生成的主键,二进制表示形式包含4部分,从高位到低位分表为:1bit 符号位、41bit 时间戳位、10bit 工作进程位以及12bit 序列号位。在 ShardingSphereProxy 中运行下面 DistSQL 语句建立分片规则:\n\n```\\nMySQL [distsql_sharding_db]> CREATE SHARDING TABLE RULE t_order(\\n→ RESOURCES(ds_0,ds_1, ds_2),\\n→ SHARDING_COLUMN=order_id,\\n→ TYPE(NAME=hash_mod,PROPERTIES(\\"sharding-count\\"=6)),\\n→ KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME=snowflake))\\n→ );\\nQuery OK, 0 rows affected (0.02 sec)\\n```\n\n##### **4.1.4 建表**\n建表语句和普通 MySQL 建表语句一致。在 ShardingSphereProxy 中运行下面语句建表:\n\n```\\nMySQL [distsql_sharding_db]> CREATE TABLE `t_order` ( `order_id` bigint NOT NULL, `user_id` int NOT NULL, `status` varchar(45) DEFAULT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\\n -> ;\\nQuery OK, 0 rows affected (0.22 sec)\\n```\n在 ShardingSphereProxy上 查看表的状态。\n```\\nMySQL [distsql_sharding_db]> show tables;\\n+-------------------------------+------------+\\n| Tables_in_distsql_sharding_db | Table_type |\\n+-------------------------------+------------+\\n| t_order | BASE TABLE |\\n+-------------------------------+------------+\\n1 row in set (0.00 sec)\\n```\n\n分别连接到3个 Aurora 集群上查看表是否自动创建。可以看到每个底层数据库集群上都创建了两张表,一共是6张表。而且表名是以“t_oder_”数字排序的\n\n```\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e \\"show tables;\\"\\n+-------------------+\\n| Tables_in_dist_ds |\\n+-------------------+\\n| t_order_0 |\\n| t_order_3 |\\n+-------------------+\\n[ec2-user@ ip-111-22-3-123 bin ]\$ rshard2 -Ddist_ds -e \\"show tables;\\"\\n+-------------------+\\n| Tables_in_dist_ds |\\n+-------------------+\\n| t_order_1 |\\n| t_order_4 |\\n+-------------------+\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard3 -Ddist_ds -e \\"show tables;\\"\\n+-------------------+\\n| Tables_in_dist_ds |\\n+-------------------+\\n| t_order_2 |\\n| t_order_5 |\\n+-------------------+\\n```\n\n##### **4.1.5 插入和查找数据**\n在 ShardingSphere-Proxy 中插入并查找数据,数据可以正常插入和查找成功。在 ShardingSphere-Proxy 中运行:\n\n```\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (1, 'ok');\\ninsert into t_order(user_id, status) values (2, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (2, 'abc');\\ninsert into t_order(user_id, status) values (3, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (3, 'abc');\\ninsert into t_order(user_id, status) values (4, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (4, 'abc');\\ninsert into t_order(user_id, status) values (5, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (5, 'abc');\\ninsert into t_order(user_id, status) values (6, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (6, 'abc');\\ninsert into t_order(user_id, status) values (7, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (7, 'abc');\\ninsert into t_order(user_id, status) values (8, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (8, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order(user_id, status) values (9, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]> select * from t_order;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161915748353 | 2 | abc |\\n| 708700161995440128 | 5 | abc |\\n| 708700169725542400 | 9 | abc |\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n| 708700161970274305 | 4 | abc |\\n| 708700162016411649 | 6 | abc |\\n| 708700162058354689 | 8 | abc |\\n+--------------------+---------+--------+\\n9 rows in set (0.01 sec)\\n```\n\n去各个 Aurora 集群中查找子表插入的数据,可以看到在 Proxy 插入的9条记录被打散到底层的6张表中。因为 order_id 为 snowflake 算法生成而数据量比较小,这里的数据并不均匀。\n\n```\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e \\"select * from t_order_0;\\"\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e \\"select * from t_order_3;\\"\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161915748353 | 2 | abc |\\n+--------------------+---------+--------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e \\"select * from t_order_1;\\"\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e \\"select * from t_order_4;\\"\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161995440128 | 5 | abc |\\n| 708700169725542400 | 9 | abc |\\n+--------------------+---------+--------+\\n[ec2-user@111-22-3-123 bin]\$ rshard3 -Ddist_ds -e \\"select * from t_order_2;\\"\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n+--------------------+---------+--------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard3 -Ddist_ds -e \\"select * from t_order_5;\\"\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161970274305 | 4 | abc |\\n| 708700162016411649 | 6 | abc |\\n| 708700162058354689 | 8 | abc |\\n+--------------------+---------+--------+</code></pre></div>\\n```\n\n上述实验验证了 ShardingSphere-Proxy 具有创建逻辑库、连接数据源、创建分片规则、创建逻辑表时会自动在底层数据库上创建子表、能够执行查询的分发以及聚合能力。\n\n#### **4.2 动态伸缩验证(在线扩展分片)**\n本节来验证 ShardingSphere-Proxy 是否具有动态更改表的分片规则的能力。\n\nShardingSphere-Proxy 提供在线更改分片规则的能力,但是如果子表已经按照之前的规则创建成功,则不会有新的子表随着分片数目的增多被创建出来,也不会有原来的子表随着分片数目的减少而被删除。所以需要手动在底层分片数据库上创建表名和迁移数据。\n\n将4.1节里的表的分片数从6调高到9,修改分片规则本身能够成功,但是后续查找会出错,因为没有新的子表创建出来。在 ShardingSphere-Proxy 上运行下面 DistSQL:\n\n```\\nMySQL [distsql_sharding_db]> alter SHARDING TABLE RULE t_order(\\n -> RESOURCES(ds_0,ds_1, ds_2),\\n -> SHARDING_COLUMN=order_id,\\n -> TYPE(NAME=hash_mod,PROPERTIES(\\"sharding-count\\"=9)),\\n -> KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME=snowflake))\\n -> );\\nQuery OK, 0 rows affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]> select * from t_order;\\nERROR 1146 (42S02): Table 'dist_ds.t_order_6' doesn't exist\\n```\n\n如果此时在子集群上分别创建好对应的子表,再在 ShardingSphere-Proxy 上查找就不会再出错。连接到3个 Aurora 集群,手动创建子表\n\n```\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e \\"create table t_order_6(order_id bigint not null, user_id int not null, status varchar(45) default null, primary key(order_id)) engine=innodb default charset=utf8mb4; \\"\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e \\"create table t_order_7(order_id bigint not null, user_id int not null, status varchar(45) default null, primary key(order_id)) engine=innodb default charset=utf8mb4; \\"\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard3 -Ddist_ds -e \\"create table t_order_8(order_id bigint not null, user_id int not null, status varchar(45) default null, primary key(order_id)) engine=innodb default charset=utf8mb4; \\"\\n```\n\nProxy 查找整个逻辑表不再报错。在 ShardingSphere-Proxy 上运行下面 SQL:\n\n```\\nMySQL [distsql_sharding_db]> select * from t_order;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161915748353 | 2 | abc |\\n| 708700161995440128 | 5 | abc |\\n| 708700169725542400 | 9 | abc |\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n| 708700161970274305 | 4 | abc |\\n| 708700162016411649 | 6 | abc |\\n| 708700162058354689 | 8 | abc |\\n+--------------------+---------+--------+\\n9 rows in set (0.01 sec)\\n```\n\n如果有新的数据插入,会按照新的分片规则进行到子表的映射。在 ShardingSphere-Proxy 上查看 SQL 语句的查询计划:\n\n```\\nMySQL [distsql_sharding_db]> preview insert into t_order values(7, 100, 'new');\\n+------------------+---------------------------------------------+\\n| data_source_name | sql |\\n+------------------+---------------------------------------------+\\n| ds_1 | insert into t_order_7 values(7, 100, 'new') |\\n+------------------+---------------------------------------------+\\n1 row in set (0.00 sec)\\n\\nMySQL [distsql_sharding_db]> insert into t_order values(7, 100, 'new');\\nQuery OK, 1 row affected (0.00 sec)\\n```\n\n登录到 Aurora 子集群上查看子表,可以看到数据已经成功插入。\n\n```\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e \\"select * from t_order_7;\\"\\n+----------+---------+--------+\\n| order_id | user_id | status |\\n+----------+---------+--------+\\n| 7 | 100 | new |\\n+----------+---------+--------+\\n```\n\n再来看下在线减少分片的情况。如果将分片数目调小,比如调到3,表里的已有数据不会被迁移,查找整张表时只能拿到部分数据。在 ShardingSphere-Proxy 上运行下面 DistSQL 和 SQL 语句:\n\n```\\nMySQL [distsql_sharding_db]> alter SHARDING TABLE RULE t_order(\\n -> RESOURCES(ds_0,ds_1, ds_2),\\n -> SHARDING_COLUMN=order_id,\\n -> TYPE(NAME=hash_mod,PROPERTIES(\\"sharding-count\\"=3)),\\n -> KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME=snowflake))\\n -> );\\nQuery OK, 0 rows affected (0.02 sec)\\nMySQL [distsql_sharding_db]> select * from t_order;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n+--------------------+---------+--------+\\n3 rows in set (0.00 sec)\\n```\n\n经过上面验证,我们的结论是 ShardingSphereProxy 的分片规则是可以在线更改的,但子表的创建和数据的重新分布需要手动去完成。\n\n\n#### **4.3 绑定表和广播表的测试**\n本节来验证 ShardingSphere-Proxy 对于多表 join 的支持。尽管 OLTP 的数据库中的操作通常较为简单,但也有可能会涉及到多表 join 的情况。ShardingSphereProxy 针对多表 join 的优化有支持绑定表和广播表。如果两张表是绑定表而且 join 时采用的是 shard key,可以进行两张表的 join。广播表通过把小表复制到各个节点,可以实现大表和小表的快速 join。\n\n##### **4.3.1 绑定表**\nShardingSphereProxy 的绑定表可以通过 DistSQL 里的 CREATE SHARDING BINDING TABLE RULES 来绑定两张表。这里以4.1节中提到的 t_order 表和新创建的一张表 t_order_item 为例进行展开。\n\n连接到 ShardingSphere-Proxy 上运行下面 DistSQL 和 SQL 语句。\n\n```\\nMySQL [distsql_sharding_db]> CREATE SHARDING TABLE RULE t_order_item(\\n -> RESOURCES(ds_0,ds_1, ds_2),\\n -> SHARDING_COLUMN=order_id,\\n -> TYPE(NAME=hash_mod,PROPERTIES(\\"sharding-count\\"=6)));\\nQuery OK, 0 rows affected (0.04 sec)\\n\\nMySQL [distsql_sharding_db]> CREATE TABLE `t_order_item` ( `order_id` bigint NOT NULL, `item_id` int NOT NULL, `name` varchar(45) DEFAULT NULL, PRIMARY KEY (`item_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;\\nQuery OK, 0 rows affected (0.08 sec)\\n```\n\n创建了binding rule以后,查看join计划,我们看到join下推到对应子表和子表的join上。在ShardingSphere-Proxy上运行:\n\n```\\nMySQL [distsql_sharding_db]> CREATE SHARDING BINDING TABLE RULES (t_order,t_order_item);\\nQuery OK, 0 rows affected (0.04 sec)\\n\\nMySQL [distsql_sharding_db]> preview select * from t_order, t_order_item where t_order.order_id=t_order_item.order_id;\\n+------------------+------------------------------------------------------------------------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------------------------------------------------------------------------+\\n| ds_0 | select * from t_order_0, t_order_item_0 where t_order_0.order_id=t_order_item_0.order_id |\\n| ds_0 | select * from t_order_3, t_order_item_3 where t_order_3.order_id=t_order_item_3.order_id |\\n| ds_1 | select * from t_order_1, t_order_item_1 where t_order_1.order_id=t_order_item_1.order_id |\\n| ds_1 | select * from t_order_4, t_order_item_4 where t_order_4.order_id=t_order_item_4.order_id |\\n| ds_2 | select * from t_order_2, t_order_item_2 where t_order_2.order_id=t_order_item_2.order_id |\\n| ds_2 | select * from t_order_5, t_order_item_5 where t_order_5.order_id=t_order_item_5.order_id |\\n+------------------+------------------------------------------------------------------------------------------+\\n6 rows in set (0.01 sec)\\n```\n\n##### **4.3.2 广播表**\n广播表是指每张表在每个库里都有一个完整的备份,可以通过 CREATE SHARDING BROADCAST TABLE RULES 来指定。\n\n```\\nMySQL [distsql_sharding_db]> CREATE SHARDING BROADCAST TABLE RULES (t_user);\\nQuery OK, 0 rows affected (0.03 sec)\\n\\nMySQL [distsql_sharding_db]> create table t_user (user_id int, name varchar(100));\\nQuery OK, 0 rows affected (0.04 sec)\\n```\n\n登录到各个 shard Aurora 集群查看创建的表。可以看到与分片表的子表名末尾有数字序号不同的是 ,广播表对应的每个库上的名字是相同的,就是逻辑表名本身。\n\n```\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -D dist_ds -e \\"show tables like '%user%';\\"\\n+----------------------------+\\n| Tables_in_dist_ds (%user%) |\\n+----------------------------+\\n| t_user |\\n+----------------------------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -D dist_ds -e \\"show tables like '%user%';\\"\\n+----------------------------+\\n| Tables_in_dist_ds (%user%) |\\n+----------------------------+\\n| t_user |\\n+----------------------------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard3 -D dist_ds -e \\"show tables like '%user%';\\"\\n+----------------------------+\\n| Tables_in_dist_ds (%user%) |\\n+----------------------------+\\n| t_user |\\n+----------------------------+\\n```\n\n在 ShardingSphereProxy 中运行广播表和其它表的 join,采用的是本地 join 的方式。\n\n```\\nMySQL [distsql_sharding_db]> preview select * from t_order, t_user where t_order.user_id=t_user.user_id;\\n+------------------+------------------------------------------------------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------------------------------------------------------+\\n| ds_0 | select * from t_order_0, t_user where t_order_0.user_id=t_user.user_id |\\n| ds_0 | select * from t_order_3, t_user where t_order_3.user_id=t_user.user_id |\\n| ds_1 | select * from t_order_1, t_user where t_order_1.user_id=t_user.user_id |\\n| ds_1 | select * from t_order_4, t_user where t_order_4.user_id=t_user.user_id |\\n| ds_2 | select * from t_order_2, t_user where t_order_2.user_id=t_user.user_id |\\n| ds_2 | select * from t_order_5, t_user where t_order_5.user_id=t_user.user_id |\\n+------------------+--------\\n```\n\n上面实验验证了 ShardingSphere-Proxy 是可以支持两张绑定表的 join,以及广播表和分片表的 join 的。对于非绑定的两张分片表的join,ShardingSphere-Proxy 有一个 Federation 的功能是在支持的,但还不是很成熟,建议后续持续关注。\n\n#### **4.4 读写分离功能验证**\n本节来验证 ShardingSphere-Proxy 对于读写分离的支持。随着业务增长,写和读的负载分别在不同的数据库节点上能够有效提供整个数据库集群的处理能力。Aurora 通过读/写的 endpoint 可以满足用户写和强一致性读的需求,只读的 endpoint 可以满足用户非强一致性读的需求。Aurora 的读写延迟在毫秒级别,比 MySQL 基于 binlog 的逻辑复制要低得多,所以有很多负载是可以直接打到只读 endpoint 的。\n\nShardingSphereProxy 提供的读写分离的特性可以进一步可以封装 Aurora 的读/写端点和只读端点。用户可以直接连接到 Proxy 的端点,即可进行自动的读写分离。ShardingSphereProxy 对特殊情况的处理逻辑是:1)同一线程且同一数据库连接内,如果有写入操作,则后续的读操作均从主库读取 2)可以通过 Hint 的机制强制把读请求发到写节点(主库)。下面会以 Aurora3个集群中的第一个集群来验证 ShardingSphere-Proxy 读写分离的能力。\n\n##### **4.4.1 查看 Aurora 集群读/写端点和只读端点**\nAurora 集群有两个端点,写的端点和读的端点。\n\n![image.png](https://dev-media.amazoncloud.cn/81c9ce32be674804ac2f0a47b62285eb_image.png)\n\n##### **4.4.2 在 Aurora 集群中创建数据库**\n连接到 Aurora 集群中运行:\n\n```\\n[ec2-user@ip-111-22-3-123 ~]\$ rdbw -e \\"create database wr_ds;\\"\\n```\n\n##### **4.4.3 数据源配置**\n在 ShardingSphere-Proxy 上创建数据源,写的数据源指向 Aurora 的读写 endpoint,读的数据源指向 Aurora 的只读 endpoint。注意:对域名的情况,ShardingSphereProxy 只支持通过 url 的方式创建数据源,尚未支持通过 HOST、Port 的方式。连接到 ShardingSphere-Proxy 上创建逻辑数据库 distsql_rwsplit_db 并在改数据库中添加数据源:\n\n```\\nMySQL [(none)]> create database distsql_rwsplit_db;\\nQuery OK, 0 rows affected (0.02 sec)\\nMySQL [(none)]> use distsql_rwsplit_db;\\nDatabase changed\\nMySQL [distsql_rwsplit_db]> add resource write_ds(url=\\"jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&useSSL=false\\",user=admin,password=12345678), read_ds(url=\\"jdbc:mysql://aurora-2-07-7-shard1.cluster-ro-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&useSSL=false\\",user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.08 sec)\\n```\n\n##### **4.4.4 读写分离规则配置**\n创建读写分离规则,写请求发到写的数据源,读请求发到读的数据源。与分库分表规则要求 RULE 后面必须是表名不同的是,这里的 RULE 后面跟的是数据源的名字,适用于在这个数据库里创建的所有的表。在 ShardingSphere-Proxy 上运行下面 DistSQL 语句:\n\n```\\nMySQL [distsql_ rwsplit_db]> CREATE READWRITE_SPLITTING RULE wr_ds (\\n -> WRITE_RESOURCE=write_ds,\\n -> READ_RESOURCES(read_ds),\\n -> TYPE(NAME=random)\\n -> );\\nQuery OK, 0 rows affected (0.36 sec)\\n```\n\n##### **4.4.5 建表**\n创建一张普通表,建表语句和 MySQL 建表语句一致。在 ShardingSphere-Proxy 上运行下面 SQL 语句:\n\n```\\nMySQL [distsql_ rwsplit_db]> create table wr_table (a int, b int, c varchar(20));\\nQuery OK, 0 rows affected (0.17 sec)\\n```\n\n##### **4.4.6 检查读写分离是否实现**\n在 ShardingSphere-Proxy 上运行下面语句查看查询计划,查看语句是发送到底层哪个数据源。可以看到:写请求发送到写节点,读请求会发送到读写点。\n\n```\\nMySQL [distsql_rwsplit_db]> preview insert into wr_table values(1,1,'ab');\\n+------------------+---------------------------------------+\\n| data_source_name | sql |\\n+------------------+---------------------------------------+\\n| write_ds | insert into wr_table values(1,1,'ab') |\\n+------------------+---------------------------------------+\\n1 row in set (0.10 sec)\\nMySQL [distsql_rwsplit_db]> preview select * from wr_table;\\n+------------------+------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------+\\n| read_ds | select * from wr_table |\\n+------------------+------------------------+\\n1 row in set (0.02 sec)\\n```\n\n运行一个脚本来多次操作,再去Aurora集群指标监控中去验证。该脚本是一个循环,运行1000次,每次会插入一条记录,并查找表的记录总条数。\n\n```\\n[ec2-user@ip-111-22-3-123 shardingproxy]\$ cat testReadWrite.sh \\n#!/bin/bash\\nn=1\\nwhile [ \$n -le 1000 ]\\ndo\\n mysql -h 127.0.0.1 -uroot --port 3307 -proot -Ddistsql_rwsplit_db -e \\"insert into wr_table values(\$n,\$n,'ok');\\"\\n mysql -h 127.0.0.1 -uroot --port 3307 -proot -Ddistsql_rwsplit_db -e \\"select count(*) from wr_table;\\"\\n let n++\\ndone \\n```\n\n查看 Aurora 集群的写节点和读节点的读写延迟,可以看到写延迟只在写节点上发生,读延迟只在读节点上发生。说明读写分离规则生效。\n\n![image.png](https://dev-media.amazoncloud.cn/b172db7c626e435dab235cf3a26c3e33_image.png)\n\n尽管 Aurora 的写和读节点之间的复制延迟很低在毫秒级别,但某些应用还是会有强一致性的需求,即要求写后立刻可以读。这时候,可以采用强制将读请求发送到写节点的方式。ShardingSphereProxy 通过 hint 的方式来支持。首先需要在前面提到的 conf/server.yaml 里添加一个属性 proxy-hint-enabled: true。然后在连接中显式设置 readwrite_splitting hint source 值为 write 来开启强制路由到写节点通过设置值为 auto 或者 clear hint 可以采用默认的规则。readwrite_splitting hint source 可以在 session 级别生效。\n\n在 ShardingSphere-Proxy 上依次运行下面语句。可以看到默认的读请求是发送到读节点,将 readwrite_splitting hint source 设置为 write 以后,会发送到写节点,再设成 auto,可以发回至读写点。\n\n```\\nMySQL [distsql_rwsplit_db]> preview select count(*) from wr_table;\\n+------------------+-------------------------------+\\n| data_source_name | sql |\\n+------------------+-------------------------------+\\n| read_ds | select count(*) from wr_table |\\n+------------------+-------------------------------+\\n1 row in set (0.01 sec)\\n\\nMySQL [distsql_rwsplit_db]> set readwrite_splitting hint source = write;\\nQuery OK, 0 rows affected (0.00 sec)\\n\\nMySQL [distsql_rwsplit_db]> preview select count(*) from wr_table;\\n+------------------+-------------------------------+\\n| data_source_name | sql |\\n+------------------+-------------------------------+\\n| write_ds | select count(*) from wr_table |\\n+------------------+-------------------------------+\\n1 row in set (0.01 sec)\\nMySQL [distsql_rwsplit_db]> set readwrite_splitting hint source = auto;\\nQuery OK, 0 rows affected (0.00 sec)\\n\\nMySQL [distsql_rwsplit_db]> preview select count(*) from wr_table;\\n+------------------+-------------------------------+\\n| data_source_name | sql |\\n+------------------+-------------------------------+\\n| read_ds | select count(*) from wr_table |\\n+------------------+-------------------------------+\\n1 row in set (0.00 sec)\\n```\n\n另外不使用 YAML 文件更改的方式是直接在 DistSQL 里先后设置两个变量 proxy_hint_enabled 和 readwrite_splitting hint source。\n\n```\\nMySQL [distsql_rwsplit_db]> set variable proxy_hint_enabled=true;\\nQuery OK, 0 rows affected (0.01 sec)\\nMySQL [distsql_rwsplit_db]> set readwrite_splitting hint source = write;\\nQuery OK, 0 rows affected (0.01 sec)\\nMySQL [distsql_rwsplit_db]> preview select * from wr_table;\\n+------------------+------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------+\\n| write_ds | select * from wr_table |\\n+------------------+------------------------+\\n1 row in set (0.00 sec)\\n```\n\n以上实验验证了 ShardingSphere-Proxy 有良好的读写分离的能力。它验证了底下连接单个 Aurora 集群进行读写分离的场景。如果既需要分库分表又需要读写分离,ShardingSphere-Proxy 也是支持的。比如先分到3个 Aurora 集群,然后每个集群需要提供读写分离的能力,我们可以直接将读写分离规则后面定义的数据源名称(4.4.4里的 wr_ds)放在分库分表规则对每张表指定的数据源里(4.1.3里的ds_0,ds_1,ds_2)。\n\n#### **4.5 故障恢复验证**\n本节来验证 ShardingSphere-Proxy 对于 Aurora 集群故障切换的感知能力。在 Aurora 集群发生主备切换时,如果 Proxy 能够动态检测到主备切换并连接到新的主数据库是比较理想的。本节实验仍然是验证第一个 Aurora 集群。\n\n测试脚本如下,它会持续连接到写节点并发送 update 请求,每次请求间隔1秒钟。\n\n```\\n[ec2-user@ip-111-22-3-123 shardingproxy]\$ cat testFailover.sh \\n#!/bin/bash\\nwhile true\\ndo\\n mysql -h 127.0.0.1 -uroot --port 3307 -proot -Ddistsql_rwsplit_db -e \\"update wr_table set c='failover' where a = 1;\\"\\n now=\$(date +\\"%T\\")\\n echo \\"update done: \$now\\"\\n sleep 1\\ndone \\n```\n\n运行脚本,然后在 Aurora 集群的写节点上点击 Action->Failover。会启动 Aurora 写节点和读节点的自动切换。在切换过程中,整个集群的读/写 endpoint 和只读 endpoint 维持不变,只是底层映射的节点发生变化。\n\n![image.png](https://dev-media.amazoncloud.cn/19051948af6442cfa2f3e41daed8bf22_image.png)\n\n通过观测 Aurora 的 Event(事件),可以看到整个故障切换在30秒左右完成。\n\n![image.png](https://dev-media.amazoncloud.cn/e807f96c1f3a4039a9e4bdcff0f4b640_image.png)\n\n遗憾的是,应用程序直接连接 ShardingSphereProxy 也就是前面的运行脚本不能自动监测到底层的 IP 变化。运行脚本一直抛错:\n\n```\\nERROR 1290 (HY000) at line 1: The MySQL server is running with the --read-only option so it cannot execute this statement\\nupdate done: 15:04:04\\nERROR 1290 (HY000) at line 1: The MySQL server is running with the --read-only option so it cannot execute this statement\\nupdate done: 15:04:05\\nERROR 1290 (HY000) at line 1: The MySQL server is running with the --read-only option so it cannot execute this statement\\nupdate done: 15:04:06\\n```\n\n直接在 MySQL 命令行连接到 Proxy 也是会有一样的错误。\n\n```\\nMySQL [distsql_rwsplit_db]> update wr_table set c=\\"failover\\" where a =2;\\nERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement\\n```\n\n分析原因在于 Aurora 发生故障切换的时候,读写 endpoint 和 IP 的映射会发生变化,而 ShardingSphere 的连接池在连接 Aurora 的时候,没有更新到新的 IP 上。我们可以采用下面的 workaround 可以使 ShardingSphereProxy 指向新的写节点,即重新创建数据源。尽管数据源本身定义没有发生变化,但是通过重建数据源 alter resource 的操作, ShardingSphereProxy 会重新拿取一遍 endpoint 到 IP 的映射,所以能够成功运行。\n\n```\\nMySQL [distsql_rwsplit_db]> alter resource write_ds(url=\\"jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&useSSL=false\\",user=admin,password=12345678), read_ds(url=\\"jdbc:mysql://aurora-2-07-7-shard1.cluster-ro-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&useSSL=false\\",user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.05 sec)\\n\\nMySQL [distsql_rwsplit_db]> update wr_table set c=\\"failover\\" where a =2;\\nQuery OK, 1 row affected (0.01 sec)\\n```\n\n每次 Aurora 故障切换时,我们可以检测故障切换的 event,或者是在应用收到 read-only 报错时显式调用上面语句。为了降低对应用的影响,我们可以采用 Lambda 的方式将 failover 重置数据源的操作自动化。因为 Aurora 的 failover 的事件是可以被监测到的,我们可以写一个Lambda函数,在监测到 failover 成功以后,显示调用更改 resource 的操作。\n\n总体的思路是: RDS 通过 Event Subscription 将 Event 的通知信息传递给 SNS topic,再由 SNS topic 传递给 Lambda 方法,然后在 Lambda 方法里显式连接 ShardingProxy 调用 alter resource 的 DistSQL 语句。\n\n具体步骤如下:\n\n##### **4.5.1 创建 SNS**\n按照 SNS 创建指南创建 SNS。打开 SNS 的 dashboard,点击创建 SNS topic,选择 Standard 标准类型。其它选择默认或者根据需要调整。\n\n![image.png](https://dev-media.amazoncloud.cn/47aa8109ccac434ca7efa5f16832fe40_image.png)\n\n##### **4.5.2 对要进行的 Aurora 集群创建 Event Subscription**\n\n在 RDS 的 Event Subscriptions 上,点击“Create Event Subscription”,在弹出的选项卡中选择 Target 为上一步骤创建的SNS,Source type 选择为 Cluster,cluster 里面选中我们需要关注的 Aurora 的集群,事件选择 Failover 事件。\n\n![image.png](https://dev-media.amazoncloud.cn/11ad9f0489084a048ad6b98e6e91dbf2_image.png)\n\n![image.png](https://dev-media.amazoncloud.cn/e90e9c38e9ee4f60aae38cdb8c6ec7ba_image.png)\n\n##### **4.5.3 创建 Lamdba 方法**\n因为 Lambda 要调用 VPC 里的 EC2上部署的 ShardingProxy,应该给它绑定一个专门的 Role,这个 Role 有权限在 VPC 里执行 Lambda 方法: 百尺竿头更进一步 – [Amazon Aurora](https://aws.amazon.com/cn/rds/aurora/?trk=cndc-detail) 的读写能力扩展之 ShardingSphere-Proxy 篇LambdaVPCAccessExecutionRole 按照 [IAM Role 创建文档](https://docs.aws.amazon.com/zh_cn/IAM/latest/UserGuide/id_roles_create_for-service.html)创建 Role 和 Policy,使 failoverlambda 的 role 有 百尺竿头更进一步 – [Amazon Aurora](https://aws.amazon.com/cn/rds/aurora/?trk=cndc-detail) 的读写能力扩展之 ShardingSphere-Proxy 篇LambdaVPCAccessExecutionRole 的权限。\n\n![image.png](https://dev-media.amazoncloud.cn/db2ecbc5e69c4254b2efafd57c4372f9_image.png)\n\n接下来按照 Lambda 文档创建 Lambda 方法\n\n![image.png](https://dev-media.amazoncloud.cn/ff1f2d3b57c3419dae2c38e866da074b_image.png)\n\n![image.png](https://dev-media.amazoncloud.cn/f74a45b15a5447c6bbc55fd38211bcc4_image.png)\n\n创建好 Lambda 方法以后,点击 Trigger,指明为 SNS,并指明在4.5.1里创建的 SNS topic。\n\n![image.png](https://dev-media.amazoncloud.cn/9389b236620c490da7d6ebf57dca4397_image.png)\n\n![image.png](https://dev-media.amazoncloud.cn/c34de3a3b83d49018ca199f0275b3e3f_image.png)\n\n##### **4.5.4 编写Lambda方法**\n\n```\\nimport os\\nimport json\\nimport pymysql\\n\\n# connect to ShardingProxy to reset the data source \\ndef resetDataSource():\\n db = pymysql.connect(host='111.22.3.123', user='root', password='root', port=3307, database='distsql_rwsplit_db')\\n \\n cur = db.cursor()\\n SQL = \\"alter resource write_ds(url=\\\\\\"jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&useSSL=false\\\\\\",user=admin,password=12345678), read_ds(url=\\\\\\"jdbc:mysql://aurora-2-07-7-shard1.cluster-ro-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&useSSL=false\\\\\\",user=admin,password=12345678);\\"\\n print (SQL)\\n cur.execute(SQL)\\n result = cur.fetchall()\\n for x in result:\\n print(x)\\n db.close()\\n\\ndef lambda_handler(event, context):\\n wholeMessage = event['Records'][0]['Sns']['Message']\\n print (\\"whole message\\" + wholeMessage)\\n \\n wholeMessageInJson = json.loads(wholeMessage)\\n eventMessage = wholeMessageInJson['Event Message']\\n print (\\"event message: \\" + eventMessage)\\n \\n isFailover = eventMessage.startswith('Completed failover to DB instance')\\n if isFailover == True:\\n print (\\"Failover completed! \\" + eventMessage)\\n resetDataSource()\\n\\n return {\\n 'statusCode': 200,\\n 'body': Lambda Invocation Successful!'\\n }\\n```\n\nLambda 方法是 Python 语言书写的,在访问 ShardingSphereProxy 时以 MySQL 方式访问,所以需要引入 pymysql 的 lib 库。具体方法为:\n\n1)在 Linux 上安装 pymysql,以 Amazon-Linux 虚拟机为例,会默认安装到目录 ./home/ec2-user/.local/lib/python3.7/site-packages/pymysql 下\n\n2)将 pymysql 目录拷贝到临时目录 /tmp\n\n3)写 Lambda 方法,存储到 lambda_function.py 文件中\n\n4)打包 zip -r lambda.zip pymysql lambda_function.py\n\n5) 在控制台通过 S3 或者本地上传。\n\n##### **4.5.5 设置 ShardingSphereProxy 所在 EC2的security group**\n因为 Lambda 要在 VPC 里访问 ShardingSphereProxy,而 ShardingSphereProxy 以3307端口运行,应该配置相应 secuity group,打开3307端口给同一个 VPC 内部的访问。依据安全组配置文档配置成的 security group 如下:\n\n![image.png](https://dev-media.amazoncloud.cn/5a0f23e596c84b2d91c5f3b74b3965be_image.png)\n\n##### **4.5.6 验证 failover**\n重复本小节开始的操作,运行 testFailover.sh,然后手动在 RDS console 页 failover Aurora 节点,会发现 testFailover.sh 持续稳定输出, 不会再出现 read-only 的错误。\n\n```\\nupdate done: 13:44:44\\n…\\nupdate done: 13:44:47\\nupdate done: 13:45:00\\nupdate done: 13:45:01\\n…\\nupdate done: 13:45:17\\n```\n\n去 cloudwatch 里查看 Lambda function 的日志,会发现 Lambda 被成功调用。\n\n![image.png](https://dev-media.amazoncloud.cn/64bca78e3ff14659b7191cb7f4a865ca_image.png)\n\n以上实验验证了 ShardingSphere-Proxy 对于 Aurora 集群故障切换的感知能力。尽管 ShardingSphere-Proxy 自己没有提供良好的匹配性,通过监测 Aurora 集群事件触发 Lamdba 方法来显式重置 ShardingSphere-Proxy 数据源的方式,我们可以实现 ShardingSphere-Proxy 与 Aurora 结合的故障切换能力。\n\n### **5.结语**\n本篇文章通过数据库中间件 ShardingSphere-Proxy 拓展了 Aurora 的分库分表能力和读写分离的能力。\n\nShardingSphere-Proxy 内置连接池,对 MySQL 语法支持较强,在分库分表和读写分离上表现出色。它对多表 join 上,可以支持分片规则相同的表的 join,以及小表和大表的 join,基本能满足 OLTP 场景的需求。在动态分片上,ShardingSphere-Proxy 提供在线更改分片规则的能力,但需要用户在底层 Aurora 集群手动操作子表创建及数据迁移,需要一定工作量。故障切换维度,ShardingSphere-Proxy 与 Aurora 的融合不是很好,但是可以通过本文提供的 Aurora 故障切换 Event 调用 Lambda 方法来显式重置数据源的方式,实现 ShardingSphere-Proxy 对 Aurora 集群故障切换对感知。\n\n总体而言,ShardingSphere-Proxy 这个中间件产品还是能与 Aurora 集群进行一个良好匹配,进一步提升 Aurora 集群的读写能力的。它有良好的文档,也是比较受关注的开源产品,建议读者在考虑 Aurora 分库分表实现时,评估下这个产品。后续我们会继续推出对其他中间件以及 JDBC 方面的拓展和研究系列博客。\n\n**本篇作者**\n\n\n**马丽丽**\n\n*亚马逊云科技数据库解决方案架构师,十余年数据库行业经验,先后涉猎 NoSQL 数据库 Hadoop/Hive、企业级数据库 DB2、分布式数仓 Greenplum/Apache HAWQ 以及亚马逊云原生数据库的开发和研究。*\n\n点击链接,了解更多内容:\n- Apache ShardingSphere GitHub 地址:[https://github.com/apache/shardingsphere](https://github.com/apache/shardingsphere)\n- SphereEx 官网:[https://www.sphere-ex.com](https://www.sphere-ex.com)\n\n\n![image.png](https://dev-media.amazoncloud.cn/7886a968195c46c2ab3dbf2e01a01f22_image.png)","render":"<h3><a id=\\"1_0\\"></a><strong>1.前言</strong></h3>\\n<p>Amazon Aurora 是亚马逊云科技自研的一项关系数据库服务,它在提供和开源数据库 MySQL、PostgreSQL 的完好兼容性同时,也能够提供和商业数据库媲美的性能和可用性。性能方面,Aurora MySQL 能够支持到与开源标准 MySQL 同等配置下五倍的吞吐量,Aurora PostgreSQL 能够支持与开源标准 PostgreSQL 同等配置下三倍的吞吐量的提升。在扩展性的角度,Aurora 在存储与计算、横向与纵向方面都进行了功能的增强和创新。Aurora 支持多达128TB 的存储容量,而且支持10GB 为单位的存储层动态收缩。计算方面,Aurora 提供多个读副本的可扩展性配置支持一个区域内多达15个读副本的扩展,提供多主的架构来支持同一个区域内4个写节点的扩展,提供 Serverless 无服务器化的架构实例级别的秒级纵向扩展,提供全球数据库来实现数据库的低延迟跨区域扩展。</p>\n<p>随着用户数据量的增长,Aurora 已经提供了很好的扩展性,那是否可以进一步处理更多的数据量、支持更多的并发访问呢?您可以考虑利用分库分表的方式,来支持底层多个 Aurora 集群的配置。基于此,包含这篇博客在内的系列博客会进行相应的介绍,旨在为您进行分库分表时选择使用代理或者 JDBC 提供参考。</p>\n<p>本篇博客会聚焦如何使用 ShardingSphere-Proxy,一个开源的分库分表中间件工具,来进行数据库集群的构建,会涵盖分库分表、读写分离、动态配置等方面。</p>\n<h3><a id=\\"2ShardingSphereProxy__7\\"></a><strong>2.ShardingSphere-Proxy 介绍</strong></h3>\\n<p><a href=\\"https://shardingsphere.apache.org/index_zh.html\\" target=\\"_blank\\">Apache ShardingSphere</a> 是一套开源的分布式数据库解决方案组成的生态圈,它由 JDBC、Proxy 和 Sidecar(规划中)这3款既能够独立部署,又支持混合部署配合使用的产品组成。</p>\\n<p>作为中间件,ShardingSphere-Proxy 的定位是透明化的数据库代理端。它采用 Apache2.0协议,持续迭代版本,最新版本为5.1.0,目前支持 MySQL 和 PostgreSQL 版本。它对应用程序透明,兼容 MySQL/PostgreSQL 协议的客户端。MySQL 命令行 mysql,MySQL workbench 等都可以直接访问 ShardingSphere-Proxy。</p>\n<p>ShardingSphere-Proxy 下层可以连接不同的数据库,这些数据库可以是同构也可以是异构的。用户可以有两种方式指定底层数据库的分库分表或者读写分离规则:1)根据 yaml 配置文件静态指定2)利用 ShardingSphere 提供的增强性的 DistSQL 语言来指定。因为 DistSQL 支持动态创建规则不需要重启 Proxy 本身,它成为 ShardingSphere-Proxy 未来发展的重点。</p>\n<p>作为数据库代理,是否能够提供连接池增强用户并发访问的连接处理是需要考量的一方面,ShardingSphere-Proxy 在添加数据源并进行初始化时,会支持为每个数据库配置一个 Hikari 连接池。Hikari 是业界广泛使用的连接池,对性能损耗较小,而且被 SpringBoot 采用为缺省连接池。ShardingSphere-Proxy 的连接池可以支持用户配置最大连接数、最大空闲时间以及缓存相关的信息等。除 Hikari 连接池外,ShardingSphere-Proxy 也支持其它连接池的配置。</p>\n<p>和现有 SQL 的语法兼容性也是用户衡量数据库代理的关键因素,因为这涉及到是否更改应用代码。以 MySQL 为例,ShardingSphere 支持大部分的 MySQL 语法,但也有少量不支持的语法,比如 optimize 表、资源组的管理、用户的创建和GRANT权限管理等。具体可以查阅 ShardingSphere 的最新文档。</p>\n<p>下面会分享我对 ShardingSphereProxy 连接 Aurora 的几个维度的实验测试:1)分库分表 2)动态扩展 3)读写分离 4)多表 join 5)故障恢复。</p>\n<h3><a id=\\"3_20\\"></a><strong>3.环境构建</strong></h3>\\n<h4><a id=\\"31_Aurora__21\\"></a><strong>3.1 Aurora 集群搭建</strong></h4>\\n<p>首先根据 <a href=\\"https://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/Aurora.CreateInstance.html\\" target=\\"_blank\\">Aurora 集群创建指南</a>创建三套 Aurora MySQL集群,机型为 db.r5.2xlarge,每套集群有一个写节点一个读节点。</p>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/8dee62e5764d46939df454eb4e120134_image.png\\" alt=\\"image.png\\" /></p>\n<h4><a id=\\"32_ShardingSphereProxy_26\\"></a><strong>3.2 ShardingSphere-Proxy搭建</strong></h4>\\n<p>在与 Aurora 相同的可用区下启动一台 EC2节点,机型为 r5.8xlarge. 然后在上面安装 ShardingSphere-Proxy。</p>\n<h5><a id=\\"321__29\\"></a><strong>3.2.1 下载安装包</strong></h5>\\n<p>直接下载二进制安装包,进行解压。下载最新版本5.1.0,它对 DistSQL 支持较好。</p>\n<pre><code class=\\"lang-\\">wget https://dlcdn.apache.org/shardingsphere/5.1.0/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin.tar.gz\\ntar -xvf apache-shardingsphere-5.1.0-shardingsphere-proxy-bin.tar.gz\\n</code></pre>\\n<p>SharingSphereProxy 自带的库里包含对 PostgreSQL 的 JDBC driver,但不包含 MySQL 的 driver。因为创建的集群是 MySQL,需要将 MySQL 的 JDBC driver 拷贝到 lib 目录。</p>\n<pre><code class=\\"lang-\\">wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar\\ncp mysql-connector-java-5.1.47.jar apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/lib/\\n</code></pre>\\n<h5><a id=\\"322__Proxy__44\\"></a><strong>3.2.2 配置 Proxy 的服务端</strong></h5>\\n<p>在 ShardingSphere-Proxy 的根目录下,有个配置文件目录为 conf,里面有一个文件是 server.yaml,用来配置 ShardingSphere-Proxy 自己作为代理对外提供服务的信息以及元信息存放等。下面是一个配置示例,里面配置了用户权限信息,特定属性信息,以及元信息以集群模式存放在 zookeeper 里。</p>\n<pre><code class=\\"lang-\\">rules:\\n - !AUTHORITY\\n users: //访问Proxy的用户名和密码信息\\n - root@%:root\\n - sharding@:sharding\\n provider: //控制用户对schema的登陆权限\\n type: ALL_PRIVILEGES_PERMITTED\\n - !TRANSACTION //事务类型配置,支持本地事务、XA两阶段事务、BASE柔性事务\\n defaultType: XA\\n providerType: Atomikos\\n\\nprops: //特定属性配置\\n max-connections-size-per-query: 1\\n proxy-hint-enabled: true //为强制路由使用,默认值为false\\n\\nmode: //元信息存放的配置,shardingsphereProxy支持三种模式:内存、单机和集群\\n type: Cluster\\n repository:\\n type: ZooKeeper //可以设置为zookeeper、etcd等\\n props:\\n namespace: shardingproxy\\n server-lists: localhost:2181\\n retryIntervalMilliseconds: 500\\n timeToLiveSeconds: 60\\n maxRetries: 3\\n operationTimeoutMilliseconds: 500\\n overwrite: false\\n</code></pre>\\n<h4><a id=\\"33__Proxy_77\\"></a><strong>3.3 启动 Proxy</strong></h4>\\n<p>直接在 ShardingSphereProxy 根目录下的 bin 对应着启动和停止脚本。运行时的日志在目录 logs 下。启动 Proxy</p>\n<pre><code class=\\"lang-\\">bin/start.sh\\nbin/stop.sh\\n</code></pre>\\n<h4><a id=\\"34__85\\"></a><strong>3.4 验证连接</strong></h4>\\n<p>如无特殊配置,ShardingSphereProxy 默认使用3307端口。使用3.2.2中配置的用户名和密码登录 proxy。在 EC2上运行 mysql 命令行工具进行连接,连接成功。注意这里没有任何数据库,因为我们没有使用 YAML 配置文件预先配置数据源。</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 bin]\$ mysql -h 127.0.0.1 -uroot --port 3307 -proot\\nWelcome to the MariaDB monitor. Commands end with ; or \\\\g.\\nYour MySQL connection id is 1\\nServer version: 5.7.22-ShardingSphere-Proxy 5.1.0 \\n\\nCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.\\n\\nType 'help;' or '\\\\h' for help. Type '\\\\c' to clear the current input statement.\\n\\nMySQL [(none)]&gt; show databases;\\nEmpty set (0.01 sec)\\n</code></pre>\\n<h3><a id=\\"4_102\\"></a><strong>4.功能测试</strong></h3>\\n<h4><a id=\\"41_DistSQL__103\\"></a><strong>4.1 DistSQL 创建分片规则和数据分片测试</strong></h4>\\n<p>本节来验证 ShardingSphere 的基本的分库分表能力。ShardingSphereProxy 支持两种方式创建分片规则和读写分离规则,YAML 和 DistSQL。DistSQL 扩展了 SQL 语法,可以支持在线创建数据源、创建和更改建表规则,较为灵活,本文只介绍 DistSQL 的用例。</p>\n<h5><a id=\\"411__106\\"></a><strong>4.1.1 创建数据库</strong></h5>\\n<p>连接到 ShardingSphere-Proxy,去创建数据库,作为逻辑的分布式数据库。</p>\n<pre><code class=\\"lang-\\">MySQL [(none)]&gt; create database distsql_sharding_db;\\nQuery OK, 0 rows affected (0.90 sec)\\n</code></pre>\\n<p>在各个 Aurora 集群上创建数据库,作为数据库源进行连接。其中,rshard1,rshard2,rshard3是我自己定义的连接 Aurora 数据库的alias。</p>\n<pre><code class=\\"lang-\\">alias rshard1=’mysql -h \$dbname -u\$username -p\$password’\\n</code></pre>\\n<pre><code class=\\"lang-\\">[ec2-user@ ip-111-22-3-123 bin]\$ rshard1 -e &quot;create database dist_ds&quot;;\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard2 -e &quot;create database dist_ds;&quot;\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard3 -e &quot;create database dist_ds;&quot;\\n</code></pre>\\n<h4><a id=\\"412__127\\"></a><strong>4.1.2 创建数据源</strong></h4>\\n<p>在 ShadingSphereProxy 中运行下面 DistSQL 语句创建3个数据源,分别指向3个不同 Aurora 集群</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; add resource ds_0(url=&quot;jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/dist_ds?serverTimezone=UTC&amp;useSSL=false&quot;,user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.03 sec)\\n\\nMySQL [distsql_sharding_db]&gt; add resource ds_1(url=&quot;jdbc:mysql://aurora-2-07-7-shard2.cluster-12345678.us-east-1.rds.amazonaws.com:3306/dist_ds?serverTimezone=UTC&amp;useSSL=false&quot;,user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.06 sec)\\n\\nMySQL [distsql_sharding_db]&gt; add resource ds_2(url=&quot;jdbc:mysql://aurora-2-07-7-shard3.cluster-12345678.us-east-1.rds.amazonaws.com:3306/dist_ds?serverTimezone=UTC&amp;useSSL=false&quot;,user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.05 sec)\\n</code></pre>\\n<h4><a id=\\"413__141\\"></a><strong>4.1.3 创建分片规则</strong></h4>\\n<p>这里指明t_order表的分片规则,注意分片规则的表名和后续要创建的表表名一致。具体规则为:对底层的3个数据源(Aurora 集群)按照 order_id 对表进行 hash 分片,分成6份。另外,对 order_id 采用值自动生成的策略,采用策略为 snowflake 算法。ShardingSphere 支持两种分布式主键生成策略:UUID 和雪花算法 SNOWFLAKE。使用雪花算法生成的主键,二进制表示形式包含4部分,从高位到低位分表为:1bit 符号位、41bit 时间戳位、10bit 工作进程位以及12bit 序列号位。在 ShardingSphereProxy 中运行下面 DistSQL 语句建立分片规则:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; CREATE SHARDING TABLE RULE t_order(\\n→ RESOURCES(ds_0,ds_1, ds_2),\\n→ SHARDING_COLUMN=order_id,\\n→ TYPE(NAME=hash_mod,PROPERTIES(&quot;sharding-count&quot;=6)),\\n→ KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME=snowflake))\\n→ );\\nQuery OK, 0 rows affected (0.02 sec)\\n</code></pre>\\n<h5><a id=\\"414__154\\"></a><strong>4.1.4 建表</strong></h5>\\n<p>建表语句和普通 MySQL 建表语句一致。在 ShardingSphereProxy 中运行下面语句建表:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; CREATE TABLE `t_order` ( `order_id` bigint NOT NULL, `user_id` int NOT NULL, `status` varchar(45) DEFAULT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\\n -&gt; ;\\nQuery OK, 0 rows affected (0.22 sec)\\n</code></pre>\\n<p>在 ShardingSphereProxy上 查看表的状态。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; show tables;\\n+-------------------------------+------------+\\n| Tables_in_distsql_sharding_db | Table_type |\\n+-------------------------------+------------+\\n| t_order | BASE TABLE |\\n+-------------------------------+------------+\\n1 row in set (0.00 sec)\\n</code></pre>\\n<p>分别连接到3个 Aurora 集群上查看表是否自动创建。可以看到每个底层数据库集群上都创建了两张表,一共是6张表。而且表名是以“t_oder_”数字排序的</p>\n<pre><code class=\\"lang-\\">[ec2-user@ ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e &quot;show tables;&quot;\\n+-------------------+\\n| Tables_in_dist_ds |\\n+-------------------+\\n| t_order_0 |\\n| t_order_3 |\\n+-------------------+\\n[ec2-user@ ip-111-22-3-123 bin ]\$ rshard2 -Ddist_ds -e &quot;show tables;&quot;\\n+-------------------+\\n| Tables_in_dist_ds |\\n+-------------------+\\n| t_order_1 |\\n| t_order_4 |\\n+-------------------+\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard3 -Ddist_ds -e &quot;show tables;&quot;\\n+-------------------+\\n| Tables_in_dist_ds |\\n+-------------------+\\n| t_order_2 |\\n| t_order_5 |\\n+-------------------+\\n</code></pre>\\n<h5><a id=\\"415__199\\"></a><strong>4.1.5 插入和查找数据</strong></h5>\\n<p>在 ShardingSphere-Proxy 中插入并查找数据,数据可以正常插入和查找成功。在 ShardingSphere-Proxy 中运行:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (1, 'ok');\\ninsert into t_order(user_id, status) values (2, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (2, 'abc');\\ninsert into t_order(user_id, status) values (3, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (3, 'abc');\\ninsert into t_order(user_id, status) values (4, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (4, 'abc');\\ninsert into t_order(user_id, status) values (5, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (5, 'abc');\\ninsert into t_order(user_id, status) values (6, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (6, 'abc');\\ninsert into t_order(user_id, status) values (7, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (7, 'abc');\\ninsert into t_order(user_id, status) values (8, 'abc');\\nQuery OK, 1 row affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (8, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order(user_id, status) values (9, 'abc');\\nQuery OK, 1 row affected (0.00 sec)\\n\\nMySQL [distsql_sharding_db]&gt; select * from t_order;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161915748353 | 2 | abc |\\n| 708700161995440128 | 5 | abc |\\n| 708700169725542400 | 9 | abc |\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n| 708700161970274305 | 4 | abc |\\n| 708700162016411649 | 6 | abc |\\n| 708700162058354689 | 8 | abc |\\n+--------------------+---------+--------+\\n9 rows in set (0.01 sec)\\n</code></pre>\\n<p>去各个 Aurora 集群中查找子表插入的数据,可以看到在 Proxy 插入的9条记录被打散到底层的6张表中。因为 order_id 为 snowflake 算法生成而数据量比较小,这里的数据并不均匀。</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e &quot;select * from t_order_0;&quot;\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e &quot;select * from t_order_3;&quot;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161915748353 | 2 | abc |\\n+--------------------+---------+--------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e &quot;select * from t_order_1;&quot;\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e &quot;select * from t_order_4;&quot;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161995440128 | 5 | abc |\\n| 708700169725542400 | 9 | abc |\\n+--------------------+---------+--------+\\n[ec2-user@111-22-3-123 bin]\$ rshard3 -Ddist_ds -e &quot;select * from t_order_2;&quot;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n+--------------------+---------+--------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard3 -Ddist_ds -e &quot;select * from t_order_5;&quot;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161970274305 | 4 | abc |\\n| 708700162016411649 | 6 | abc |\\n| 708700162058354689 | 8 | abc |\\n+--------------------+---------+--------+&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;\\n</code></pre>\\n<p>上述实验验证了 ShardingSphere-Proxy 具有创建逻辑库、连接数据源、创建分片规则、创建逻辑表时会自动在底层数据库上创建子表、能够执行查询的分发以及聚合能力。</p>\n<h4><a id=\\"42__292\\"></a><strong>4.2 动态伸缩验证(在线扩展分片)</strong></h4>\\n<p>本节来验证 ShardingSphere-Proxy 是否具有动态更改表的分片规则的能力。</p>\n<p>ShardingSphere-Proxy 提供在线更改分片规则的能力,但是如果子表已经按照之前的规则创建成功,则不会有新的子表随着分片数目的增多被创建出来,也不会有原来的子表随着分片数目的减少而被删除。所以需要手动在底层分片数据库上创建表名和迁移数据。</p>\n<p>将4.1节里的表的分片数从6调高到9,修改分片规则本身能够成功,但是后续查找会出错,因为没有新的子表创建出来。在 ShardingSphere-Proxy 上运行下面 DistSQL:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; alter SHARDING TABLE RULE t_order(\\n -&gt; RESOURCES(ds_0,ds_1, ds_2),\\n -&gt; SHARDING_COLUMN=order_id,\\n -&gt; TYPE(NAME=hash_mod,PROPERTIES(&quot;sharding-count&quot;=9)),\\n -&gt; KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME=snowflake))\\n -&gt; );\\nQuery OK, 0 rows affected (0.01 sec)\\n\\nMySQL [distsql_sharding_db]&gt; select * from t_order;\\nERROR 1146 (42S02): Table 'dist_ds.t_order_6' doesn't exist\\n</code></pre>\\n<p>如果此时在子集群上分别创建好对应的子表,再在 ShardingSphere-Proxy 上查找就不会再出错。连接到3个 Aurora 集群,手动创建子表</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -Ddist_ds -e &quot;create table t_order_6(order_id bigint not null, user_id int not null, status varchar(45) default null, primary key(order_id)) engine=innodb default charset=utf8mb4; &quot;\\n[ec2-user@ ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e &quot;create table t_order_7(order_id bigint not null, user_id int not null, status varchar(45) default null, primary key(order_id)) engine=innodb default charset=utf8mb4; &quot;\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard3 -Ddist_ds -e &quot;create table t_order_8(order_id bigint not null, user_id int not null, status varchar(45) default null, primary key(order_id)) engine=innodb default charset=utf8mb4; &quot;\\n</code></pre>\\n<p>Proxy 查找整个逻辑表不再报错。在 ShardingSphere-Proxy 上运行下面 SQL:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; select * from t_order;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161915748353 | 2 | abc |\\n| 708700161995440128 | 5 | abc |\\n| 708700169725542400 | 9 | abc |\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n| 708700161970274305 | 4 | abc |\\n| 708700162016411649 | 6 | abc |\\n| 708700162058354689 | 8 | abc |\\n+--------------------+---------+--------+\\n9 rows in set (0.01 sec)\\n</code></pre>\\n<p>如果有新的数据插入,会按照新的分片规则进行到子表的映射。在 ShardingSphere-Proxy 上查看 SQL 语句的查询计划:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; preview insert into t_order values(7, 100, 'new');\\n+------------------+---------------------------------------------+\\n| data_source_name | sql |\\n+------------------+---------------------------------------------+\\n| ds_1 | insert into t_order_7 values(7, 100, 'new') |\\n+------------------+---------------------------------------------+\\n1 row in set (0.00 sec)\\n\\nMySQL [distsql_sharding_db]&gt; insert into t_order values(7, 100, 'new');\\nQuery OK, 1 row affected (0.00 sec)\\n</code></pre>\\n<p>登录到 Aurora 子集群上查看子表,可以看到数据已经成功插入。</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -Ddist_ds -e &quot;select * from t_order_7;&quot;\\n+----------+---------+--------+\\n| order_id | user_id | status |\\n+----------+---------+--------+\\n| 7 | 100 | new |\\n+----------+---------+--------+\\n</code></pre>\\n<p>再来看下在线减少分片的情况。如果将分片数目调小,比如调到3,表里的已有数据不会被迁移,查找整张表时只能拿到部分数据。在 ShardingSphere-Proxy 上运行下面 DistSQL 和 SQL 语句:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; alter SHARDING TABLE RULE t_order(\\n -&gt; RESOURCES(ds_0,ds_1, ds_2),\\n -&gt; SHARDING_COLUMN=order_id,\\n -&gt; TYPE(NAME=hash_mod,PROPERTIES(&quot;sharding-count&quot;=3)),\\n -&gt; KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME=snowflake))\\n -&gt; );\\nQuery OK, 0 rows affected (0.02 sec)\\nMySQL [distsql_sharding_db]&gt; select * from t_order;\\n+--------------------+---------+--------+\\n| order_id | user_id | status |\\n+--------------------+---------+--------+\\n| 708700161877999616 | 1 | ok |\\n| 708700161936719872 | 3 | abc |\\n| 708700162041577472 | 7 | abc |\\n+--------------------+---------+--------+\\n3 rows in set (0.00 sec)\\n</code></pre>\\n<p>经过上面验证,我们的结论是 ShardingSphereProxy 的分片规则是可以在线更改的,但子表的创建和数据的重新分布需要手动去完成。</p>\n<h4><a id=\\"43__390\\"></a><strong>4.3 绑定表和广播表的测试</strong></h4>\\n<p>本节来验证 ShardingSphere-Proxy 对于多表 join 的支持。尽管 OLTP 的数据库中的操作通常较为简单,但也有可能会涉及到多表 join 的情况。ShardingSphereProxy 针对多表 join 的优化有支持绑定表和广播表。如果两张表是绑定表而且 join 时采用的是 shard key,可以进行两张表的 join。广播表通过把小表复制到各个节点,可以实现大表和小表的快速 join。</p>\n<h5><a id=\\"431__393\\"></a><strong>4.3.1 绑定表</strong></h5>\\n<p>ShardingSphereProxy 的绑定表可以通过 DistSQL 里的 CREATE SHARDING BINDING TABLE RULES 来绑定两张表。这里以4.1节中提到的 t_order 表和新创建的一张表 t_order_item 为例进行展开。</p>\n<p>连接到 ShardingSphere-Proxy 上运行下面 DistSQL 和 SQL 语句。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; CREATE SHARDING TABLE RULE t_order_item(\\n -&gt; RESOURCES(ds_0,ds_1, ds_2),\\n -&gt; SHARDING_COLUMN=order_id,\\n -&gt; TYPE(NAME=hash_mod,PROPERTIES(&quot;sharding-count&quot;=6)));\\nQuery OK, 0 rows affected (0.04 sec)\\n\\nMySQL [distsql_sharding_db]&gt; CREATE TABLE `t_order_item` ( `order_id` bigint NOT NULL, `item_id` int NOT NULL, `name` varchar(45) DEFAULT NULL, PRIMARY KEY (`item_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;\\nQuery OK, 0 rows affected (0.08 sec)\\n</code></pre>\\n<p>创建了binding rule以后,查看join计划,我们看到join下推到对应子表和子表的join上。在ShardingSphere-Proxy上运行:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; CREATE SHARDING BINDING TABLE RULES (t_order,t_order_item);\\nQuery OK, 0 rows affected (0.04 sec)\\n\\nMySQL [distsql_sharding_db]&gt; preview select * from t_order, t_order_item where t_order.order_id=t_order_item.order_id;\\n+------------------+------------------------------------------------------------------------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------------------------------------------------------------------------+\\n| ds_0 | select * from t_order_0, t_order_item_0 where t_order_0.order_id=t_order_item_0.order_id |\\n| ds_0 | select * from t_order_3, t_order_item_3 where t_order_3.order_id=t_order_item_3.order_id |\\n| ds_1 | select * from t_order_1, t_order_item_1 where t_order_1.order_id=t_order_item_1.order_id |\\n| ds_1 | select * from t_order_4, t_order_item_4 where t_order_4.order_id=t_order_item_4.order_id |\\n| ds_2 | select * from t_order_2, t_order_item_2 where t_order_2.order_id=t_order_item_2.order_id |\\n| ds_2 | select * from t_order_5, t_order_item_5 where t_order_5.order_id=t_order_item_5.order_id |\\n+------------------+------------------------------------------------------------------------------------------+\\n6 rows in set (0.01 sec)\\n</code></pre>\\n<h5><a id=\\"432__429\\"></a><strong>4.3.2 广播表</strong></h5>\\n<p>广播表是指每张表在每个库里都有一个完整的备份,可以通过 CREATE SHARDING BROADCAST TABLE RULES 来指定。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; CREATE SHARDING BROADCAST TABLE RULES (t_user);\\nQuery OK, 0 rows affected (0.03 sec)\\n\\nMySQL [distsql_sharding_db]&gt; create table t_user (user_id int, name varchar(100));\\nQuery OK, 0 rows affected (0.04 sec)\\n</code></pre>\\n<p>登录到各个 shard Aurora 集群查看创建的表。可以看到与分片表的子表名末尾有数字序号不同的是 ,广播表对应的每个库上的名字是相同的,就是逻辑表名本身。</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 bin]\$ rshard1 -D dist_ds -e &quot;show tables like '%user%';&quot;\\n+----------------------------+\\n| Tables_in_dist_ds (%user%) |\\n+----------------------------+\\n| t_user |\\n+----------------------------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard2 -D dist_ds -e &quot;show tables like '%user%';&quot;\\n+----------------------------+\\n| Tables_in_dist_ds (%user%) |\\n+----------------------------+\\n| t_user |\\n+----------------------------+\\n[ec2-user@ip-111-22-3-123 bin]\$ rshard3 -D dist_ds -e &quot;show tables like '%user%';&quot;\\n+----------------------------+\\n| Tables_in_dist_ds (%user%) |\\n+----------------------------+\\n| t_user |\\n+----------------------------+\\n</code></pre>\\n<p>在 ShardingSphereProxy 中运行广播表和其它表的 join,采用的是本地 join 的方式。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_sharding_db]&gt; preview select * from t_order, t_user where t_order.user_id=t_user.user_id;\\n+------------------+------------------------------------------------------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------------------------------------------------------+\\n| ds_0 | select * from t_order_0, t_user where t_order_0.user_id=t_user.user_id |\\n| ds_0 | select * from t_order_3, t_user where t_order_3.user_id=t_user.user_id |\\n| ds_1 | select * from t_order_1, t_user where t_order_1.user_id=t_user.user_id |\\n| ds_1 | select * from t_order_4, t_user where t_order_4.user_id=t_user.user_id |\\n| ds_2 | select * from t_order_2, t_user where t_order_2.user_id=t_user.user_id |\\n| ds_2 | select * from t_order_5, t_user where t_order_5.user_id=t_user.user_id |\\n+------------------+--------\\n</code></pre>\\n<p>上面实验验证了 ShardingSphere-Proxy 是可以支持两张绑定表的 join,以及广播表和分片表的 join 的。对于非绑定的两张分片表的join,ShardingSphere-Proxy 有一个 Federation 的功能是在支持的,但还不是很成熟,建议后续持续关注。</p>\n<h4><a id=\\"44__481\\"></a><strong>4.4 读写分离功能验证</strong></h4>\\n<p>本节来验证 ShardingSphere-Proxy 对于读写分离的支持。随着业务增长,写和读的负载分别在不同的数据库节点上能够有效提供整个数据库集群的处理能力。Aurora 通过读/写的 endpoint 可以满足用户写和强一致性读的需求,只读的 endpoint 可以满足用户非强一致性读的需求。Aurora 的读写延迟在毫秒级别,比 MySQL 基于 binlog 的逻辑复制要低得多,所以有很多负载是可以直接打到只读 endpoint 的。</p>\n<p>ShardingSphereProxy 提供的读写分离的特性可以进一步可以封装 Aurora 的读/写端点和只读端点。用户可以直接连接到 Proxy 的端点,即可进行自动的读写分离。ShardingSphereProxy 对特殊情况的处理逻辑是:1)同一线程且同一数据库连接内,如果有写入操作,则后续的读操作均从主库读取 2)可以通过 Hint 的机制强制把读请求发到写节点(主库)。下面会以 Aurora3个集群中的第一个集群来验证 ShardingSphere-Proxy 读写分离的能力。</p>\n<h5><a id=\\"441__Aurora__486\\"></a><strong>4.4.1 查看 Aurora 集群读/写端点和只读端点</strong></h5>\\n<p>Aurora 集群有两个端点,写的端点和读的端点。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/81c9ce32be674804ac2f0a47b62285eb_image.png\\" alt=\\"image.png\\" /></p>\n<h5><a id=\\"442__Aurora__491\\"></a><strong>4.4.2 在 Aurora 集群中创建数据库</strong></h5>\\n<p>连接到 Aurora 集群中运行:</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 ~]\$ rdbw -e &quot;create database wr_ds;&quot;\\n</code></pre>\\n<h5><a id=\\"443__498\\"></a><strong>4.4.3 数据源配置</strong></h5>\\n<p>在 ShardingSphere-Proxy 上创建数据源,写的数据源指向 Aurora 的读写 endpoint,读的数据源指向 Aurora 的只读 endpoint。注意:对域名的情况,ShardingSphereProxy 只支持通过 url 的方式创建数据源,尚未支持通过 HOST、Port 的方式。连接到 ShardingSphere-Proxy 上创建逻辑数据库 distsql_rwsplit_db 并在改数据库中添加数据源:</p>\n<pre><code class=\\"lang-\\">MySQL [(none)]&gt; create database distsql_rwsplit_db;\\nQuery OK, 0 rows affected (0.02 sec)\\nMySQL [(none)]&gt; use distsql_rwsplit_db;\\nDatabase changed\\nMySQL [distsql_rwsplit_db]&gt; add resource write_ds(url=&quot;jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&amp;useSSL=false&quot;,user=admin,password=12345678), read_ds(url=&quot;jdbc:mysql://aurora-2-07-7-shard1.cluster-ro-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&amp;useSSL=false&quot;,user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.08 sec)\\n</code></pre>\\n<h5><a id=\\"444__510\\"></a><strong>4.4.4 读写分离规则配置</strong></h5>\\n<p>创建读写分离规则,写请求发到写的数据源,读请求发到读的数据源。与分库分表规则要求 RULE 后面必须是表名不同的是,这里的 RULE 后面跟的是数据源的名字,适用于在这个数据库里创建的所有的表。在 ShardingSphere-Proxy 上运行下面 DistSQL 语句:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_ rwsplit_db]&gt; CREATE READWRITE_SPLITTING RULE wr_ds (\\n -&gt; WRITE_RESOURCE=write_ds,\\n -&gt; READ_RESOURCES(read_ds),\\n -&gt; TYPE(NAME=random)\\n -&gt; );\\nQuery OK, 0 rows affected (0.36 sec)\\n</code></pre>\\n<h5><a id=\\"445__522\\"></a><strong>4.4.5 建表</strong></h5>\\n<p>创建一张普通表,建表语句和 MySQL 建表语句一致。在 ShardingSphere-Proxy 上运行下面 SQL 语句:</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_ rwsplit_db]&gt; create table wr_table (a int, b int, c varchar(20));\\nQuery OK, 0 rows affected (0.17 sec)\\n</code></pre>\\n<h5><a id=\\"446__530\\"></a><strong>4.4.6 检查读写分离是否实现</strong></h5>\\n<p>在 ShardingSphere-Proxy 上运行下面语句查看查询计划,查看语句是发送到底层哪个数据源。可以看到:写请求发送到写节点,读请求会发送到读写点。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_rwsplit_db]&gt; preview insert into wr_table values(1,1,'ab');\\n+------------------+---------------------------------------+\\n| data_source_name | sql |\\n+------------------+---------------------------------------+\\n| write_ds | insert into wr_table values(1,1,'ab') |\\n+------------------+---------------------------------------+\\n1 row in set (0.10 sec)\\nMySQL [distsql_rwsplit_db]&gt; preview select * from wr_table;\\n+------------------+------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------+\\n| read_ds | select * from wr_table |\\n+------------------+------------------------+\\n1 row in set (0.02 sec)\\n</code></pre>\\n<p>运行一个脚本来多次操作,再去Aurora集群指标监控中去验证。该脚本是一个循环,运行1000次,每次会插入一条记录,并查找表的记录总条数。</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 shardingproxy]\$ cat testReadWrite.sh \\n#!/bin/bash\\nn=1\\nwhile [ \$n -le 1000 ]\\ndo\\n mysql -h 127.0.0.1 -uroot --port 3307 -proot -Ddistsql_rwsplit_db -e &quot;insert into wr_table values(\$n,\$n,'ok');&quot;\\n mysql -h 127.0.0.1 -uroot --port 3307 -proot -Ddistsql_rwsplit_db -e &quot;select count(*) from wr_table;&quot;\\n let n++\\ndone \\n</code></pre>\\n<p>查看 Aurora 集群的写节点和读节点的读写延迟,可以看到写延迟只在写节点上发生,读延迟只在读节点上发生。说明读写分离规则生效。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/b172db7c626e435dab235cf3a26c3e33_image.png\\" alt=\\"image.png\\" /></p>\n<p>尽管 Aurora 的写和读节点之间的复制延迟很低在毫秒级别,但某些应用还是会有强一致性的需求,即要求写后立刻可以读。这时候,可以采用强制将读请求发送到写节点的方式。ShardingSphereProxy 通过 hint 的方式来支持。首先需要在前面提到的 conf/server.yaml 里添加一个属性 proxy-hint-enabled: true。然后在连接中显式设置 readwrite_splitting hint source 值为 write 来开启强制路由到写节点通过设置值为 auto 或者 clear hint 可以采用默认的规则。readwrite_splitting hint source 可以在 session 级别生效。</p>\n<p>在 ShardingSphere-Proxy 上依次运行下面语句。可以看到默认的读请求是发送到读节点,将 readwrite_splitting hint source 设置为 write 以后,会发送到写节点,再设成 auto,可以发回至读写点。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_rwsplit_db]&gt; preview select count(*) from wr_table;\\n+------------------+-------------------------------+\\n| data_source_name | sql |\\n+------------------+-------------------------------+\\n| read_ds | select count(*) from wr_table |\\n+------------------+-------------------------------+\\n1 row in set (0.01 sec)\\n\\nMySQL [distsql_rwsplit_db]&gt; set readwrite_splitting hint source = write;\\nQuery OK, 0 rows affected (0.00 sec)\\n\\nMySQL [distsql_rwsplit_db]&gt; preview select count(*) from wr_table;\\n+------------------+-------------------------------+\\n| data_source_name | sql |\\n+------------------+-------------------------------+\\n| write_ds | select count(*) from wr_table |\\n+------------------+-------------------------------+\\n1 row in set (0.01 sec)\\nMySQL [distsql_rwsplit_db]&gt; set readwrite_splitting hint source = auto;\\nQuery OK, 0 rows affected (0.00 sec)\\n\\nMySQL [distsql_rwsplit_db]&gt; preview select count(*) from wr_table;\\n+------------------+-------------------------------+\\n| data_source_name | sql |\\n+------------------+-------------------------------+\\n| read_ds | select count(*) from wr_table |\\n+------------------+-------------------------------+\\n1 row in set (0.00 sec)\\n</code></pre>\\n<p>另外不使用 YAML 文件更改的方式是直接在 DistSQL 里先后设置两个变量 proxy_hint_enabled 和 readwrite_splitting hint source。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_rwsplit_db]&gt; set variable proxy_hint_enabled=true;\\nQuery OK, 0 rows affected (0.01 sec)\\nMySQL [distsql_rwsplit_db]&gt; set readwrite_splitting hint source = write;\\nQuery OK, 0 rows affected (0.01 sec)\\nMySQL [distsql_rwsplit_db]&gt; preview select * from wr_table;\\n+------------------+------------------------+\\n| data_source_name | sql |\\n+------------------+------------------------+\\n| write_ds | select * from wr_table |\\n+------------------+------------------------+\\n1 row in set (0.00 sec)\\n</code></pre>\\n<p>以上实验验证了 ShardingSphere-Proxy 有良好的读写分离的能力。它验证了底下连接单个 Aurora 集群进行读写分离的场景。如果既需要分库分表又需要读写分离,ShardingSphere-Proxy 也是支持的。比如先分到3个 Aurora 集群,然后每个集群需要提供读写分离的能力,我们可以直接将读写分离规则后面定义的数据源名称(4.4.4里的 wr_ds)放在分库分表规则对每张表指定的数据源里(4.1.3里的ds_0,ds_1,ds_2)。</p>\n<h4><a id=\\"45__621\\"></a><strong>4.5 故障恢复验证</strong></h4>\\n<p>本节来验证 ShardingSphere-Proxy 对于 Aurora 集群故障切换的感知能力。在 Aurora 集群发生主备切换时,如果 Proxy 能够动态检测到主备切换并连接到新的主数据库是比较理想的。本节实验仍然是验证第一个 Aurora 集群。</p>\n<p>测试脚本如下,它会持续连接到写节点并发送 update 请求,每次请求间隔1秒钟。</p>\n<pre><code class=\\"lang-\\">[ec2-user@ip-111-22-3-123 shardingproxy]\$ cat testFailover.sh \\n#!/bin/bash\\nwhile true\\ndo\\n mysql -h 127.0.0.1 -uroot --port 3307 -proot -Ddistsql_rwsplit_db -e &quot;update wr_table set c='failover' where a = 1;&quot;\\n now=\$(date +&quot;%T&quot;)\\n echo &quot;update done: \$now&quot;\\n sleep 1\\ndone \\n</code></pre>\\n<p>运行脚本,然后在 Aurora 集群的写节点上点击 Action-&gt;Failover。会启动 Aurora 写节点和读节点的自动切换。在切换过程中,整个集群的读/写 endpoint 和只读 endpoint 维持不变,只是底层映射的节点发生变化。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/19051948af6442cfa2f3e41daed8bf22_image.png\\" alt=\\"image.png\\" /></p>\n<p>通过观测 Aurora 的 Event(事件),可以看到整个故障切换在30秒左右完成。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/e807f96c1f3a4039a9e4bdcff0f4b640_image.png\\" alt=\\"image.png\\" /></p>\n<p>遗憾的是,应用程序直接连接 ShardingSphereProxy 也就是前面的运行脚本不能自动监测到底层的 IP 变化。运行脚本一直抛错:</p>\n<pre><code class=\\"lang-\\">ERROR 1290 (HY000) at line 1: The MySQL server is running with the --read-only option so it cannot execute this statement\\nupdate done: 15:04:04\\nERROR 1290 (HY000) at line 1: The MySQL server is running with the --read-only option so it cannot execute this statement\\nupdate done: 15:04:05\\nERROR 1290 (HY000) at line 1: The MySQL server is running with the --read-only option so it cannot execute this statement\\nupdate done: 15:04:06\\n</code></pre>\\n<p>直接在 MySQL 命令行连接到 Proxy 也是会有一样的错误。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_rwsplit_db]&gt; update wr_table set c=&quot;failover&quot; where a =2;\\nERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement\\n</code></pre>\\n<p>分析原因在于 Aurora 发生故障切换的时候,读写 endpoint 和 IP 的映射会发生变化,而 ShardingSphere 的连接池在连接 Aurora 的时候,没有更新到新的 IP 上。我们可以采用下面的 workaround 可以使 ShardingSphereProxy 指向新的写节点,即重新创建数据源。尽管数据源本身定义没有发生变化,但是通过重建数据源 alter resource 的操作, ShardingSphereProxy 会重新拿取一遍 endpoint 到 IP 的映射,所以能够成功运行。</p>\n<pre><code class=\\"lang-\\">MySQL [distsql_rwsplit_db]&gt; alter resource write_ds(url=&quot;jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&amp;useSSL=false&quot;,user=admin,password=12345678), read_ds(url=&quot;jdbc:mysql://aurora-2-07-7-shard1.cluster-ro-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&amp;useSSL=false&quot;,user=admin,password=12345678);\\nQuery OK, 0 rows affected (0.05 sec)\\n\\nMySQL [distsql_rwsplit_db]&gt; update wr_table set c=&quot;failover&quot; where a =2;\\nQuery OK, 1 row affected (0.01 sec)\\n</code></pre>\\n<p>每次 Aurora 故障切换时,我们可以检测故障切换的 event,或者是在应用收到 read-only 报错时显式调用上面语句。为了降低对应用的影响,我们可以采用 Lambda 的方式将 failover 重置数据源的操作自动化。因为 Aurora 的 failover 的事件是可以被监测到的,我们可以写一个Lambda函数,在监测到 failover 成功以后,显示调用更改 resource 的操作。</p>\n<p>总体的思路是: RDS 通过 Event Subscription 将 Event 的通知信息传递给 SNS topic,再由 SNS topic 传递给 Lambda 方法,然后在 Lambda 方法里显式连接 ShardingProxy 调用 alter resource 的 DistSQL 语句。</p>\n<p>具体步骤如下:</p>\n<h5><a id=\\"451__SNS_680\\"></a><strong>4.5.1 创建 SNS</strong></h5>\\n<p>按照 SNS 创建指南创建 SNS。打开 SNS 的 dashboard,点击创建 SNS topic,选择 Standard 标准类型。其它选择默认或者根据需要调整。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/47aa8109ccac434ca7efa5f16832fe40_image.png\\" alt=\\"image.png\\" /></p>\n<h5><a id=\\"452__Aurora__Event_Subscription_685\\"></a><strong>4.5.2 对要进行的 Aurora 集群创建 Event Subscription</strong></h5>\\n<p>在 RDS 的 Event Subscriptions 上,点击“Create Event Subscription”,在弹出的选项卡中选择 Target 为上一步骤创建的SNS,Source type 选择为 Cluster,cluster 里面选中我们需要关注的 Aurora 的集群,事件选择 Failover 事件。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/11ad9f0489084a048ad6b98e6e91dbf2_image.png\\" alt=\\"image.png\\" /></p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/e90e9c38e9ee4f60aae38cdb8c6ec7ba_image.png\\" alt=\\"image.png\\" /></p>\n<h5><a id=\\"453__Lamdba__693\\"></a><strong>4.5.3 创建 Lamdba 方法</strong></h5>\\n<p>因为 Lambda 要调用 VPC 里的 EC2上部署的 ShardingProxy,应该给它绑定一个专门的 Role,这个 Role 有权限在 VPC 里执行 Lambda 方法: 百尺竿头更进一步 – Amazon Aurora 的读写能力扩展之 ShardingSphere-Proxy 篇LambdaVPCAccessExecutionRole 按照 <a href=\\"https://docs.aws.amazon.com/zh_cn/IAM/latest/UserGuide/id_roles_create_for-service.html\\" target=\\"_blank\\">IAM Role 创建文档</a>创建 Role 和 Policy,使 failoverlambda 的 role 有 百尺竿头更进一步 – [Amazon Aurora](https://aws.amazon.com/cn/rds/aurora/?trk=cndc-detail) 的读写能力扩展之 ShardingSphere-Proxy 篇LambdaVPCAccessExecutionRole 的权限。</p>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/db2ecbc5e69c4254b2efafd57c4372f9_image.png\\" alt=\\"image.png\\" /></p>\n<p>接下来按照 Lambda 文档创建 Lambda 方法</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/ff1f2d3b57c3419dae2c38e866da074b_image.png\\" alt=\\"image.png\\" /></p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/f74a45b15a5447c6bbc55fd38211bcc4_image.png\\" alt=\\"image.png\\" /></p>\n<p>创建好 Lambda 方法以后,点击 Trigger,指明为 SNS,并指明在4.5.1里创建的 SNS topic。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/9389b236620c490da7d6ebf57dca4397_image.png\\" alt=\\"image.png\\" /></p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/c34de3a3b83d49018ca199f0275b3e3f_image.png\\" alt=\\"image.png\\" /></p>\n<h5><a id=\\"454_Lambda_710\\"></a><strong>4.5.4 编写Lambda方法</strong></h5>\\n<pre><code class=\\"lang-\\">import os\\nimport json\\nimport pymysql\\n\\n# connect to ShardingProxy to reset the data source \\ndef resetDataSource():\\n db = pymysql.connect(host='111.22.3.123', user='root', password='root', port=3307, database='distsql_rwsplit_db')\\n \\n cur = db.cursor()\\n SQL = &quot;alter resource write_ds(url=\\\\&quot;jdbc:mysql://aurora-2-07-7-shard1.cluster-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&amp;useSSL=false\\\\&quot;,user=admin,password=12345678), read_ds(url=\\\\&quot;jdbc:mysql://aurora-2-07-7-shard1.cluster-ro-12345678.us-east-1.rds.amazonaws.com:3306/wr_ds?serverTimezone=UTC&amp;useSSL=false\\\\&quot;,user=admin,password=12345678);&quot;\\n print (SQL)\\n cur.execute(SQL)\\n result = cur.fetchall()\\n for x in result:\\n print(x)\\n db.close()\\n\\ndef lambda_handler(event, context):\\n wholeMessage = event['Records'][0]['Sns']['Message']\\n print (&quot;whole message&quot; + wholeMessage)\\n \\n wholeMessageInJson = json.loads(wholeMessage)\\n eventMessage = wholeMessageInJson['Event Message']\\n print (&quot;event message: &quot; + eventMessage)\\n \\n isFailover = eventMessage.startswith('Completed failover to DB instance')\\n if isFailover == True:\\n print (&quot;Failover completed! &quot; + eventMessage)\\n resetDataSource()\\n\\n return {\\n 'statusCode': 200,\\n 'body': Lambda Invocation Successful!'\\n }\\n</code></pre>\\n<p>Lambda 方法是 Python 语言书写的,在访问 ShardingSphereProxy 时以 MySQL 方式访问,所以需要引入 pymysql 的 lib 库。具体方法为:</p>\n<p>1)在 Linux 上安装 pymysql,以 Amazon-Linux 虚拟机为例,会默认安装到目录 ./home/ec2-user/.local/lib/python3.7/site-packages/pymysql 下</p>\n<p>2)将 pymysql 目录拷贝到临时目录 /tmp</p>\n<p>3)写 Lambda 方法,存储到 lambda_function.py 文件中</p>\n<p>4)打包 zip -r lambda.zip pymysql lambda_function.py</p>\n<p>5) 在控制台通过 S3 或者本地上传。</p>\n<h5><a id=\\"455__ShardingSphereProxy__EC2security_group_761\\"></a><strong>4.5.5 设置 ShardingSphereProxy 所在 EC2的security group</strong></h5>\\n<p>因为 Lambda 要在 VPC 里访问 ShardingSphereProxy,而 ShardingSphereProxy 以3307端口运行,应该配置相应 secuity group,打开3307端口给同一个 VPC 内部的访问。依据安全组配置文档配置成的 security group 如下:</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/5a0f23e596c84b2d91c5f3b74b3965be_image.png\\" alt=\\"image.png\\" /></p>\n<h5><a id=\\"456__failover_766\\"></a><strong>4.5.6 验证 failover</strong></h5>\\n<p>重复本小节开始的操作,运行 testFailover.sh,然后手动在 RDS console 页 failover Aurora 节点,会发现 testFailover.sh 持续稳定输出, 不会再出现 read-only 的错误。</p>\n<pre><code class=\\"lang-\\">update done: 13:44:44\\n…\\nupdate done: 13:44:47\\nupdate done: 13:45:00\\nupdate done: 13:45:01\\n…\\nupdate done: 13:45:17\\n</code></pre>\\n<p>去 cloudwatch 里查看 Lambda function 的日志,会发现 Lambda 被成功调用。</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/64bca78e3ff14659b7191cb7f4a865ca_image.png\\" alt=\\"image.png\\" /></p>\n<p>以上实验验证了 ShardingSphere-Proxy 对于 Aurora 集群故障切换的感知能力。尽管 ShardingSphere-Proxy 自己没有提供良好的匹配性,通过监测 Aurora 集群事件触发 Lamdba 方法来显式重置 ShardingSphere-Proxy 数据源的方式,我们可以实现 ShardingSphere-Proxy 与 Aurora 结合的故障切换能力。</p>\n<h3><a id=\\"5_785\\"></a><strong>5.结语</strong></h3>\\n<p>本篇文章通过数据库中间件 ShardingSphere-Proxy 拓展了 Aurora 的分库分表能力和读写分离的能力。</p>\n<p>ShardingSphere-Proxy 内置连接池,对 MySQL 语法支持较强,在分库分表和读写分离上表现出色。它对多表 join 上,可以支持分片规则相同的表的 join,以及小表和大表的 join,基本能满足 OLTP 场景的需求。在动态分片上,ShardingSphere-Proxy 提供在线更改分片规则的能力,但需要用户在底层 Aurora 集群手动操作子表创建及数据迁移,需要一定工作量。故障切换维度,ShardingSphere-Proxy 与 Aurora 的融合不是很好,但是可以通过本文提供的 Aurora 故障切换 Event 调用 Lambda 方法来显式重置数据源的方式,实现 ShardingSphere-Proxy 对 Aurora 集群故障切换对感知。</p>\n<p>总体而言,ShardingSphere-Proxy 这个中间件产品还是能与 Aurora 集群进行一个良好匹配,进一步提升 Aurora 集群的读写能力的。它有良好的文档,也是比较受关注的开源产品,建议读者在考虑 Aurora 分库分表实现时,评估下这个产品。后续我们会继续推出对其他中间件以及 JDBC 方面的拓展和研究系列博客。</p>\n<p><strong>本篇作者</strong></p>\\n<p><strong>马丽丽</strong></p>\\n<p><em>亚马逊云科技数据库解决方案架构师,十余年数据库行业经验,先后涉猎 NoSQL 数据库 Hadoop/Hive、企业级数据库 DB2、分布式数仓 Greenplum/Apache HAWQ 以及亚马逊云原生数据库的开发和研究。</em></p>\\n<p>点击链接,了解更多内容:</p>\n<ul>\\n<li>Apache ShardingSphere GitHub 地址:<a href=\\"https://github.com/apache/shardingsphere\\" target=\\"_blank\\">https://github.com/apache/shardingsphere</a></li>\\n<li>SphereEx 官网:<a href=\\"https://www.sphere-ex.com\\" target=\\"_blank\\">https://www.sphere-ex.com</a></li>\\n</ul>\n<p><img src=\\"https://dev-media.amazoncloud.cn/7886a968195c46c2ab3dbf2e01a01f22_image.png\\" alt=\\"image.png\\" /></p>\n"}
0
目录
关闭