---
title: "KONG API Gateway入门教程"
author: "codyJava"
date: "2018-05-08T14:15:12Z"
tags: []
categories: ["开源软件"]
slug: "kong-api-gateway"
draft: false
lastmod: "2018-05-08T14:18:27Z"
id: 2422245
url: "https://codyjava.iteye.com/blog/2422245"
---

**环境信息：**

OS： CentOS 7 x86\_64

防火墙： 关闭 systemctl stop firewalld

 

一、安装

1\. Kong

当前Kong Dashboard兼容的Kong版本是**0.12.x** 不能通过 yum install 默认的方式去安装 kong，这样会默认安装最新版本的 kong，导致兼容问题。

1)  配置Kong repo信息

```
cat /etc/yum.repos.d/kong.repo

[kong]
name=Kong Community Edition
baseurl=https://kong.bintray.com/kong-community-edition-rpm/centos/7
failovermethod=priority
enabled=1
gpgcheck=0
repo_gpgcheck=0

yum install -y kong-community-edition-0.12.3-1.noarch
```

 2)  安装 PostgreSQL 10

```
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
yum install postgresql10
yum install postgresql10-server
/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl enable postgresql-10
systemctl start postgresql-10
```

 3）  配置PostgreSQL数据库，并创建Kong相关的数据库信息。

```
/var/lib/pgsql/10/data/postgresql.conf
listen_addresses = '*'

/var/lib/pgsql/10/data/pg_hba.conf
host    all             all             127.0.0.1/32            trust

systemctl restart postgresql-10
su - postgres
-bash-4.1$ psql
postgres=# CREATE USER kong; CREATE DATABASE kong OWNER kong;
CREATE ROLE
CREATE DATABASE
postgres=# quit
postgres-# \q
-bash-4.1$ exit
```

 4)  启动 Kong

```
kong migrations up
kong start
```

 5) 测试  
curl -i http://localhost:8001/

输出如下信息并没有错误生成。

```
HTTP/1.1 200 OK
Date: Tue, 08 May 2018 05:56:11 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.12.3

{"plugins":{"...
```

 **参考文档：**

[https://getkong.org/install/centos/?\_ga=2.22984958.475071696.1525657125-1876323536.1524451300](https://getkong.org/install/centos/?_ga=2.22984958.475071696.1525657125-1876323536.1524451300 "Kong Installation GUID")

 

2\. Kong Dashboard

Kong-Dashboard 兼容版本信息  
![](http://dl2.iteye.com/upload/attachment/0129/7694/d101672b-ecb9-38be-90f1-20e7023788bb.png)

1)  安装 npm & Nodejs

```
curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
yum install -y nodejs
```

 2)  安装Kong Dashboard

```
npm install -g kong-dashboard
```

 3)  启动 Kong Dashboard

```
kong-dashboard start --kong-url http://<kong server>:8001
```

 4)  测试

默认端口8080  
![](http://dl2.iteye.com/upload/attachment/0129/7700/25e1ff12-aa2e-343c-bd98-bd40c242badd.png)  
浏览器中直接访问 http://&lt;kong dashboard ip&gt;:8080/ 显示如下图。  
![](http://dl2.iteye.com/upload/attachment/0129/7698/89610c28-8a88-3496-9cbf-1b79399700e2.png)  
   
 **参考文档：**

[https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora](https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora "NodeJS Installation Guide")

[https://github.com/PGBI/kong-dashboard](https://github.com/PGBI/kong-dashboard "Kong Dashboard")

 

二、添加API

 **1. 编写 RESTful API 程序**

```
import web
import yaml
from tendo import colorer, singleton

urls = (
    '/manager/(.*)', 'manage',
    '/flavor/(.*)', 'flavor',
)

app = web.application(urls, globals())


class flavor:
    def GET(self, name):
        if 'list' == name:
            return "[GET] Flavor list api"
        else:
            return "[GET] Flavor other api"

class manage:
    def GET(self, name):
        return "[GET] VM Manage api"
    
    def POST(self, name):
        return "[POST] VM Manage api"
    
    def PUT(self, name):
        return "[PUT] VM Manage api"
    
    def DELETE(self, name):
        return "[DELETE] VM Manage api"

if __name__ == "__main__":
    me = singleton.SingleInstance()
    app.run()
```

 **2.  测试程序可以正常访问**

 将上述代码保存在 demo.py 中，运行

```
python demo.py 1234
```

 运行：

```
curl -X GET http://192.168.44.147:1234/manager/1
```

 返回：

```
[GET] VM Manage api
```

 **3. 通过Kong Dashboard 界面添加该 API**

配置如图所示，192.168.44.147是demo web的 ip 地址。

![](http://dl2.iteye.com/upload/attachment/0129/7702/ffaf973d-af77-327c-88c8-7dda5a8ae54e.png)  
   
![](http://dl2.iteye.com/upload/attachment/0129/7704/7deebef8-5589-3064-a81b-942485e8cd6c.png)

添加成功后，在界面显示如下  
![](http://dl2.iteye.com/upload/attachment/0129/7706/bbcd8a41-f705-35aa-bccb-632e82aabfde.png)

**4. 测试**

运行：

```
curl -X POST http://<kong ip address>:8000/manager/manager/1
```

 返回：

```
[POST] VM Manage api
```