开鲁县 民勤县 含山县 越西县 嵊泗县 温宿县 水富县 广丰县 揭阳市 九龙县 当阳市 乌兰察布市 太仓市 张家口市 绵阳市 大埔县
2018年最新新闻网

您当前的位置:主页 > 2018年最新新闻网国内 >

作者:帝王道 来源:原创 发布日期:06-01

南开大学_apigateway-kong(二)admin-api(结合实例比官网还详细)

标签:农发行 支持花呗的博彩娱乐平台

部署好kong之后,则需要将我们自己的接口加入到kong中管理,kong提供了比较全面的restful api,每个版本会有所不同,下面的记录基于kong v0.13.x

kong的8001端口是resful admin api,服务、路由、配置都是通过这个端口进行管理,所以部署好之后页面可以直接访问localhost:8001

下面针对每个模块的API进行简介,每个对象模块对应数据库中的一张存储表。

Information Routes

获取kong节点的通用详细信息

管理API的请求日志路径

/usr/local/opt/kong/logs
.
├── access.log
├── admin_access.log #admin-api请求日志
└── error.log

查询节点信息

Example

curl  http://localhost:8001

Endpoint

GET /

Response

{
    "plugins": {
        "enabled_in_cluster": [],
        "available_on_server": {
            "response-transformer": true,
            "correlation-id": true,
            "statsd": true,
            "jwt": true,
            "cors": true,
            "basic-auth": true,
            "key-auth": true,
            "ldap-auth": true,
            "http-log": true,
            "oauth2": true,
            "hmac-auth": true,
            "acl": true,
            "datadog": true,
            "tcp-log": true,
            "ip-restriction": true,
            "request-transformer": true,
            "file-log": true,
            "bot-detection": true,
            "loggly": true,
            "request-size-limiting": true,
            "syslog": true,
            "udp-log": true,
            "response-ratelimiting": true,
            "aws-lambda": true,
            "runscope": true,
            "rate-limiting": true,
            "request-termination": true
        }
    },
    "tagline": "Welcome to kong",
    "configuration": {
        "error_default_type": "text/plain",
        "admin_listen": [
            "0.0.0.0:8001"
        ],
        "proxy_acc全职妈妈_2018年最新新闻网ess_log": "logs/access.log",
        "trusted_ips": {},
        "prefix": "/usr/local/opt/kong",
        "nginx_conf": "/usr/local/opt/kong/nginx.conf",
        "cassandra_username": "kong",
        "admin_ssl_cert_csr_default": "/usr/local/opt/kong/ssl/admin-kong-default.csr",
        "dns_resolver": {},
        "pg_user": "kong",
        "mem_cache_size": "128m",
        "server_tokens": true,
        "custom_plugins": {},
        "pg_host": "127.0.0.1",
        "nginx_acc_logs": "/usr/local/opt/kong/logs/access.log",
        "proxy_listen": [
            "0.0.0.0:8000"
        ],
        "client_ssl_cert_default": "/usr/local/opt/kong/ssl/kong-default.crt",
        "ssl_cert_key_default": "/usr/local/opt/kong/ssl/kong-default.key",
        "db_update_frequency": 5,
        "db_update_propagation": 0,
        "nginx_err_logs": "/usr/local/opt/kong/logs/error.log",
        "cassandra_port": 9042,
        "dns_order": [
            "LAST",
            "SRV",
            "A",
            "CNAME"
        ],
        "dns_error_ttl": 1,
        "cassandra_lb_policy": "RoundRobin",
        "nginx_optimizations": true,
        "database": "postgres",
        "pg_database": "kong",
        "nginx_worker_processes": "auto",
        "lua_package_cpath": "",
        "lua_package_path": "./?.lua;./?/init.lua;",
        "nginx_pid": "/usr/local/opt/kong/pids/nginx.pid",
        "upstream_keepalive": 60,
        "admin_access_log": "logs/admin_access.log",
        "client_ssl_cert_csr_default": "/usr/local/opt/kong/ssl/kong-default.csr",
        "proxy_listeners": [{
            "ssl": false,
            "ip": "0.0.0.0",
            "proxy_protocol": false,
            "port": 8000,
            "http2": false,
            "listener": "0.0.0.0:8000"
        }],
        "proxy_ssl_enabled": false,
        "lua_socket_pool_size": 30,
        "plugins": {
            "response-transformer": true,
            "correlation-id": true,
            "statsd": true,
            "jwt": true,
            "cors": true,
            "basic-auth": true,
            "key-auth": true,
            "ldap-auth": t绅士的品格_2018年最新新闻网rue,
            "http-log": true,
            "request-termination": true,
            "hmac-auth": true,
            "rate-limiting": true,
            "datadog": true,
            "tcp-log": true,
            "runscope": true,
            "aws-lambda": true,
            "response-ratelimiting": true,
            "acl": true,
            "loggly": true,
            "syslog": true,
            "request-size-limiting": true,
            "udp-log": true,
            "file-log": true,
            "request-transformer": true,
            "bot-detection": true,
            "ip-restriction": true,
            "oauth2": true
        },
        "lua_ssl_verify_depth": 1,
        "cassandra_consistency": "ONE",
        "client_max_body_size": "0",
        "admin_error_log": "logs/error.log",
        "admin_ssl_cert_default": "/usr/local/opt/kong/ssl/admin-kong-default.crt",
        "dns_not_found_ttl": 30,
        "pg_ssl": false,
        "admin_ssl_enabled": false,
        "cassandra_ssl": false,
        "cassandra_repl_strategy": "SimpleStrategy",
        "latency_tokens": true,
        "dns_stale_ttl": 4,
        "cassandra_repl_factor": 1,
        "cassandra_data_centers": [
            "dc1:2",
            "dc2:3"
        ],
        "kong_env": "/usr/local/opt/kong/.kong_env",
        "cassandra_schema_consensus_timeout": 10000,
        "dns_hostsfile": "/etc/hosts",
        "log_level": "notice",
        "admin_ssl_cert_key_default": "/usr/local/opt/kong/ssl/admin-kong-default.key",
        "real_ip_header": "X-Real-IP",
        "db_cache_ttl": 3600,
        "cassandra_timeout": 5000,
        "cassandra_ssl_verify": false,
        "dns_no_sync": false,
        "cassandra_contact_points": [
            "127.0.0.1"
        ],
        "real_ip_recursive": "off",
        "proxy_error_log": "logs/error.log",
        "client_ssl_cert_key_default": "/usr/local/opt/kong/ssl/kong-default.key",
        "nginx_daemon": "on",
        "anonymous_repor一闪一闪亮晶晶_2018年最新新闻网ts": true,
        "ssl_cipher_suite": "modern",
        "nginx_kong_conf": "/usr/local/opt/kong/nginx-kong.conf",
        "pg_port": 5432,
        "pg_ssl_verify": false,
        "client_body_buffer_size": "8k",
        "nginx_admin_acc_logs": "/usr/local/opt/kong/logs/admin_access.log",
        "ssl_cert_csr_default": "/usr/local/opt/kong/ssl/kong-default.csr",
        "admin_listeners": [{
            "ssl": false,
            "ip": "0.0.0.0",
            "proxy_protocol": false,
            "port": 8001,
            "http2": false,
            "listener": "0.0.0.0:8001"
        }],
        "cassandra_keyspace": "kong",
        "ssl_cert_default": "/usr/local/opt/kong/ssl/kong-default.crt",
        "client_ssl": false,
        "ssl_ciphers": "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256"
    },
    "version": "0.13.1",
    "node_id": "e9ca18fb-a82c-4730-83a1-621ceadc3c38",
    "lua_version": "LuaJIT 2.1.0-beta3",
    "prng_seeds": {
        "pid: 752": 149231181689,
        "pid: 751": 159113818774,
        "pid: 750": 702104292191,
        "pid: 753": 191164118531
    },
    "timers": {
        "pending": 5,
        "running": 0
    },
    "hostname": "jeandeMacBook-Pro.local"
}
View Code

