0 寫在前面
作為一個偽后端,免不了要用到nginx, 做一些基本的負載均衡,接口轉發已經非常穩定高效了。當了解了openresty之后,才知道通過lua就可以為nginx高效開發一些API接口服務。有了openresty就可以避免一個簡單的功能都需要搞一個龐大的SpringBoot工程。
再說到Kong,首先它是基于openresty開發,且不說(實際上是不懂)有大量的功能插件支持,光一個GUI的配置界面,就說服我去了解一下啦。
1 添加 yum 安裝源, 安裝kong服務
# 添加repo
wget https://bintray.com/kong/kong-rpm/rpm -O bintray-kong-kong-rpm.repo
export major_version=`grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d "." -f1`
sed -i -e 's/baseurl.*/&\/centos\/'$major_version''/ bintray-kong-kong-rpm.repo
mv bintray-kong-kong-rpm.repo /etc/yum.repos.d/
# 可選:更新所有可用包
yum update -y
# 查詢kong可用版本
[root@gorey ~]# yum provides kong
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: nrt.edge.kernel.org
* extras: mirrors.huaweicloud.com
* updates: mirrors.aliyun.com
kong-0.10.3-1.noarch : Kong is an open distributed platform for your APIs, focused on high performance and reliability.
Repo : bintray--kong-kong-rpm
... 中略
kong-1.2.1-1.noarch : Kong is a distributed gateway for APIs and Microservices, focused on high performance and reliability.
Repo : bintray--kong-kong-rpm
kong-1.3.0rc1-1.noarch : Kong is a distributed gateway for APIs and Microservices, focused on high performance and reliability.
Repo : bintray--kong-kong-rpm
# 這里選擇安裝 1.2.1
yum install kong-1.2.1-1.noarch
2. 安裝 postgresql10
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
yum install postgresql10 -y
yum install postgresql10-server -y
/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl enable postgresql-10
systemctl start postgresql-10
配置 postgresql, 創建kong要使用的信息
su - postgres
psql -U postgres
CREATE USER kong; CREATE DATABASE kong OWNER kong;
ALTER USER kong WITH PASSWORD 'kong';
\q # 退出
psql.png
postgresql 默認只能使用postgres 用戶,這里修改一下, 方便其他用戶都可以登錄。
vim /var/lib/pgsql/10/data/pg_hba.conf
# 找到下面的行,修改成 trust
host all all 127.0.0.1/32 trust
修改前:
before_hba.png
修改后:
after_hba.png
# 重起服務
systemctl restart postgresql-10
3. kong 配置文件
# 復制默認的 kong.conf
cd /etc/kong/
cp kong.conf.default kong.conf
# 修改kong.conf中數據庫相關設置
vim kong.conf
修改前:
before_kong.png
修改后:
after_kong.png
4. 創建kong的數據庫
kong migrations bootstrap
kong_init_db.png
5. 啟動kong服務
kong start
kong_port.png