在 mysql shardingsphere-proxy 逐漸成熟并被廣泛采用的同時,shardingsphere 團隊也在 postgresql shardingsphere-proxy 上持續(xù)發(fā)力。相比前期的 alpha 與 beta,5.0.0 正式版對 postgresql 的協(xié)議實現(xiàn)、sql 支持度、權限控制等方面進行了大量的完善,為后續(xù)全面對接 postgresql 生態(tài)打下基礎。shardingsphere-proxy 與 postgresql 的生態(tài)對接,讓用戶能夠在 postgresql 數(shù)據(jù)庫的基礎上獲得如數(shù)據(jù)分片、讀寫分離、影子庫、數(shù)據(jù)加密/脫敏、分布式治理等透明化的增量能力。
除了 postgresql 方面,由華為開源的國產(chǎn)數(shù)據(jù)庫 opengauss 的熱度持續(xù)攀升。opengauss 具備優(yōu)秀的單機性能,配合 shardingsphere 的能力和生態(tài),能夠打造出覆蓋更多場景的國產(chǎn)分布式數(shù)據(jù)庫解決方案。
shardingsphere postgresql/opengauss proxy 目前能夠支持數(shù)據(jù)分片、讀寫分離、影子庫、數(shù)據(jù)加密/脫敏、分布式治理等 apache shardingsphere 生態(tài)中大部分能力,在完善程度上逐漸對齊 shardingsphere mysql proxy。
本文將給大家介紹 shardingsphere-proxy 5.0.0 在 postgresql 上所做的提升以及與 opengauss 的生態(tài)對接。
作者介紹
吳偉杰
apache shardingsphere committer,sphereex 中間件工程師。目前專注于 apache shardingsphere 及其子項目 elasticjob 的研發(fā)。
shardingsphere-proxy 介紹
shardingsphere-proxy 是 shardingsphere 生態(tài)中的一個接入端,定位為對客戶端透明的數(shù)據(jù)庫代理。shardingsphere proxy 不局限于 java,其實現(xiàn)了 mysql、postgresql 數(shù)據(jù)庫協(xié)議,可以使用各種兼容 mysql / postgresql 協(xié)議的客戶端連接并操作數(shù)據(jù)。
shardingsphere-jdbc | shardingsphere-proxy | |
---|---|---|
數(shù)據(jù)庫 | 任意 | 基于 mysql / postgresql 協(xié)議的數(shù)據(jù)庫 |
連接消耗數(shù) | 高 | 低 |
異構語言 | 支持 java 等基于 jvm 語言 | 任意 |
性能 | 損耗低 | 損耗略高 |
無中心化 | 是 | 否 |
靜態(tài)入口 | 無 | 有 |
在做了分庫分表或其他規(guī)則的情況下,數(shù)據(jù)會分散到多個數(shù)據(jù)庫實例上,在管理上難免會有一些不便;或者使用非 java 語言的開發(fā)者,需要 shardingsphere 所提供的能力…… 以上這些情況,正是 shardingsphere-proxy 力所能及之處。
shardingsphere-proxy 隱藏了后端實際數(shù)據(jù)庫,對于客戶端來說就是在使用一個數(shù)據(jù)庫,不需要關心 shardingsphere 如何協(xié)調(diào)背后的數(shù)據(jù)庫,對于使用非 java 語言的開發(fā)者或 dba 更友好。
在協(xié)議方面,shardingsphere postgresql proxy 實現(xiàn)了 simple query 與大部分 extended query 協(xié)議,支持異構語言通過 postgresql/opengauss 驅動連接 proxy。shardingsphere opengauss proxy 在復用 postgresql 協(xié)議的基礎上,還支持 opengauss 特有的批量插入?yún)f(xié)議。
不過,由于 shardingsphere-proxy 相比 shardingsphere-jdbc 增加了一層網(wǎng)絡交互,sql 執(zhí)行的延時會有所增加,損耗相比 shardingsphere-jdbc 略高。
shardingsphere-proxy 與 postgresql 的生態(tài)對接
兼容 postgresql simple query 與 extended query
simple query 與 extended query 是大多數(shù)用戶在使用 postgresql 時最常用的協(xié)議。
比如,使用如下命令行工具 psql
連接 postgresql 數(shù)據(jù)庫進行 crud 操作時,主要使用 simple query 協(xié)議與數(shù)據(jù)庫交互。
1
2
3
4
5
6
7
8
|
$ psql -h 127.0.0.1 -u postgres psql (14.0 (debian 14.0-1.pgdg110+1)) type "help" for help. postgres=# select id, name from person where age < 35; id | name ----+------ 1 | foo (1 row) |
simple query 的協(xié)議交互示意圖如下:
當用戶使用 postgresql jdbc driver 等驅動時,可能會如下代碼使用 preparedstatement,默認情況下對應著 extended query 協(xié)議。
1
2
3
4
|
string sql = "select id, name from person where age > ?" ; preparedstatement ps = connection .preparestatement(sql); ps.setint(1, 35); resultset resultset = ps.executequery(); |
extended query 的協(xié)議交互示意圖如下:
目前,shardingsphere postgresql proxy 實現(xiàn)了 simple query 與大部分 extended query 協(xié)議,不過,因為數(shù)據(jù)庫客戶端與驅動已經(jīng)封裝好 api 供用戶使用,一般用戶并不需要關心數(shù)據(jù)庫協(xié)議層面的事情。
shardingsphere-proxy 兼容 postgresql 的 simple query 與 extended query 意味著:用戶可以使用常見的 postgresql 客戶端或驅動連接 shardingsphere-proxy 進行 crud 操作,利用 shardingsphere 在數(shù)據(jù)庫上層提供的增量能力。
shardingsphere-proxy 與 opengauss 的生態(tài)對接
支持 opengauss jdbc driver
opengauss 數(shù)據(jù)庫有對應的 jdbc 驅動,jdbc url 的前綴jdbc:opengauss
。雖然用 postgresql 的 jdbc 驅動也能夠連接 opengauss 數(shù)據(jù)庫,但這樣就無法完全利用 opengauss 特有的批量插入等特性。shardingsphere 增加了 opengauss 數(shù)據(jù)庫類型,能夠識別 opengauss jdbc driver,開發(fā)者在使用 shardingsphere 的時候可以直接使用 opengauss 的 jdbc 驅動。
支持 opengauss 批量插入?yún)f(xié)議
舉一個例子,當我們 prepare 一個 insert 語句如下
1
|
insert into person (id, name , age) values (?, ?, ?) |
以 jdbc 為例,我們可能會使用如下方法執(zhí)行批量插入:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
string sql = "insert into person (id, name, age) values (?, ?, ?)" ; preparedstatement ps = connection .preparestatement(sql); ps.setlong(1, 1); ps.setstring(2, "foo" ); ps.setint(3, 18); ps.addbatch(); ps.setlong(1, 2); ps.setstring(2, "bar" ); ps.setint(3, 36); ps.addbatch(); ps.setlong(1, 3); ps.setstring(2, "tom" ); ps.setint(3, 54); ps.addbatch(); ps.executebatch(); |
在 postgresql 協(xié)議層面,bind
消息每次能夠傳遞一組參數(shù)形成 portal,execute
每次能夠執(zhí)行一個 portal。執(zhí)行批量插入可以通過反復執(zhí)行 bind
和 execute
實現(xiàn)。協(xié)議交互示意圖如下:
batch bind
是 opengauss 特有的消息類型,相比原本的 bind
,batch bind
一次能夠傳遞多組參數(shù),使用 batch bind
執(zhí)行批量插入的協(xié)議交互示意如下:
shardingsphere-proxy opengauss 實現(xiàn)了對 batch bind 協(xié)議的支持,也就是說,客戶端能夠直接用 opengauss 的客戶端或驅動對 shardingsphere proxy 執(zhí)行批量插入。
shardingsphere-proxy 后續(xù)要做的事情
支持 shardingsphere postgresql proxy 邏輯 metadata 查詢
shardingsphere-proxy 作為透明數(shù)據(jù)庫代理,用戶無需關心 proxy 如何協(xié)調(diào)背后的數(shù)據(jù)庫。
以下圖為例,在 shardingsphere-proxy 中配置邏輯庫 sharding_db
和邏輯表 person
,proxy背后實際對應了 2 個數(shù)據(jù)庫共 4 個表。
目前在 shardingsphere mysql proxy 中分別執(zhí)行 show schemas
、show tables
語句,查詢的結果能夠正常的列出邏輯庫 sharding_db
和邏輯表 person
。
使用 psql
連接 postgresql
時可以通過 \l
、\d
等命令查詢庫、表。但與 mysql 不同的是,show tables
是 mysql 所支持的語句,而在 psql
中所使用的 \d
實際上對應了一條比較復雜的 sql,目前使用 shardingsphere postgresql proxy 暫時無法查詢出邏輯庫或邏輯表。
支持 extended query 的 describe prepared statement
postgresql 協(xié)議的 describe 消息有兩種變體,分別是 describe portal 和 describe prepared statement。目前 shardingsphere proxy 僅支持 describe portal,暫時不支持 describe prepared statement。
describe prepared statement 的實際應用舉例:在 preparedstatement 執(zhí)行之前獲取結果集的 metadata。
1
2
|
preparedstatement preparedstatement = connection .preparestatement( "select * from t_order limit ?" ); resultsetmetadata metadata = preparedstatement.getmetadata(); |
shardingsphere 與 postgresql/opengauss 生態(tài)對接的過程仍在進行,后續(xù)需要做的事情還有很多。如果您對我們所做的事情感興趣,歡迎通過 github 或郵件列表參與 shardingsphere 社區(qū)。
github: https://github.com/apache/shardingsphere
參考資料
https://www.postgresql.org/docs/current/protocol.html
到此這篇關于打造基于 postgresql/opengauss 的分布式數(shù)據(jù)庫解決方案的文章就介紹到這了,更多相關postgresql分布式數(shù)據(jù)庫內(nèi)容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/sphereex/p/15637679.html