部分返回字段含义:

node_id : 正在运行的kong节点的uuid,当kong启动时随机生成,每次kong重启时这个uuid都会变

availabel_on_server : kong节点上安装的plugins的名称

enabled_in_cluster : kong节点中启用的插件,即在数据库中生成了对应存储表

查询节点状态

Example

curl  http://localhost:8001/status

Endpoint

GET /status

Response

{
    "database": {
        "reachable": true
    },
    "server": {
        "connections_writing": 1,
        "total_requests": 29,
        "connections_handled": 32,
        "connections_accepted": 32,
        "connections_reading": 0,
        "connections_active": 3,
        "connections_waiting": 2
    }
}

total_requests : 客户端请求总数

connections_active : 包括等待连接的活动客户端连接的当前数量

connections_accepted :接受的客户端连接的总数

connections_handled :处理连接的总数。一般来说,除非达到一定的资源限制,否则参数值与接受值相同

connections_reading :当前Kong正在读取请求头的连接数

connections_writing : NGINX将响应写入客户端的连接的当前数量

connections_wa全运会闭幕式_2018年最新新闻网iting :等待请求的空闲客户端连接的当前数量

reachable :反映数据库连接状态的布尔值。注意,此标志不反映数据库本身的健康状况。

API Object

kong v0.13.x之前的版本是通过这个接口来管理用户接入的API,但是v0.13.x版本之后,官方不建议使用API来管理用户接口,而是用Service和Route模块来替代,管理的更精细。

KONG API模块管理的是接入kong的上游API,每个接入的api必须至少指定hosts/uris/methods其中一个参数,kong将会代理所有指定upstream url的请求。

新增一个接入的API

Example

curl -i -X POST 
  --url  http://localhost:8001/apis/ 
  --data "name=weather-api" 
  --data "hosts=www.sojson.com" 
  --data "uris=/weather" 
  --data "upstream_url=https://www.sojson.com/open/api/weather/json.shtml"

Endpoint

POST  /apis/

Request Body

