1. 程式人生 > >hasura graphql-engine整合pgbouncer 連線池工具

hasura graphql-engine整合pgbouncer 連線池工具

pgbouncer 是一個輕量的pg 連線池工具,我們可以和hasura graphql-engine整合起來,進行連線的一些優化

環境準備

  • docker-compose 檔案
version: '3.6'
services:
  postgres:
    image: postgres:9.6
    ports:
    - "5432:5432"
    environment:
    - "POSTGRES_PASSWORD:dalong"
    volumes:
    - ./db_data2:/var/lib/postgresql/data
  pgbouncer:
    image: brainsam/pgbouncer
    environment:
    - DB_HOST=postgres
    - DB_USER=postgres
    - DB_PASSWORD=dalong
    ports:
    - "6432:6432"
  graphql-engine:
    image: hasura/graphql-engine:v1.0.0-alpha30
    ports:
    - "8080:8080"
    depends_on:
    - "postgres"
    environment:
    - "POSTGRES_PASSWORD:dalong"
    command: >
      /bin/sh -c "
      graphql-engine --database-url postgres://postgres:
[email protected]
:6432/postgres serve --enable-console; "

說明

方式一樣,主要是修改了連線的地址,因為好使用了pgbouncer,地址修改為了pgbouncer server的地址,同時
對於pgbouncer server 配置了後端的pg server, 實際上hasura graphql-engine,後端的連線自身是支援資料庫
連線池的,效能也很不錯,但是如果使用引擎進行應用開發,參考3factor 的模型,是會存在資料回寫的,使用
連線池工具可以幫助我們做好多事情,比如ha的處理,可以做到一些效能的提升

參考資料

https://github.com/pgbouncer/pgbouncer
https://hub.docker.com/r/brainsam/pgbouncer/
https://www.percona.com/blog/2018/06/27/scaling-postgresql-with-pgbouncer-you-may-need-a-connection-pooler-sooner-than-you-expect/