api 网关 kong 数据库记录请求响应报文

简介: Kong的tcp-log-with-body插件是一个高效的工具,它能够转发Kong处理的请求和响应。这个插件非常适用于需要详细记录API请求和响应信息的情景,尤其是在调试和排查问题时。

2000元阿里云代金券免费领取,2核4G云服务器仅664元/3年,新老用户都有优惠,立即抢购>>>


阿里云采购季(云主机223元/3年)活动入口:请点击进入>>>,


阿里云学生服务器(9.5元/月)购买入口:请点击进入>>>,

tcp-log-with-body

介绍

Kong的tcp-log-with-body插件是一个高效的工具,它能够转发Kong处理的请求和响应。这个插件非常适用于需要详细记录API请求和响应信息的情景,尤其是在调试和排查问题时。

软件环境说明

  • kong version 2.1.4
  • CentOS version 7.3.1611

插件安装教程

  1. 找到 kong 插件存放位置
    ```bash
    ─[root@bwg] - [/usr/local/share/lua/5.1/kong/plugins] - [2024-02-28 11:52:33]
    └─[0] ls
    acl basic-auth file-log http-log-with-body key-auth post-function request-size-limiting session udp-log
    acme bot-detection grpc-gateway http-mirror ldap-auth pre-function request-termination statsd zipkin
    aws-lambda correlation-id grpc-web ip-restriction loggly prometheus request-transformer syslog
    azure-functions cors hmac-auth jwt log-serializers proxy-cache response-ratelimiting tcp-log
    base_plugin.lua datadog http-log kafka-log oauth2 rate-limiting response-transformer tcp-log-with-body
2. git 克隆项目到plugins目录下
```bash
git clone https://gitee.com/XiongMingcai/tcp-log-with-body.git
  1. 修改kong配置文件 加入启用tcp-log-with-body 插件
vim /etc/kong/kong.conf

修改位置

plugins = bundled,tcp-log-with-body
  1. 重启kong

监听启动错误日志

tail -f /usr/local/kong/logs/error.log

重启kong

sudo systemctl restart kong

使用说明

  1. 配置tcp-log-with-body插件

输入图片说明

  1. 接收日志tcp请求 node.js 模拟tcp-log-server
const net = require('net');
const {inspect} = require("util");

// Create a server instance
const server = net.createServer((socket) => {


    socket.on('data', (data) => {
 // 将数据转换为字符串,并以JSON格式输出

        const dataString = data.toString('utf8');
        console.log("Received data:", inspect(JSON.parse(dataString), false, null, true));

});


    socket.on('end', () => {

    });
});

// Error callback
server.on('error', (err) => {
    console.error('Server error:', err);
});

// Listening callback
server.listen(9999, '127.0.0.1', () => {
    console.log('Server is listening on 127.0.0.1:9999');
});
  1. 效果
.....
  request: {
   
   
    querystring: {
   
   },
    size: '563',
    uri: '/ok',
    url: 'https://ssl.hunangl.com:443/ok',
    headers: {
   
   
      host: 'ssl.hunangl.com',
      authorization: 'REDACTED',
      'postman-token': 'a0cf800e-06ac-41b0-8a4d-d849c945cee1',
      accept: '*/*',
      digest: 'SHA-256=eji/gfOD9pQzrW6QDTWz4jhVk/dqe3q11DVbi6Qe4ks=',
      'request-id': '5c54a71f-9bdb-445c-8549-f4af181ad49c',
      'cache-control': 'no-cache',
      'content-length': '13',
      'accept-encoding': 'gzip, deflate, br',
      'user-agent': 'PostmanRuntime/7.36.3',
      'x-date': 'Thu, 29 Feb 2024 05:14:21 GMT',
      connection: 'keep-alive',
      'content-type': 'application/json'
    },
    body: '{"foo":"bar"}',
    method: 'POST'
  },
  client_ip: '54.86.50.139',
.....
response: {
   
   
    body: '{
   
   "host":"0.0.0.0:8300","connection":"keep-alive","x-forwarded-for":"54.86.50.139","x-forwarded-proto":"https","x-forwarded-host":"ssl.hunangl.com","x-forwarded-port":"443","x-real-ip":"54.86.50.139","content-length":"13","x-date":"Thu, 29 Feb 2024 05:14:21 GMT","authorization":"hmac username=\\"hmac_username\\", algorithm=\\"hmac-sha256\\", headers=\\"x-date request-line digest\\", signature=\\"Kh+sGhrL3NbFNa9dsLdTs/q6hMBublOEPYUw4j8rLGM=\\"","digest":"SHA-256=eji/gfOD9pQzrW6QDTWz4jhVk/dqe3q11DVbi6Qe4ks=","content-type":"application/json","user-agent":"PostmanRuntime/7.36.3","accept":"*/*","cache-control":"no-cache","postman-token":"a0cf800e-06ac-41b0-8a4d-d849c945cee1","accept-encoding":"gzip, deflate, br","request-id":"5c54a71f-9bdb-445c-8549-f4af181ad49c"}',
    headers: {
   
   
      'content-type': 'application/json; charset=utf-8',
      date: 'Thu, 29 Feb 2024 05:14:15 GMT',
      connection: 'close',
      'server-port': '8300',
      'request-ip': '54.86.50.139',
      'content-length': '761',
      via: 'kong/2.1.4',
      'x-kong-proxy-latency': '6',
      'x-kong-upstream-latency': '12',
      'request-id': '5c54a71f-9bdb-445c-8549-f4af181ad49c'
    },
    status: 200,
    size: '1075'
  },
.....
目录
相关文章
|
5天前
|
缓存
SpringCloud Gateway 网关的请求体body的读取和修改
SpringCloud Gateway 框架中,为了处理请求体body,实现多次读取与修改,创建了一个名为`RequestParamGlobalFilter`的全局过滤器。这个过滤器使用`@Component`和`@Slf4j`注解,实现了`GlobalFilter`和`Ordered`接口,设置最高优先级以首先读取body。它通过缓存请求体并创建装饰过的`ServerHttpRequest`来实现body的动态获取。
84 4
|
5天前
|
API 数据库 C语言
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
175 0
|
5天前
|
API
阿里云微服务引擎及 API 网关 2024 年 2 月产品动态
阿里云微服务引擎及 API 网关 2024 年 2 月产品动态
|
5天前
|
人工智能 API
阿里云微服务引擎及 API 网关 2024 年 4 月产品动态
阿里云微服务引擎及 API 网关 2024 年 4 月产品动态。
|
5天前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2024 年 04 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要。
|
5天前
|
SQL Java 数据库连接
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
12 0
|
5天前
|
SQL 关系型数据库 API
从API获取数据并将其插入到PostgreSQL数据库:步骤解析
使用Python处理从API获取的数据并插入到PostgreSQL数据库:安装`psycopg2`,建立数据库连接,确保DataFrame与表结构匹配,然后使用`to_sql`方法将数据插入到已存在的表中。注意数据准备、权限设置、性能优化和安全处理。
|
5天前
|
API 数据库
ionic4 接收API数据库传值并显示
ionic4 接收API数据库传值并显示
23 0
|
5天前
|
API
阿里云微服务引擎及 API 网关 2024 年 3 月产品动态
阿里云微服务引擎及 API 网关 2024 年 3 月产品动态。
|
5天前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2024 年 03 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要。
http://www.vxiaotou.com