`

KONG API Gateway入门教程

 
阅读更多

环境信息:

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

 

2. Kong Dashboard

Kong-Dashboard 兼容版本信息

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://<kong dashboard ip>:8080/ 显示如下图。

 
 参考文档:

https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

https://github.com/PGBI/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 地址。



 

添加成功后,在界面显示如下

4. 测试

运行:

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

 返回:

[POST] VM Manage api

 
 
 

  • 大小: 12 KB
  • 大小: 86.8 KB
  • 大小: 6.5 KB
  • 大小: 32.3 KB
  • 大小: 40.6 KB
  • 大小: 22.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics