kong 实战

        

Kong是在客户端和(微)服务间转发API通信的API网关,通过插件扩展功能。Kong有两个主要组件:

  • Kong Server :基于nginx的服务器,用来接收API请求。
  • Apache Cassandra :用来存储操作数据。

你可以通过增加更多Kong Server机器对Kong服务进行水平扩展,通过前置的负载均衡器向这些机器分发请求。根据文档描述,两个Cassandra节点就足以支撑绝大多数情况,但如果网络非常拥挤,可以考虑适当增加更多节点。

对于开源社区来说,Kong中最诱人的一个特性是可以通过插件扩展已有功能,这些插件在API请求响应循环的生命周期中被执行。插件使用Lua编写,而且Kong还有如下几个基础功能:HTTP基本认证、密钥认证、、CORS( Cross-origin Resource Sharing,跨域资源共享)。

全部的功能是要收费的! 一张图说明使用Kong-API网关前后的效果:


Paste_Image.png

brew安装kong


brew tap mashape/kong
brew install kong

5分钟体验


基本操作

启动kong

kong start  -c <path_to_config>

检查kong是不是正常启动了

正常启动会输出:[OK] Started,kong会监听两个端口:

  • 8000: API请求的代理层。
  • 8001: restful的配置管理API。

停止kong

kong stop

重新加载kong

kong reload

添加API

通过RESTful API添加你的API

curl -i -X POST \
  --url http://localhost:8001/apis/ \
  --data 'name=mockbin' \
  --data 'upstream_url=http://mockbin.com/' \
  --data 'request_host=mockbin.com'

检查API是不是已经被加如了:

HTTP/1.1 201 Created
Content-Type: application/json
Connection: keep-alive

{
  "request_host": "mockbin.com",
  "upstream_url": "http://mockbin.com/",
  "id": "2eec1cb2-7093-411a-c14e-42e67142d2c4",
  "created_at": 1428456369000,
  "name": "mockbin"
}

发起一个请求

curl -i -X GET \
  --url http://localhost:8000/ \
  --header 'Host: mockbin.com'

启动插件


通过API配置插件

下面增加一个key-auth插件

curl -i -X POST \
  --url http://localhost:8001/apis/mockbin/plugins/ \
  --data 'name=key-auth'

检查API是否已经被配置了

curl -i -X GET \
  --url http://localhost:8000/ \
  --header 'Host: mockbin.com'

# 返回错误,应该key-auth要求提供apikey头和其他参数

HTTP/1.1 403 Forbidden
...

{
  "message": "Your authentication credentials are invalid"
}

增加用户


用户可以被单独管理,以及控制他们对API的访问。

新建一个用户

$ curl -i -X POST \
  --url http://localhost:8001/consumers/ \
  --data "username=Jason"  \
  --data "custom_id=13"

HTTP/1.1 201 Created
Content-Type: application/json
Connection: keep-alive

{
  "username": "Jason",
  "created_at": 1428555626000,
  "id": "bbdf1c48-19dc-4ab7-cae0-ff4f59d87dc9"
}

为用户创建秘钥

$ curl -i -X POST \
  --url http://localhost:8001/consumers/Jason/key-auth/ \
  --data 'key=ENTER_KEY_HERE'

{
    "consumer_id": "876bf719-8f18-4ce5-cc9f-5b5af6c36007",
    "created_at": 1443371053000,
    "id": "62a7d3b7-b995-49f9-c9c8-bac4d781fb59",
    "key": "62eb165c070a41d5c1b58d9d3d725ca1"
}

如果不指定key参数,kong会为你生成一个,推荐不指定。

检查用户凭证是否有效

curl -i -X GET \
  --url http://localhost:8000 \
  --header "Host: mockbin.com" \
  --header "apikey: ENTER_KEY_HERE"

使用API key

# 参数
curl http://kong:8000/{api path}?apikey=<some_key>

# headers
curl http://kong:8000/{api path} -H 'apikey: <some_key>'

上游HTTP头。

当一个客户端被验证通过后,这个插件会附加一些参数到请求中,可以用来完成你自己的逻辑。

  • X-Consumer-ID, the ID of the Consumer on Kong
  • X-Consumer-Custom-ID, the custom_id of the Consumer (if set)
  • X-Consumer-Username, the username of the Consumer (if set)

kong如何和consul搭配使用

github-Example with consul.io

很多项目用consul做服务发现,consul支持dns做服务发现,所以直接将kong的dns服务器地址配置成consul就可以了。

dns_resolver: server
dns_resolvers_available:
  server:
    address: "192.168.9.31:8600"

问题:kong是否可以配置2-3个dns服务器,然后自己负载均衡。

目前不能,kong只能接受1个dns地址,需要用户自己在consul前面搭建一个load balancer,然后在kong中配置这个lb的地址。

问题:kong不支持consul

kong使用普通dns查询,不支持SRV查询,但是Consul的服务发现只支持SRV查询。
kong会在0.9.0加入支持.

猜你喜欢

转载自hugoren.iteye.com/blog/2383381