寫在前面:
持續記錄一下自己在解決api網關kong上的各種問題。
1、關於Kong網關
這是官網地址:https://konghq.com/
2、通過brew安裝postgres
因為kong的數據持久化是
brew install postgresql
initdb /usr/local/var/postgres -E utf8 pg_ctl -D /usr/local/var/postgres -l logfile start /usr/local/bin/psql -d postgres
成功后初始化kong用戶和數據庫:
CREATE USER kong;
CREATE DATABASE kong OWNER kong;
3、通過Navicat工具新建postgres連接
創建好后:
4、通過brew安裝Kong
brew tap kong/kong
brew install kong
5、安裝好Kong后設置系統環境變量
vi ~/.bash_profile
export PATH="$PATH:/usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/bin"
export PATH="$PATH:/usr/local/Cellar/kong/2.0.3/bin"
6、從官網按教程下載 kong.conf.default
並將文件移動 /etc/kong/kong.conf 並改名
注意:這個kong.conf配置文件的信息需要修改,其中包括配置postgres數據庫的地址賬號密碼等信息。
其中我的配置如下:
database = postgres # Determines which of PostgreSQL or Cassandra
pg_host = 127.0.0.1 # Host of the Postgres server.
pg_port = 5432 # Port of the Postgres server.
pg_timeout = 5000 # Defines the timeout (in ms), for connecting,
pg_user = kong # Postgres user.
pg_password = kong # Postgres user's password.
pg_database = kong # The database name to connect to.
pg_ssl = off # Toggles client-server TLS connections
pg_ssl_verify = off # Toggles server certificate verification if
執行命令:
sudo mkdir -p /etc/kong
sudo cp kong.conf.default /etc/kong/kong.conf
7、運行Kong migrations 進行Database初始化操作
kong migrations up -c /etc/kong/kong.conf
8、運行啟動Kong網關
kong start -c /etc/kong/kong.conf
9、驗證Kong網關啟動成功
訪問127.0.0.1:8001,出現一大串Json數據,說明Kong網關已經啟動成功。如下圖:
至此,本地搭建Kong網關的步驟已經全部完成。