免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3783 | 回复: 1

kingshard简介 [复制链接]

求职 : Linux运维
论坛徽章:
203
拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之约旦
日期:2015-04-05 20:08:292015年亚洲杯之澳大利亚
日期:2015-04-09 09:25:552015年亚洲杯之约旦
日期:2015-04-10 17:34:102015年亚洲杯之巴勒斯坦
日期:2015-04-10 17:35:342015年亚洲杯之日本
日期:2015-04-16 16:28:552015年亚洲杯纪念徽章
日期:2015-04-27 23:29:17操作系统版块每日发帖之星
日期:2015-06-06 22:20:00操作系统版块每日发帖之星
日期:2015-06-09 22:20:00
发表于 2016-04-11 18:34 |显示全部楼层
kingshard(https://github.com/flike/kingshard)是一个由Go开发高性能MySQL Proxy项目,kingshard在满足基本的读写分离的功能上,致力于简化MySQL分库分表操作;能够让DBA通过kingshard轻松平滑地实现MySQL数据库扩容。

主要功能:
1.读写分离。
2.跨节点分表。
3.客户端IP访问控制。
4.平滑上线DB或下线DB,前端应用无感知。
kingshard sharding介绍
现在开源的MySQL Proxy已经有几款了,并且有的已经在生产环境上广泛应用。但这些proxy在sharding方面,都是不能分子表的。也就是说一个node节点只能分一张表。但我们的线上需求通常是这样的:

我有一张非常大的表,行数超过十亿,需要进行拆分处理。假设拆分因子是512。
如果采用单node单数据库的分表方式,那其实这512个子表还是存在一个物理节点上,意义不大。
如果采用他们的sharding功能,就需要512个物理节点,也不现实。
面对这种需求,现有的proxy就不能很好地满足要求了。通常我们希望将512张子表均分在几个MySQL节点上,从而达到系统的横向扩展。

然而kingshard较好地实现了这种典型的需求。简单来说,kingshard的分表方案采用两级映射的方式:

1.kingshard将该表分成512张子表,例如:test_0000,test_0001,...
test_511。
2.将shardKey通过hash或range方式定位到其要操作的记录在哪张子表上。
3.子表落在哪个node上通过配置文件设置。
sharding支持的操作
目前kingshard sharding支持insert, delete, select, update和replace语句, 所有这五类操作都支持跨子表。但写操作仅支持单node上的跨子表,select操作则可以跨node,跨子表。

sharding方式
range方式
基于整数范围划分来得到子表下标。该方式的优点:基于范围的查询或更新速度快,因为查询(或更新)的范围有可能落在同一张子表中。这样可以避免全部子表的查询(更新)。缺点:数据热点问题。因为在一段时间内整个集群的写压力都会落在一张子表上。此时整个mysql集群的写能力受限与单台mysql server的性能。并且,当正在集中写的mysql 节点如果宕机的话,整个mysql集群处于不可写状态。基于range方式的分表字段类型受限。

hash方式
kingshard采用(shardKey%子表个数)的方式得到子表下标。优点:数据分布均匀,写压力会比较平均地落在后端的每个MySQL节点上,整个集群的写性能不会受限于单个MySQL节点。并且当某个分片节点宕机,只会影响到写入该节点的请求,其他节点的写入请求不受影响。分表字段类型不受限。因为任何一个类型的分表字段,都可以通过一个hash函数计算得到一个整数。缺点:基于范围的查询或更新,都需要将请求发送到全部子表,对性能有一定影响。但如果不是基于范围的查询或更新,则性能不会受到影响。

sharding相关的配置介绍
在配置文件中,有关sharding设置是通过scheam设置:

```

schemas :
db : kingshard
nodes: [node1,node2]
rules:
    default: node1
    shard:
    -   
        #分表名字
        table: test_shard_hash
        #sharding key
        key: id
        #子表分布的节点名字
        nodes: [node1, node2]
        #sharding类型
        type: hash
        #子表个数分布,表示[test_shard_hash_0000, test_shard_hash_0001, test_shard_hash_0002, test_shard_hash_003]在node1上。
        #[test_shard_hash_0004, test_shard_hash_0005, test_shard_hash_0006, test_shard_hash_007]在node2上
        locations: [4,4]

    -   
         #分表名字
        table: test_shard_range
        #sharding key
        key: id
        #sharding类型
        type: range
        #子表分布的节点名字
        nodes: [node1, node2]
        #子表个数分布,表示[test_shard_hash_0000, test_shard_hash_0001, test_shard_hash_0002, test_shard_hash_003]在node1上。
        #[test_shard_hash_0004, test_shard_hash_0005, test_shard_hash_0006, test_shard_hash_007]在node2上
        locations: [4,4]
        #每张子表的记录数。[0,10000)在test_shard_hash_0000上,[10000,20000)在test_shard_hash_0001上。....
        table_row_limit: 10000
```
一个kingshard实例只能有一个schemas,从上面的配置可以看出,schema可以分为三个部分:

1.db,表示这个schemas使用的数据库。

2.nodes,表示子表分布的节点名字。

3.rules,sharding规则。其中rules又可以分为两个部分:
    - default,默认分表规则。所有操作不在shard(default规则下面的规则)中的表的SQL语句都会发向该node。
    - hash,hash分表方式。
    - range,range分表方式
kingshard架构图


基于kingshard的子表迁移方案
通过kingshard可以非常方便地动态迁移子表,从而保证MySQL节点的不至于负载压力太大。大致步骤如下所述:

通过自动数据迁移工具开始数据迁移。
数据差异小于某一临界值,阻塞老子表写操作(read-only)
等待新子表数据同步完毕
更改kingshard配置文件中的对应子表的路由规则。
删除老节点上的子表。
Exaple
简单演示一下kingshard的相关操作,感兴趣的同学可以自己试一试。

#启动kingshard
kingshard gitmaster) ✗ ./bin/kingshard -config=etc/multi.yaml
kingshard
2015/07/19 11:13:43 - INFO - server.go:[205] - [server] "NewServer" "Server running" "netProto=tcp|address=127.0.0.1:9696" conn_id=0

