kong插件之httplog

来源:互联网 发布:mac high sierra降级 编辑:程序博客网 时间:2024/06/03 17:19

转载:https://blog.lqsos.com/archives/33.html

创建API

curl -i -X POST \    --url http://localhost:8001/apis/ \    --data 'name=example-api' \    --data 'uris=/user' \    --data 'upstream_url=https://www.baidu.com'

返回值

{    "created_at":1513844533000,    "strip_uri":true,    "id":"16f447f2-846d-473f-929a-90aaab945142",    "name":"example-api",    "http_if_terminated":false,    "preserve_host":false,    "upstream_url":"https://www.baidu.com",    "uris":[        "/user"    ],    "upstream_connect_timeout":60000,    "upstream_send_timeout":60000,    "upstream_read_timeout":60000,    "retries":5,    "https_only":false}

参数说明

consumer_id: 可选参数,消费者id(启用了消费者认证可以使用,根据id识别发出请求的消费者);config.http_endpoint: 日志接收服务器(包括使用的协议,http or https);config.method: 可选参数,默认POST,访问日志服务器的请求方式(可选值:PUT,PATCH,POST);config.timeout: 可选参数,默认10000毫秒,请求超时时间config.keepalive: 可选参数,默认60000毫秒,连接在关闭之前可存活时间

测试

curl -X POST http://localhost:8001/apis/example-api/plugins \    --data "name=http-log" \    --data "config.http_endpoint=http://test.my" \    --data "config.method=POST" \    --data "config.timeout=1000" \    --data "config.keepalive=1000"

返回值

{    "created_at":1513844551000,    "config":{        "http_endpoint":"http://test.my",        "timeout":1000,        "method":"POST",        "keepalive":1000,        "content_type":"application/json"    },    "id":"4fe57a5b-68d3-4f88-9824-d6071f2a68dd",    "name":"http-log",    "api_id":"16f447f2-846d-473f-929a-90aaab945142",    "enabled":true}

test.my代码

<?php$myfile = fopen("testfile.txt", "w");$txt = file_get_contents("php://input");fwrite($myfile, $txt);fclose($myfile);

testfile.txt文件内容(格式化后的)

{    "api":{        "created_at":1513844533000,        "strip_uri":true,        "id":"16f447f2-846d-473f-929a-90aaab945142",        "name":"example-api",        "http_if_terminated":false,        "https_only":false,        "upstream_url":"https://www.baidu.com",        "uris":[            "/user"        ],        "preserve_host":false,        "upstream_connect_timeout":60000,        "upstream_read_timeout":60000,        "upstream_send_timeout":60000,        "retries":5    },    "request":{        "querystring":{        },        "size":"82",        "uri":"/user",        "request_uri":"https://localhost:8443/user",        "method":"GET",        "headers":{            "host":"localhost:8443",            "accept":"*/*",            "user-agent":"curl/7.52.1"        }    },    "client_ip":"127.0.0.1",    "latencies":{        "request":9,        "kong":3,        "proxy":6    },    "response":{        "headers":{            "content-type":"text/html; charset=UTF-8",            "date":"Thu, 21 Dec 2017 08:41:21 GMT",            "connection":"close",            "via":"kong/0.11.0",            "x-kong-proxy-latency":"3",            "x-kong-upstream-latency":"6",            "cache-control":"private, no-cache, no-store, proxy-revalidate, no-transform",            "content-length":"2443",            "pragma":"no-cache",            "last-modified":"Mon, 23 Jan 2017 13:24:18 GMT",            "set-cookie":"BDORZ=27315; max-age=86400; domain=.baidu.com; path=/",            "server":"bfe/1.0.8.18",            "accept-ranges":"bytes",            "etag":""58860402-98b""        },        "status":200,        "size":"2929"    },    "tries":[        {            "balancer_latency":0,            "port":443,            "ip":"14.215.177.38"        }    ],    "started_at":1513845681183}

官方文档

原创粉丝点击