kong插件之httplog

本文介绍如何使用curl命令创建API,并为该API配置HTTP日志插件,记录详细的请求与响应信息。通过设置插件参数如日志接收地址、请求方式等,实现对API调用过程的有效监控。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建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
}

官方文档

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值