KONGA配置KONG添加http-log插件
简单说明:
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/
还没有评论,来说两句吧...