概述
上文我們瞭解了JAVA技術棧的
閘道器Shenyu
,今天再來認識下基於nginx Kong。Kong是一個在Nginx中執行的Lua應用程式,並且可以透過lua-nginx模組實現。Kong不是用這個模組編譯Nginx,而是與OpenResty一起分發,OpenResty已經包含了lua-nginx-module。OpenResty不是Nginx的分支,而是一組擴充套件其功能的模組。
這為可插拔架構奠定了基礎,可以在執行時啟用和執行Lua指令碼(稱為“外掛”)。 因此,我們認為Kong是
微服務架構的典範
:它的核心是實現資料庫抽象,路由和外掛管理。 外掛可以存在於單獨的程式碼庫中,並且可以在幾行程式碼中注入到請求生命週期的任何位置。
依賴的框架以及版本
實現步驟
1,下載並安裝
下載地址健參考
root
# sudo yum install epel-release
root
# mkdir -p /data/work/kong
root
# cd /data/work/kong/
root
# yum install kong-2。5。0。el7。amd64。rpm
root
# kong config init
root
# touch kong。conf
root
# cat kong。conf
database
=
off
declarative_config
=
/data/work/kong/kong。yml
root
# kong start kong start -c /data/work/kong/kong。conf
2,配置service、routes以及驗證:
配置service以及routes
…………。
services
:
-
name
:
example-service
url
:
http
:
//192。168。3。115
:
18091/
routes
:
-
name
:
example-route
paths
:
-
/example
…………。。。
使用postman請求驗證,可以透過kong 閘道器訪問到後臺服務。如下圖所示:
3,啟用外掛以及驗證
啟用Key Authentication,kong。yml配置如下:
。。。
……。。。
services
:
-
name
:
url
:
http
:
:
18091/
routes
:
-
name
:
paths
:
-
/example
plugins
:
-
name
:
key-auth
consumers
:
-
username
:
example-consumer-user
keyauth_credentials
:
-
key
:
userkey
。。。
……。。。。
進行驗證
異常報錯
1,ulimit is currently set to “1024”。 For better performance set it to at least “4096” using “ulimit -n”
解決辦法:臨時調整:ulimit -n 65535
2,Error: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: connection refused
解決辦法:新增kong。conf檔案,編輯檔案新增以下內容,選擇不依賴資料庫:
database
=
off
=
3,傳送 curl -i -X POST
ocalhost:8001/services/
-d ‘name=example’
-d ‘url=
example。com’
請求 報錯:“message”:“cannot create ‘services’ entities when not using a database”,“name”:“operation unsupported”,“code”:12
解決辦法:由於沒有配置資料庫,所以不能使用api建立服務,只能透過配置檔案去設定。
4,kong pack 報錯:Error: Failed packing /data/work/kong/plugin/kong-plugin-template-master/kong-plugin-myplugin-0。1。0-1。all。rock
解決辦法:這是沒有安裝zip元件,執行命令:yum install -y zip
5,luarocks install 報錯:
Warning: Failed searching manifest: Failed extracting manifest file: ‘unzip -n’ program not found。 Make sure unzip is installed and is available in your PATH (or youmay want to edit the ‘variables。UNZIP’ value in file ‘/usr/local/etc/luarocks/config-5。1。lua’)
解決辦法:這是沒有安裝unzip元件,執行命令:yum install -y unzip
參考
下載地址
kong文件
Service和Route配置參考