美文网首页
Kong Quick Start

Kong Quick Start

作者: 香菜香菜我是折耳根 | 来源:发表于2020-06-06 10:20 被阅读0次

CentOS 7

安装

sudo yum install epel-release
sudo yum install wget
wget https://bintray.com/kong/kong-rpm/download_file?file_path=centos/7/kong-2.0.4.el7.amd64.rpm
sudo yum install ./kong-2.0.4.el7.amd64.rpm

配置

Kong 目前支持 PostgreSQL 和 Cassandra,也可以支持无数据库模式(DB-less mode),但是将会无法通过 Admin API 动态管理网关,只能通过配置文件手动更改然后重启网关。这里使用 PostgreSQL:

CREATE USER kong; CREATE DATABASE kong OWNER kong;

复制默认的 kong 配置文件:

cd /etc/kong/
sudo cp kong.conf.default kong.conf
sudo vi kong.conf
database = postgres             # Determines which of PostgreSQL or Cassandra
                                 # this node will use as its datastore.
                                 # Accepted values are `postgres`,
                                 # `cassandra`, and `off`.

pg_host = 10.0.2.2             # Host of the Postgres server.
#pg_port = 5432                  # Port of the Postgres server.
#pg_timeout = 5000               # Defines the timeout (in ms), for connecting,
                                 # reading and writing.

#pg_user = kong                  # Postgres user.
#pg_password =                   # Postgres user's password.
#pg_database = kong              # The database name to connect to.

然后执行数据库初始化命令:

kong migrations bootstrap -c /etc/kong/kong.conf

通过 RPM 安装后,Kong 会成为系统服务:

sudo systemctl start kong
sudo systemctl status kong

创建示例服务

mkdir example-service
cd example-service
npm init
npm install --save express
vi index.js

输入以下内容:

const app = express();
const port = 4001;

app.get('/', (req, res) => {
    return res.status(200).send({'hello': '4001'});
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
node index.js

配置 Kong 路由

curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=example-service' \
  --data 'url=http://10.0.2.2:4001'

curl -i -X POST \
  --url http://localhost:8001/services/example-service/routes \
  --data 'paths[]=/'

压测

安装 ab:

sudo yum install httpd-tools
ab -n 5000 -c 5 http://192.168.33.10:8000/

其中,-n 是请求数量,-c 是并发数量。

相关文章

网友评论

      本文标题:Kong Quick Start

      本文链接:https://www.haomeiwen.com/subject/pxeazhtx.html