浏览器就能正常访问书籍的Restful API 接口
用Kong配置一个book服务
在安装并启动Kong之后,使用Kong的管理API端口8001添加一个名称为book的服务
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=book' \
--data 'url=http://contoso.com/v1/books'
HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:46:16 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
{
"host": "contoso.com",
"created_at": 1525956376,
"connect_timeout": 60000,
"id": "6401d012-893b-437c-a115-c255ee0ef496",
"protocol": "http",
"name": "book",
"read_timeout": 60000,
"port": 80,
"path": "/v1/books",
"updated_at": 1525956376,
"retries": 5,
"write_timeout": 60000
}
添加一个路由(paths[]的值必须与book服务中的/v1/books一致)
HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:46:27 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
{
"created_at": 1525927587,
"strip_path": true,
"hosts": null,
"preserve_host": false,
"regex_priority": 0,
"updated_at": 1525927587,
"paths": [
"/v1/books"
],
"service": {
"id": "6401d012-893b-437c-a115-c255ee0ef496"
},
"methods": null,
"protocols": [
"http",
"https"
],
"id": "8f4d4267-2edc-4bd9-bc03-6419f5e93317"
}
为book服务启用跨源资源共享(CORS)插件参数配置
HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:47:29 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
{
"created_at": 1525956449000,
"config": {
"methods": [
"GET",
"POST"
],
"exposed_headers": [
"X-Auth-Token"
],
"max_age": 3600,
"headers": [
"Accept",
"Accept-Version",
"Content-Length",
"Content-MD5",
"Content-Type",
"Date",
"X-Auth-Token"
],
"credentials": true,
"origins": [
"http://contoso.com"
],
"preflight_continue": false
},
"id": "5f0c6512-c50a-4df1-8a8f-fbde047b6324",
"enabled": true,
"service_id": "6401d012-893b-437c-a115-c255ee0ef496",
"name": "cors"
}
为book服务的路由{route_id}启用跨源资源共享(CORS)插件参数配置
HTTP/1.1 201 Created
Date: Thu, 10 May 2018 12:47:43 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1
{
"created_at": 1525956463000,
"config": {
"methods": [
"GET",
"POST"
],
"exposed_headers": [
"X-Auth-Token"
],
"max_age": 3600,
"headers": [
"Accept",
"Accept-Version",
"Content-Length",
"Content-MD5",
"Content-Type",
"Date",
"X-Auth-Token"
],
"credentials": true,
"origins": [
"http://contoso.com"
],
"preflight_continue": false
},
"id": "e4118c02-9665-4e15-ad08-1c50d14088fc",
"enabled": true,
"route_id": "8f4d4267-2edc-4bd9-bc03-6419f5e93317",
"name": "cors"
}