目錄
配置使用
驗證
token使用
上文部署高可用PostgreSQL14集群后,本文介紹PostgREST,以及如何基于PostgREST實現數據庫即服務,PostgREST可以在?PostgreSQL 數據庫上通過解析數據庫結構(如表、視圖、存儲過程、權限等)自動生成 RESTful API,并直接轉換為標準的 HTTP 接口,相當于消除了簡單場景下的后端代碼和服務,與之前紹過的MagicAPI這種基于SpringBoot的快速api生成工具(magicApi)相比,magicAPI因為復雜度高反而意義不大了(至少在pg數據庫中,其他數據庫不一定有PostgREST這種工具)。
軟件架構逃不開合久必分分久必合的趨勢,現在硬件這么能打,玩明白數據庫高級服務就可以不用搞那些復雜的后端,等業務足夠復雜再去上微服務等分布式架構才是合理的;
配置使用
直接參照官方文檔操作PostgREST,復用上文的環境,在100.3.254.211這個主節點上下載二進制文件?PostgREST,創建配置文件,并做成服務啟動:
# json解析工具
[root@node2 postgrest]# yum install -y jq
# 配置postgrest
[root@node2 postgrest]# mkdir -p /opt/postgrest
[root@node2 postgrest]# chown postgres:postgres /opt/postgrest/ -R
[root@node2 postgrest]# cd /opt/postgrest
[root@node2 postgrest]# chmod +x postgrest
[root@node2 postgrest]# cat tutorial.conf
db-uri = "postgres://authenticator:admin123@localhost:5432/postgres"
db-schemas = "api"
db-anon-role = "web_anon"# 配置成服務
[root@node2 postgrest]# cat /etc/systemd/system/postgrest.service
[Unit]
Description=PostgREST API Server
After=network.target[Service]
Type=simple
User=postgres
Group=postgres
ExecStart=/opt/postgrest/postgrest /opt/postgrest/tutorial.conf
Restart=always
RestartSec=5
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin[Install]
WantedBy=multi-user.target# 啟動
[root@node2 postgrest]# systemctl daemon-reload
[root@node2 postgrest]# systemctl restart postgrest.service
[root@node2 postgrest]# systemctl status postgrest.service
在postgresql中創建用戶和權限:
postgres=# create schema api;
postgres=# create table api.todos (
postgres(# id int primary key generated by default as identity,
postgres(# done boolean not null default false,
postgres(# task text not null,
postgres(# due timestamptz
postgres(# );
postgres=# insert into api.todos (task) values ('finish tutorial 0'), ('pat self on back');
postgres=# create role web_anon nologin;
postgres=# grant usage on schema api to web_anon;
postgres=# grant select on api.todos to web_anon;
postgres=# create role authenticator noinherit login password 'admin123';
postgres=# grant web_anon to authenticator;
驗證
這就ok啦可以使用curl來測試API接口了,3000是postgrest提供的服務端口,web_anon用戶能查到api這個schema下的todos表,并提供查詢服務;
[root@node2 ~]# curl http://localhost:3000/todos |jq
[{"id": 1,"done": false,"task": "finish tutorial 0","due": null},{"id": 2,"done": false,"task": "pat self on back","due": null}
]
token使用
postgrest還提供基于jwt的認證機制,官方文檔寫的很清晰,在jwt.io中拿到手動生成的token,并帶入curl請求中:
export LC_CTYPE=C
echo "jwt-secret = \"$(< /dev/urandom tr -dc A-Za-z0-9 | head -c32)\"" >> tutorial.conf[root@node2 postgrest]# cat tutorial.conf
db-uri = "postgres://authenticator:admin123@localhost:5432/postgres"
db-schemas = "api"
db-anon-role = "web_anon"
jwt-secret = "chthyFjIdcLxEgKGHh780q5ly3yPebW1"# 配置文件中增加了jwt-secret重啟服務
[root@node2 postgrest]# systemctl restart postgrest

復制這個jwt-token放入環境變量并重啟postgrest服務:
[root@node2 postgrest]# export TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoidG9kb191c2VyIn0.rIRseKU6UQv7GA_sY6tBYsOt4-0nK0zCmP_y1jsIxA0"[root@node2 postgrest]# curl http://localhost:3000/todos -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"task": "learn how to auth"}'[root@node2 postgrest]# curl http://localhost:3000/todos |jq
[{"id": 1,"done": false,"task": "finish tutorial 0","due": null},{"id": 2,"done": false,"task": "pat self on back","due": null},{"id": 3,"done": false,"task": "learn how to auth","due": null}
]
以上簡單測試實現PostgreSQL的數據庫即服務的配置,部署數據庫就能提供api,還支持試圖、函數等高級使用,而且這個開源工具還比較活躍,是個很好的選項,后續高級功能待研究;