属性 约束 描述
name required 接入的API名称
hosts semi-optional 逗号分割的接入API的域名列表
uris semi-optional 逗号分割的接入API的前缀path,即指定uri用户通过kong刚问要加上这个path
methods semi-optional 逗号分割的接入API的HTTP method,如get /post/ put/delete/..
upstream_url required 代理的上游API Server
strip_uri optional,default:true 当匹配到uris前缀时,去掉请求的upstream_url中匹配的uris;即uris是挂载在kong的路径下,不是上游接口的path
preserve_host optional,default:false Kong默认将上游请求的Host头设置为从API的upstream_url中提取的主机名,当通过hosts来匹配API时,确保hosts能转发到上游服务
retries optional,default:5 代理失败时重试的次数
upstream_connect_timeout optional,default:60000ms 建立与上游连接的超时时间(ms)
upstream_send_timeout optional,default:60000ms 在发送请求到上游服务的两个连续写入操作之间的超时时间(ms)
upstream_read_timeout optional,default:60000ms 在发送请求到上游服务的两个连续读取操作之间的超时时间(ms)
https_only optional,default:false 如果希望仅通过HTTPS转发API(默认8443端口),则启用
http_if_terminated optional,default:false 仅在https限流时才考虑设置X-Forwarded-Proto头部

 

Response

HTTP/1.1 201 Created
Date: Sat, 26 May 2018 07:54:34 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": 1527350074050,
    "strip_uri": true,
    "id": "c347a69e-d81e-402b-87b0-c8cf25a771e5",
    "hosts": ["www.sojson.com"],
    "name": "weather-api",
    "uris": ["/weather"],
    "http_if_terminated": false,
    "preserve_host": false,
    "upstream_url": "https://www.sojson.com/open/api/weather/json.shtml",
    "upstream_connect_timeout": 60000,
    "upstream_send_timeout": 60000,
    "upstream_read_timeout": 60000,
    "retries":gucci_2018年最新新闻网 5,
    "https_only": false
}

  上面接口到意思是:这个API注册的名字叫weather-api。它被挂载在网关的/weather路径下,上游转发到http://localhost:8000去处理,转发的时候把前面的/weather前缀给去掉。

注意uris必须加slash /,默认是/ strip_uri到作用也很明显,就是在代理下面划分虚拟路径便于管理

  新增好API后则可以通过kong代理来访问代理的服务

# 原接口
curl https://www.sojson.com/open/api/weather/json.shtml?city=上海

#通过kong代理访问
curl -i -X GET 
   --header "host:www.sojson.com" 
   --url  http://localhost:8000/weather?city=上海

根据name或id获取一个API

Example

  curl -i -X GET 
  --url  http://localhost:8001/apis/weather-api 
或
  curl -i -X GET 
  --url  http://localhost:8001/apis/c347a69e-d81e-402b-87b0-c8cf25a771e5

Endpoint

GET /apis/{name or id}

Request Params

属性 约束 描述
name or id required 接入API到唯一标识符,name或者id

Response

HTTP/1.1 200 OK
Date: Sat, 26 May 2018 08:18: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": 1527350074050,
    "strip_uri": true,
    "id": "c347a69e-d81e-402b-87b0-c8cf25a771e5",
    "hosts": ["www.sojson.com"],
    "name": "weather-api",
    "uris": ["/weather"],
    "http_if_terminated": false,
    "preserve_host": false,
    "upstream_url": "https://www.sojson.com/open/api/weather/json.shtml",
    "upstream_connect_timeout": 60000,
    "upstream_send_timeout": 60000,
    "upstream_read_timeout": 60000,
    "retries": 5,
    "https_only": false
}

查询所有接入到API列表

Example

curl -i -X GET   --url  http://localhost:8001/apis/

Endpoint

GET /apis/

Request Querystring

属性 约束 描述
id optional 同post描述
name optional
upstream_url optional
retries optional
size optional limit,查询的记录条数
offset optional cursor位置,用于分页

Response

HTTP/1.1 200 OK
Date: Sat, 26 May 2018 08:25:33 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "total": 1,
    "data": [{
        "created_at": 1527350717171,
        "strip_uri": true,
        "id": "b93fcbe7-5dba-4888-bf8c-f4c8f798b53a",
        "hosts": ["www.sojson.com"],
        "name": "weather-api",
        "http_if_terminated": false,
        "https_only": false,
        "retries": 5,
        "uris": ["/weather"],
        "preserve_host": false,
        "upstream_connect_timeout": 60000,
        "upstream_readiphone_2018年最新新闻网_timeout": 60000,
        "upstream_send_timeout": 60000,
        "upstream_url": "https://www.sojson.com/open/api/weather/json.shtml澳门银河2949所有网址  919银河优越会  银河娱乐在线官方网址  银河优越会会员申请  银河国际手机网址2949  银河娱乐在线官方网址  银河娱乐是正规网站吗  银河娱乐在线官方网址  澳门银河送彩金  银河娱乐是正规网站吗   

Copyright @ 2016-2018 2018年最新新闻网 版权所有