#另一个终端连接kingshard
mysql -ukingshard -pkingshard -h127.0.0.1 -P9696;
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10001
Server version: kingshard-1.0 Homebrew

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>use kingshard;
Database changed
mysql> select/*master*/ * from kingshard_test_conn;
+----+----------+------+-------+------+------+
| id | str      | f    | e     | u    | i    |
+----+----------+------+-------+------+------+
|  1 | a        | 3.14 | test1 | NULL | NULL |
|  5 | ""''\abc | NULL | NULL  | NULL | NULL |
|  6 | 中国     | NULL | NULL  | NULL | NULL |
+----+----------+------+-------+------+------+
3 rows in set (0.01 sec)

mysql> select * from test_shard_hash where id in(6,10);
+----+-------+------+-------+------+------+
| id | str   | f    | e     | u    | i    |
+----+-------+------+-------+------+------+
| 10 | world |  2.1 | test1 |    1 |    1 |
+----+-------+------+-------+------+------+
1 row in set (0.03 sec)

mysql> show tables;
+----------------------------+
| Tables_in_kingshard        |
+----------------------------+
| kingshard_test_conn        |
| kingshard_test_proxy_conn  |
| kingshard_test_proxy_stmt  |
| kingshard_test_shard_hash  |
| kingshard_test_shard_range |
| kingshard_test_stmt        |
| test_shard_hash_0000       |
| test_shard_hash_0001       |
| test_shard_hash_0002       |
| test_shard_hash_0003       |
| test_shard_range_0000      |
| test_shard_range_0001      |
| test_shard_range_0002      |
| test_shard_range_0003      |
+----------------------------+
14 rows in set (0.00 sec)

反馈
目前kingshard还是1.0版本,比较核心的功能已经实现了。但还有很多地方不完善。如果您在使用kingshard的过程中发现BUG或者有新的功能需求,非常欢迎您发邮件至flikecn#126.com与作者取得联系,或者加入QQ群(147926796)交流。

github:https://github.com/flike/kingshard

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
发表于 2016-04-12 13:52 |显示全部楼层
这不错,代码那些用 code 标签括起来就更美观啦
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP