Kong管理UI -kong-dashboard


本文仍然是在ubuntu18的環境下進行

https://github.com/PGBI/kong-dashboard

kong dashboart如果要正常使用管理UI,前提為kong已經正常run(可參考)起來,此UI可看作為一層薄薄的皮,包裝好了我們需要的請求與返回的顯示問題(懶人必備?). 

在Kong的git上可以看到豐富的三方工具,本文選用的管理UI也是上面的一個admin ui 項目

Here is a list of third-party tools maintained by the community:

 

  • 安裝

  准備工作(整個准備工作以用源碼安裝為例 form source):

  1.使用的管理UI kong-dashboard 基於nodejs 所以需要安裝nodejs(友情提示,使用node 5.11.1版本比較好,其他版本例如6.幾沒有跑起來,或者有朋友運行起來了可以告知一下)

  2.由於會使用到github資源和可能偶爾在牆外的資源,所以建議使用淘寶npm;

  3.同時會使用到bower;

  4.npm install 過程需要c++ 11(由於小Eight用的環境gcc版本低於4.8無法支持c++11 需要升級gcc版本到4.8或4.8以上;關於如何升級的園子里面文章不少如果大家需要我后續可以poll一篇我的升級過程)

  5.git clone下來的文件夾權限

  6.如果使用root用戶進行install 需要修改(如下圖)

    

  搞定准備工作之后,就可以根據git步驟上進行安裝了(以通過源碼安裝為例,具體命令有些許不同):

# Pull repository
git clone https://github.com/PGBI/kong-dashboard.git
sudo chmod -R 777 kong-dashboard (!!!!該步非必須,根據實際使用進行文件夾讀寫權限調整)
cd kong-dashboard
#npm install
sudo apt install npm

# Build Kong Dashboard
sudo npm install --unsafe-perm --registry=https://registry.npm.taobao.org
 # Start Kong Dashboard npm start # To start Kong Dashboard on a custom port npm start -- -p [port]

#或者使用下面的命令啟動kong-dashboard
# Install Kong Dashboard
npm install -g kong-dashboard

# Start Kong Dashboard
kong-dashboard start --kong-url http://kong:8001

# 使用自定義端口啟動kong-dashboard
kong-dashboard start \
  --kong-url http://localhost:8001 \
  --port 8088

啟動成功后訪問:http://你的機器或綁定的域名:8080

 

重新安裝kong-0.13.x

1、安裝數據庫postgresql9.6

CentOS7安裝配置PostgreSQL9.6

安裝環境:CentOS7.2,與此處環境相同

2. 檢查數據庫服務狀態,有綠色,沒紅色說明啟動OK了

service postgresql-9.5 status



2、安裝kong

安裝包下載:https://kong.bintray.com/kong-community-edition-rpm/centos/7/

目前最新版本kong-community-edition-0.14.0.el7.noarch.rpm

我們安裝kong-community-edition-0.13.1.el7.noarch.rpm原因是Kong UI管理工具目前最高支持的版本就是13.1

1)、直接安裝

# rpm -vih kong-community-edition-0.13.0.el7.noarch.rpm
2)、配置kong

 # vi /etc/kong/kong.conf.default
以下為kong連接postgresql數據庫的相關配置

database = postgres   # 指明用哪個數據庫

pg_host = 127.0.0.1  #數據庫所在主機ip地址
pg_port = 5432                  # 端口號
pg_user = kong                  #  數據庫用戶
pg_password =kong                   # 數據庫密碼
pg_database = kong              # 數據庫名

 


3)、啟動kong
遷移數據庫,將kong所需的表刷入數據庫
# kong migrations up -c /etc/kong/kong.conf.default
啟動kong

# kong start -c /etc/kong/kong.conf.default
測試kong是否啟動成功

#  curl -i http://localhost:8001/
 

3、安裝Kong UI管理工具

Using npm (使用 npm)

1. 安裝 nodejs:

curl -sL https://rpm.nodesource.com/setup_6.x  |  bash  -  

yum install -y nodejs

如果提示你需要安裝構建工具 gcc gcc++ 什么的你就安裝就行了,安裝完了再重新安裝 nodejs

命令:node -v 如果輸出版本號就安裝成功了。

2.  更新 npm (此時的 npm 命令已經隨 nodejs 的安裝過程給安裝了,這里可能只是為了更新一下我也不太明白)

npm install npm@latest -g

命令:npm -v 輸出版本號就成功安裝了。

開始kong-dashboard安裝了

    # Install Kong Dashboard
    npm install -g kong-dashboard
     
    # Start Kong Dashboard
    kong-dashboard start --kong-url http://kong:8001
     
    # Start Kong Dashboard on a custom port
    kong-dashboard start \
    --kong-url http://kong:8001 \
    --port [port]
     
    # Start Kong Dashboard with basic auth
    kong-dashboard start \
    --kong-url http://kong:8001 \
    --basic-auth user1=password1 user2=password2
     
    # See full list of start options
    kong-dashboard start --help

 

 

 

 通過Docker方式安裝 Using Docker (使用 Docker )

    # Start Kong Dashboard
    docker run --rm -p 8080:8080 pgbi/kong-dashboard start --kong-url http://kong:8001
     
    # Start Kong Dashboard on a custom port
    docker run --rm -p [port]:8080 pgbi/kong-dashboard start --kong-url http://kong:8001
     
    # Start Kong Dashboard with basic auth
    docker run --rm -p 8080:8080 pgbi/kong-dashboard start \
    --kong-url http://kong:8001
    --basic-auth user1=password1 user2=password2
     
    # See full list of start options
    docker run --rm -p 8080:8080 pgbi/kong-dashboard start --help

 

