KONGA配置KONG添加http-log插件

不念不忘少年蓝@ 2022-10-19 04:14 604阅读 0赞

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

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

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

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

  1. import cn.hutool.core.io.IoUtil;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import javax.servlet.http.HttpServletRequest;
  7. import java.io.IOException;
  8. import java.util.Map;
  9. @RestController
  10. @Slf4j
  11. public class TestController {
  12. @RequestMapping(value = "/testlog")
  13. public JSONObject testlog(HttpServletRequest request) throws IOException {
  14. String string = IoUtil.read(request.getInputStream(), "UTF-8");
  15. System.out.println(">>>参数:" + string);
  16. Map<String, String[]> map = request.getParameterMap();
  17. JSONObject res = new JSONObject();
  18. if (map != null && map.size() != 0)
  19. res = JSONObject.parseObject(JSONObject.toJSONString(map));
  20. else
  21. res.put("res", "ok");
  22. return res;
  23. }
  24. }

参数的打印信息如下:

  1. {
  2. "request": {
  3. "headers": {
  4. "cache-control": "max-age=0",
  5. "connection": "keep-alive",
  6. "cookie": "io=mNwwgVgMqOrlHgZgAAAM",
  7. "accept-encoding": "gzip, deflate",
  8. "if-modified-since": "Thu, 01 Jul 2021 09:56:44 GMT",
  9. "accept-language": "zh-CN,zh;q=0.9",
  10. "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",
  11. "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",
  12. "upgrade-insecure-requests": "1",
  13. "host": "172.16.6.146:8000"
  14. },
  15. "url": "http://172.16.6.146:8000/hsa-local-test/api/hsa-pss-pw-ups/swagger-ui.html",
  16. "method": "GET",
  17. "uri": "/hsa-local-test/api/hsa-pss-pw-ups/swagger-ui.html",
  18. "querystring": { },
  19. "size": 594
  20. },
  21. "tries": [{
  22. "balancer_start": 1625796122795,
  23. "ip": "172.16.6.146",
  24. "balancer_latency": 0,
  25. "port": 8801
  26. }],
  27. "started_at": 1625796122794,
  28. "service": {
  29. "ws_id": "18957d6e-193f-4bf1-bf78-256ecb71f962",
  30. "tags": { },
  31. "path": "/hsa-pss-pw/",
  32. "retries": 5,
  33. "created_at": 1625712150,
  34. "read_timeout": 60000,
  35. "connect_timeout": 60000,
  36. "protocol": "http",
  37. "id": "0871d648-35f6-4180-8ebe-9c02d1162444",
  38. "updated_at": 1625713035,
  39. "name": "pw-ups",
  40. "write_timeout": 60000,
  41. "port": 8700,
  42. "host": "pw-ups"
  43. },
  44. "response": {
  45. "status": 304,
  46. "headers": {
  47. "last-modified": "Thu, 01 Jul 2021 09:56:44 GMT",
  48. "x-kong-upstream-latency": "7",
  49. "connection": "close",
  50. "date": "Fri, 09 Jul 2021 02:02:02 GMT",
  51. "via": "kong/2.4.1",
  52. "x-kong-proxy-latency": "1"
  53. },
  54. "size": 194
  55. },
  56. "upstream_uri": "/hsa-pss-pw/swagger-ui.html",
  57. "client_ip": "172.16.6.158",
  58. "latencies": {
  59. "request": 8,
  60. "proxy": 7,
  61. "kong": 1
  62. },
  63. "route": {
  64. "ws_id": "18957d6e-193f-4bf1-bf78-256ecb71f962",
  65. "protocols": ["http"],
  66. "paths": ["/hsa-local-test/api/hsa-pss-pw-ups/"],
  67. "methods": ["GET", "POST"],
  68. "id": "55ff656b-71bd-4262-abab-1440597ac553",
  69. "strip_path": true,
  70. "name": "pw-ups-r",
  71. "regex_priority": 0,
  72. "request_buffering": true,
  73. "response_buffering": true,
  74. "path_handling": "v1",
  75. "updated_at": 1625736658,
  76. "https_redirect_status_code": 426,
  77. "preserve_host": false,
  78. "service": {
  79. "id": "0871d648-35f6-4180-8ebe-9c02d1162444"
  80. },
  81. "created_at": 1625712654
  82. }
  83. }

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

发表评论

表情:
评论列表 (有 0 条评论,604人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Kong: ACL

    简述 这个插件使用控制谁可以访问,谁不能访问的。如果使用这个插件,就必须使用认证的插件了比如base-auth, key-auth等。拿base-auth举例: ...

    相关 Kong: JWT

    JWT的简述 JWT是JSON Web Token的缩写,使用cookie的解决跨域认证的问题,和cas系统是不一样的理论。具体可以看一下大神些的文档:http://...

    相关 Kong: hmac

    hmac简述 hmac是Hashing for Message Authentication的简写,可以用来保证数据的完整,客户端把内容通过散列/哈希算法算出一个摘要...

    相关 KONGKONGA部署及配置

    个人理解,仅供参考: 首先,kong+konga除去其他高级功能,个人觉得就是把nginx,变成可以页面配置的了。比如,配置的router,service就是反向代理,配置