本文仍然是在centos 6.7的環境下進行
本文轉載請注明出處 —— xiaoEight
btw如果要正常使用管理UI,前提為kong已經正常run(可參考)起來,此UI可看作為一層薄薄的皮,包裝好了我們需要的請求與返回的顯示問題(懶人必備?).
附kong封裝webservice方法,小Eight處理的小方法
在Kong的git上可以看到豐富的三方工具,本文選用的管理UI也是上面的一個admin ui 項目
Here is a list of third-party tools maintained by the community:
- Ansible role for Kong on Ubuntu
- Biplane: declarative configuration in Crystal
- Bonobo: key management (with Mashery migration scripts)
- Chef cookbook
- Django Kong Admin: Admin UI in Python
- Jungle: Admin UI in JavaScript
- Kong Dashboard: Admin UI in JavaScript
- Kong for CanopyCloud
- Kong image waiting for Cassandra
- Kong image for Tutum
- Kong-UI: Admin UI in JavaScript
- Konga: CLI Admin tool in JavaScript
- Kongfig: Declarative configuration in JavaScript
- Kongfig on Puppet Forge
- Puppet recipe
- Python-Kong: Admin client library for Python
- .NET-Kong: Admin client library for .NET
- 安裝
准備工作(整個准備工作以用源碼安裝為例 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 # 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]
啟動成功后訪問:http://你的機器或綁定的域名:8080
- 使用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中不正確會有如下提示
如果后面key在地址欄或header中不正確會有如下提示
iii.新建customer並設置其key的內容也可以認為為keyname對應的值上面(該處暫時使用的對應上述auth key),用於實際訪問使用
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>