kingshard--一个支持sharding的MySQL Proxy项目
kingshard簡(jiǎn)介
kingshard(https://github.com/flike/king...)是一個(gè)由Go開發(fā)高性能MySQL Proxy項(xiàng)目,kingshard在滿足基本的讀寫分離的功能上,致力于簡(jiǎn)化MySQL分庫分表操作;能夠讓DBA通過kingshard輕松平滑地實(shí)現(xiàn)MySQL數(shù)據(jù)庫擴(kuò)容。
主要功能:
1.讀寫分離。 2.跨節(jié)點(diǎn)分表。 3.客戶端IP訪問控制。 4.平滑上線DB或下線DB,前端應(yīng)用無感知。kingshard sharding介紹
現(xiàn)在開源的MySQL Proxy已經(jīng)有幾款了,并且有的已經(jīng)在生產(chǎn)環(huán)境上廣泛應(yīng)用。但這些proxy在sharding方面,都是不能分子表的。也就是說一個(gè)node節(jié)點(diǎn)只能分一張表。但我們的線上需求通常是這樣的:
**我有一張非常大的表,行數(shù)超過十億,需要進(jìn)行拆分處理。假設(shè)拆分因子是512。
如果采用單node單數(shù)據(jù)庫的分表方式,那其實(shí)這512個(gè)子表還是存在一個(gè)物理節(jié)點(diǎn)上,意義不大。
然而kingshard較好地實(shí)現(xiàn)了這種典型的需求。簡(jiǎn)單來說,kingshard的分表方案采用兩級(jí)映射的方式:
1.kingshard將該表分成512張子表,例如:test_0000,test_0001,... test_511。 2.將shardKey通過hash或range方式定位到其要操作的記錄在哪張子表上。 3.子表落在哪個(gè)node上通過配置文件設(shè)置。sharding支持的操作
目前kingshard sharding支持insert, delete, select, update和replace語句, 所有這五類操作都支持跨子表。但寫操作僅支持單node上的跨子表,select操作則可以跨node,跨子表。
sharding方式
range方式
基于整數(shù)范圍劃分來得到子表下標(biāo)。該方式的優(yōu)點(diǎn):基于范圍的查詢或更新速度快,因?yàn)椴樵?#xff08;或更新)的范圍有可能落在同一張子表中。這樣可以避免全部子表的查詢(更新)。缺點(diǎn):數(shù)據(jù)熱點(diǎn)問題。因?yàn)樵谝欢螘r(shí)間內(nèi)整個(gè)集群的寫壓力都會(huì)落在一張子表上。此時(shí)整個(gè)mysql集群的寫能力受限與單臺(tái)mysql server的性能。并且,當(dāng)正在集中寫的mysql 節(jié)點(diǎn)如果宕機(jī)的話,整個(gè)mysql集群處于不可寫狀態(tài)。基于range方式的分表字段類型受限。
hash方式
kingshard采用(shardKey%子表個(gè)數(shù))的方式得到子表下標(biāo)。優(yōu)點(diǎn):數(shù)據(jù)分布均勻,寫壓力會(huì)比較平均地落在后端的每個(gè)MySQL節(jié)點(diǎn)上,整個(gè)集群的寫性能不會(huì)受限于單個(gè)MySQL節(jié)點(diǎn)。并且當(dāng)某個(gè)分片節(jié)點(diǎn)宕機(jī),只會(huì)影響到寫入該節(jié)點(diǎn)的請(qǐng)求,其他節(jié)點(diǎn)的寫入請(qǐng)求不受影響。分表字段類型不受限。因?yàn)槿魏我粋€(gè)類型的分表字段,都可以通過一個(gè)hash函數(shù)計(jì)算得到一個(gè)整數(shù)。缺點(diǎn):基于范圍的查詢或更新,都需要將請(qǐng)求發(fā)送到全部子表,對(duì)性能有一定影響。但如果不是基于范圍的查詢或更新,則性能不會(huì)受到影響。
sharding相關(guān)的配置介紹
在配置文件中,有關(guān)sharding設(shè)置是通過scheam設(shè)置:
schemas : -db : kingshardnodes: [node1,node2]rules:default: node1shard:- #分表名字table: test_shard_hash#sharding keykey: id#子表分布的節(jié)點(diǎn)名字nodes: [node1, node2]#sharding類型type: hash#子表個(gè)數(shù)分布,表示[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 keykey: id#sharding類型type: range#子表分布的節(jié)點(diǎn)名字nodes: [node1, node2]#子表個(gè)數(shù)分布,表示[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]#每張子表的記錄數(shù)。[0,10000)在test_shard_hash_0000上,[10000,20000)在test_shard_hash_0001上。....table_row_limit: 10000一個(gè)kingshard實(shí)例只能有一個(gè)schemas,從上面的配置可以看出,schema可以分為三個(gè)部分:
1.db,表示這個(gè)schemas使用的數(shù)據(jù)庫。2.nodes,表示子表分布的節(jié)點(diǎn)名字。3.rules,sharding規(guī)則。其中rules又可以分為兩個(gè)部分:- default,默認(rèn)分表規(guī)則。所有操作不在shard(default規(guī)則下面的規(guī)則)中的表的SQL語句都會(huì)發(fā)向該node。- hash,hash分表方式。- range,range分表方式kingshard架構(gòu)圖
基于kingshard的子表遷移方案
通過kingshard可以非常方便地動(dòng)態(tài)遷移子表,從而保證MySQL節(jié)點(diǎn)的不至于負(fù)載壓力太大。大致步驟如下所述:
Exaple
簡(jiǎn)單演示一下kingshard的相關(guān)操作,感興趣的同學(xué)可以自己試一試。:)
#啟動(dòng)kingshard kingshard git:(master) ? ./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#另一個(gè)終端連接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 HomebrewCopyright (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 | 中國(guó) | 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)反饋
非常歡迎您發(fā)郵件至flikecn#126.com與作者取得聯(lián)系,或者加入QQ群(147926796)交流。
github:https://github.com/flike/king...
歡迎關(guān)注后端技術(shù)快訊公眾號(hào),有關(guān)kingshard的最新消息與后端架構(gòu)設(shè)計(jì)類的文章,都會(huì)在這個(gè)公眾號(hào)分享。
總結(jié)
以上是生活随笔為你收集整理的kingshard--一个支持sharding的MySQL Proxy项目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache CXF实现WebServi
- 下一篇: [EF]vs15+ef6+mysql这个