使用Admin UI

具體使用和UI上顯示內容的含義可參考kong的doc,下面只簡單提及一些需要注意的地方,補贅述具體使用

1.配置注意地址(kong的管理地址,默認為http://kong server機器或綁定的域名:8001)后面不要多加"/"如下圖  否則點擊API會出現not found api之類的提示,當然也要確保kong server正常運行中

    

  

2.新增API與使用新增的API時,需要注意如果需要使用地址方式指向api即 需要勾選strip-request path 如果使用head中帶請求地址的方式,需要在head中帶 X-Host-Override post.demo (即request host)

  

使用時需要注意,請求的地址為http://kong server機器或綁定的域名:8000 (下圖為在url中帶請求的api的方式)

  

3.插件使用實例,使用key-

i.需要在api基礎上新建插件auth key

ii.設置插件 keyname(需要注意此keyname會在后面url中使用)

當啟用插件后

如果后面keyname在地址欄或header中不正確會有如下提示

              

iii.新建customer並設置其key的內容也可以認為為keyname對應的值上面(該處暫時使用的對應上述auth key),用於實際訪問使用

在consumers的列表中,點擊Auth下的鏈接:

如下頁面:

列表:

iiii.使用(請求的地址為http://kong server機器或綁定的域名:8000)

header方式如下圖

后注:

  1.其他插件的使用方式大同小異(例如日志等插件大家可以自行檢查嘗試)

  2.對於使用kong來處理webservice的情況,由於我們想達到的效果是可以每次都管控到調用而非僅僅在引用的時候,所以需要對引用后自動生成的配置文件中的endpoint進行修改,實質為wdsl內容進行處理.將地址替換為kong包裝過的地址即可.

    以c#為例小Eight的處理方式,在引用端調用生成類使用時,只需要指明調用的是那個端口即使用多個endpoint對應的name

      1.引入HttpsReflector

復制代碼
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services.Description;

namespace WebService4Test
{
    public class HttpsReflector : SoapExtensionReflector
    {

        public override void ReflectMethod()
        {
            //no-op
        }

        public override void ReflectDescription()
        {
            ServiceDescription description = ReflectionContext.ServiceDescription;

            foreach (Service service in description.Services)
            {
                foreach (Port port in service.Ports)
                {
                    foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                    {
                        SoapAddressBinding binding = extension as SoapAddressBinding;

                        if (null != binding)
                        {
                            binding.Location = binding.Location.Replace("localhost:9933/Service1.asmx", "kong server機器或綁定的域名:8000/wbstest?apikey=testbaidukey"); ;//http://localhost:9933/Service1.asmx
                        }
                    }
                }
            }
        }

    }

}
復制代碼

 

      2.修改Web.Config 新增HttpsReflector system.web中加入如下code

<webServices>
      <soapExtensionReflectorTypes>
        <add type="WebService4Test.HttpsReflector, WebService4Test"/>
      </soapExtensionReflectorTypes>
</webServices>

 

解決報錯: Connecting to Kong on http ... Could not reach Kong on http://xxx.xxx.xxx.xxx:8001

1. Docker 中安裝並啟動好網關 kong 后,想要安裝 kong 的圖形化界面 kong-dashboard ,運行命令:

docker run --rm -p 8080:8080 --name kong-dashboard pgbi/kong-dashboard start --kong-url http://xxx.xxx.xxx.xxx:8001

報錯如下:

    Connecting to Kong on http://xxx.xxx.xxx.xxx:8001 ...
            Could not reach Kong on http://xxx.xxx.xxx.xxx:8001
    Error details:
    { Error: connect ETIMEDOUT xxx.xxx.xxx.xxx:8001
        at Object._errnoException (util.js:1019:11)
        at _exceptionWithHostPort (util.js:1041:20)
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1175:14)
      code: 'ETIMEDOUT',
      errno: 'ETIMEDOUT',
      syscall: 'connect',
      address: 'xxx.xxx.xxx.xxx',
      port: 8001 }

2.  原因:連接拒絕錯誤的是因為2個容器(kong 和 kong-dashboard)在默認橋接網絡上,這不會執行DNS解析。

需要創建一個用戶定義的橋接網絡並將這兩個容器添加到該網絡 。


3. 解決:

    創建橋梁網絡
    docker network create my-net
    將Kong容器添加到它
    docker network connect my-net kong
    運行kong-dashboard時提供網絡信息
    docker run --rm --network my-net -p 8080:8080 pgbi/kong-dashboard start --kong-url http://kong:8001

PS : 最后一行命令運行 kong-dashboard 時,最末我原本是寫的是“http://xxx.xxx.xxx.xxx:8001”,這樣報錯依舊,直到改為如上寫成:“http://kong:8001”才行。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM