KONGA配置KONG添加http-log插件

本文介绍如何使用KONG的HTTP Log插件来记录API请求的日志,并通过一个实战例子展示了配置过程及自定义日志处理控制器的实现。

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

简单说明:
http-log插件,就是KONG转发完请求之后,异步发送post请求给所配置的自定义http日志处理地址。然后我们自定义的地址去处理请求参数,记录日志。

配置如下:
在services->routers->plugins->add plugins,(这样进入可以直接让router关联插件)
在这里插入图片描述
在这里插入图片描述

http endpoint 就是我们自定义的处理地址,然后consumer不填,即为为所有consumer添加该插件。
在这里插入图片描述

自定义处理controller(我这里仅仅打印,没做处理,有需要可自行处理):

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@Slf4j
public class TestController {

    @RequestMapping(value = "/testlog")
    public JSONObject testlog(HttpServletRequest request) throws IOException {
        String string = IoUtil.read(request.getInputStream(), "UTF-8");
        System.out.println(">>>参数:" + string);
        Map<String, String[]> map = request.getParameterMap();
        JSONObject res = new JSONObject();
        if (map != null && map.size() != 0)
            res = JSONObject.parseObject(JSONObject.toJSONString(map));
        else
            res.put("res", "ok");
        return res;
    }
}

参数的打印信息如下:

{
	"request": {
		"headers": {
			"cache-control": "max-age=0",
			"connection": "keep-alive",
			"cookie": "io=mNwwgVgMqOrlHgZgAAAM",
			"accept-encoding": "gzip, deflate",
			"if-modified-since": "Thu, 01 Jul 2021 09:56:44 GMT",
			"accept-language": "zh-CN,zh;q=0.9",
			"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
			"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
			"upgrade-insecure-requests": "1",
			"host": "172.16.6.146:8000"
		},
		"url": "http://172.16.6.146:8000/hsa-local-test/api/hsa-pss-pw-ups/swagger-ui.html",
		"method": "GET",
		"uri": "/hsa-local-test/api/hsa-pss-pw-ups/swagger-ui.html",
		"querystring": {},
		"size": 594
	},
	"tries": [{
		"balancer_start": 1625796122795,
		"ip": "172.16.6.146",
		"balancer_latency": 0,
		"port": 8801
	}],
	"started_at": 1625796122794,
	"service": {
		"ws_id": "18957d6e-193f-4bf1-bf78-256ecb71f962",
		"tags": {},
		"path": "/hsa-pss-pw/",
		"retries": 5,
		"created_at": 1625712150,
		"read_timeout": 60000,
		"connect_timeout": 60000,
		"protocol": "http",
		"id": "0871d648-35f6-4180-8ebe-9c02d1162444",
		"updated_at": 1625713035,
		"name": "pw-ups",
		"write_timeout": 60000,
		"port": 8700,
		"host": "pw-ups"
	},
	"response": {
		"status": 304,
		"headers": {
			"last-modified": "Thu, 01 Jul 2021 09:56:44 GMT",
			"x-kong-upstream-latency": "7",
			"connection": "close",
			"date": "Fri, 09 Jul 2021 02:02:02 GMT",
			"via": "kong/2.4.1",
			"x-kong-proxy-latency": "1"
		},
		"size": 194
	},
	"upstream_uri": "/hsa-pss-pw/swagger-ui.html",
	"client_ip": "172.16.6.158",
	"latencies": {
		"request": 8,
		"proxy": 7,
		"kong": 1
	},
	"route": {
		"ws_id": "18957d6e-193f-4bf1-bf78-256ecb71f962",
		"protocols": ["http"],
		"paths": ["/hsa-local-test/api/hsa-pss-pw-ups/"],
		"methods": ["GET", "POST"],
		"id": "55ff656b-71bd-4262-abab-1440597ac553",
		"strip_path": true,
		"name": "pw-ups-r",
		"regex_priority": 0,
		"request_buffering": true,
		"response_buffering": true,
		"path_handling": "v1",
		"updated_at": 1625736658,
		"https_redirect_status_code": 426,
		"preserve_host": false,
		"service": {
			"id": "0871d648-35f6-4180-8ebe-9c02d1162444"
		},
		"created_at": 1625712654
	}
}

官网文档地址:
https://docs.konghq.com/hub/kong-inc/http-log/

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值