kong插件之httplog

轉載: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
}

官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章