API網關Kong部署和使用文檔


KONG安裝使用說明
系統版本:ubuntu14

1、下載安裝包

$ wget https://github.com/Mashape/kong/releases/download/0.8.3/kong-0.8.3.trusty_all.deb
2、安裝依賴

$ sudo apt-get update

$ sudo apt-get install openjdk-7-jdk

$ sudo apt-get install netcat openssl libpcre3 dnsmasq procps

$ sudo dpkg -i kong-0.8.3.*.deb
3、安裝數據庫

安裝CASSANDRA 2.2.X

下載地址:

$ wget http://mirrors.hust.edu.cn/apache/cassandra/2.1.15/apache-cassandra-2.1.15-bin.tar.gz
解壓:

$ tar zxvf apache-cassandra-2.1.15-bin.tar.gz
移動:

$ sudo mv apache-cassandra-2.1.15-bin /usr/local/cassandra-2.1.15
執行:

$ /usr/local/cassandra-2.1.15/bin/cassandra
4、修改配置

$  sudo vi /etc/kong/kong.yml

設置 cluster_listen: "127.0.0.1:7946"
5、啟動kong

$ kong start
5、運行kong

$ curl 127.0.0.1:8001
6、添加API

$ curl -i -X POST \
  --url http://localhost:8001/apis/ \
  --data 'name=simlogin' \
  --data 'upstream_url=http://uc.ttq.dev/index.php/ttk/auth/simlogin' \
  --data 'request_path=/simlogin' \  
  --data 'request_host=uc.ttq.dev'

url:kong固定地址
request_path:請求路徑
upstream_url:真實路徑
7、訪問API

http://172.168.6.211:8000/simlogins?login_account=18022222223&channel=ttk&type=pc

 172.168.6.211:8000(Kong服務器地址)
 simlogins (request_path)
8、Key驗證

配置key

$ curl -X POST http://kong:8001/apis/{api}/plugins \
 --data "name=key-auth"

例子:
$ curl -X POST http://localhost:8001/apis/simlogin/plugins --data 'name=key-auth' 

返回值:
{
    "api_id": "95ec4e23-0d19-4c38-a974-8f5643876eb0",
    "id": "5e711b42-23f8-4f79-a7b1-91b0bd779c69",
    "created_at": 1472110325000,
    "enabled": true,
    "name": "key-auth",
    "config": {
        "key_names": ["apikey"],
        "hide_credentials": false
    }
}
創建調用賬戶

$ curl -X POST http://kong:8001/consumers/ \
 --data "username=<USERNAME>" \
 --data "custom_id=<CUSTOM_ID>"

例子:
$ curl -X POST http://localhost:8001/consumers/ \
 --data "username=login_user" 
 --data "custom_id=000002"

返回值:
{
    "custom_id": "000002",
    "username": "login_user",
    "created_at": 1472110450000,
    "id": "9267ff99-3732-4150-a727-ced409699e27"
}
設置key

$ curl -X POST http://kong:8001/consumers/{consumer}/key-auth

例子:
$ curl -X POST http://localhost:8001/consumers/9267ff99-3732-4150-a727-ced409699e27/key-auth \
 --data "key=ksdpemc514d"

返回值:
{
    "key": "ksdpemc514d",
    "consumer_id": "9267ff99-3732-4150-a727-ced409699e27",
    "created_at": 1472110618000,
    "id": "005a8a0b-61ce-4085-8300-4c9790c9599c"
} 
http調用

http://kong:8000/simlogins?apikey=ksdpemc514d 
//參數和實際接口參數一樣
8、IP限定

$ curl -X POST http://kong:8001/apis/{api}/plugins \
 --data "name=ip-restriction" \
 --data "config.whitelist=54.13.21.1, 143.1.0.0/24"

例子:
$ curl -X POST http://localhost:8001/apis/login/plugins \
 --data "name=ip-restriction" \
 --data "config.whitelist=172.168.6.210"

返回值:
{
    "api_id": "f7eb3785-2e6f-477c-bac6-4b078a7a7cf2",
    "id": "ec1666af-66f5-45f4-8059-619b93d1299a",
    "created_at": 1472191585000,
    "enabled": true,
    "name": "ip-restriction",
    "config": {
        "whitelist": ["172.168.6.210"]
    }
}
9、刪除擴展

$ curl -i -X DELETE kong:8001/plugins/{pluginsid}

例子:
$ curl -i -X DELETE localhost:8001/plugins/f1ef7f24-a5b3-40c7-8c23-75860bdf9fb3

返回值:
HTTP/1.1 204 No Content
Date: Fri, 26 Aug 2016 06:04:44 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.8.3
10、安裝ui

注:比較麻煩,而且也不穩定
安裝mongoDB

$ sudo apt-get install mongodb mongodb-clients mongodb-dev mongodb-server
安裝nodejs

$ sudo weget https://nodejs.org/dist/v4.5.0/node-v4.5.0.tar.gz //nodejs

$ sudo tar zxvf node-v4.5.0.tar.gz

$ sudo ./configure

$ sudo make && make install 
安裝npm

$ sudo weget https://npm.taobao.org/mirrors/npm/v3.9.6.tar.gz //npm

$ sudo tar zxvf v3.9.6.tar.gz

$ sudo ./configure

$ sudo make && make install 
安裝bower

1、$ sudo npm i -g bower

2、$ git clone https://github.com/lestoni/dashy.git

3、$ sudo bower install

4、修改bower.json "validator": "https://github.com/yairEO/validator.git#^1.0.6

5、配置config/index.js

6、$ npm start

7、http://localhost:7500


免責聲明!

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



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