{"id":340377,"date":"2021-02-09T06:31:37","date_gmt":"2021-02-08T22:31:37","guid":{"rendered":"http:\/\/4563.org\/?p=340377"},"modified":"2021-02-09T06:31:37","modified_gmt":"2021-02-08T22:31:37","slug":"spring-mvc-%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90-requestid-%e5%b9%b6%e5%9c%a8%e9%9c%80%e8%a6%81%e6%97%b6%e8%ae%bf%e9%97%ae","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=340377","title":{"rendered":"Spring MVC: \u5982\u4f55\u81ea\u52a8\u751f\u6210 RequestID \u5e76\u5728\u9700\u8981\u65f6\u8bbf\u95ee"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Spring MVC: \u5982\u4f55\u81ea\u52a8\u751f\u6210 RequestID \u5e76\u5728\u9700\u8981\u65f6\u8bbf\u95ee               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : feng32 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u76ee\u524d\u7684\u7a0b\u5e8f\u7ed3\u6784\u5927\u6982\u662f\u8fd9\u6837\u7684:<\/p>\n<pre><code>\/\/ base controller \u4e2d\u5b9a\u4e49\u4e86\u5f02\u5e38\u60c5\u51b5\u4e0b\u7684 API \u8fd4\u56de public class BaseController {     @ExceptionHandler(CodeMessageException.class)     @ResponseBody     protected ResultModel&lt;Long&gt; exception(CodeMessageException e) {         log.error(\"{}\", e.getMessage(), e);         return new ResultModel&lt;&gt;(e.getCode(), e.getMessage());     } }  \/\/ \u6709\u4e00\u4e2a Filter \u5df2\u7ecf\u5b9e\u73b0\u4e86\u6253\u5370 Request \/ Response Body (\u901a\u8fc7 Filter \u5b9e\u73b0\u4e86\u4e00\u4e9b\u5168\u5c40\u7684\u5904\u7406\u903b\u8f91) public class HttpLogFilter implements Filter {     @Override     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {         ...     } }  \/\/ \u4e1a\u52a1 Controller \u7ee7\u627f BaseController @Controller public class DeviceController extends BaseController {     @PostMapping(\"\/device\/activate\")     @ResponseBody     public ResultModel&lt;Long&gt; activateDevice(@RequestBody ActivateDeviceRequest request) {         return deviceService.activateDevice(request.getSn());     }  \/\/ Service \u5c42\u53ef\u4ee5\u8fdb\u884c\u5fc5\u8981\u7684\u53c2\u6570\u68c0\u67e5\uff0c\u7136\u540e\u76f4\u63a5\u629b\u4e00\u4e2a\u5f02\u5e38\u51fa\u6765 \/\/ \u5f02\u5e38\u88ab BaseController \u6355\u6349\uff0c\u7136\u540e\u8f6c\u6210 JSON\uff0c\u8fd9\u6837\u4ee3\u7801\u770b\u4e0a\u53bb\u5c31\u5f88\u5e72\u51c0 public ResultModel&lt;Long&gt; activateDevice(String sn) {     if (sn == null) {         throw new CodeMessageException(ERROR_PARAM_MISSING, \"Parameter sn is missing\");     }     ... } <\/code><\/pre>\n<p>\u95ee\u9898\u6765\u4e86\uff0c\u73b0\u5728\u9700\u8981\u7ed9\u6bcf\u4e2a Request \u751f\u6210\u4e00\u4e2a RequestID (UUID)\uff0c\u8fd9\u4e2a ID \u662f\u540e\u7aef\u751f\u6210\u7684\uff0c\u8c03\u7528\u8005\u4e0d\u9700\u8981\u5199\u5bf9\u5e94\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u8c03\u7528\u8005\u5728 Response \u4e2d\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a Request ID<\/p>\n<p>\u5982\u4f55\u4ee5\u5c3d\u91cf\u5c0f\u7684\u4fb5\u5165\u6027\u5b9e\u73b0\u8fd9\u4e2a\u7279\u6027\u5462\uff1f\u57fa\u672c\u7684\u9700\u6c42\u662f Service \u5c42\u4ee5\u53ca BaseController (\u5f02\u5e38\u5904\u7406\u903b\u8f91) \u90fd\u80fd\u591f\u6bd4\u8f83\u7b80\u5355\u5730\u53d6\u5230\u8fd9\u4e2a\u503c\u5e76\u8fd4\u56de<\/p>\n<p>\u540c\u65f6\u5e0c\u671b\u6253\u5370 Request (\u5305\u62ec url \/ body)\u7684\u65f6\u5019\uff0c\u4e5f\u80fd\u591f\u628a Request ID \u6253\u5370\u51fa\u6765\uff0c\u5426\u5219\u8bf7\u6c42\u91cf\u5927\u7684\u65f6\u5019\uff0c\u65e5\u5fd7\u8fd8\u662f\u4f1a\u5bf9\u4e0d\u4e0a<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5259472\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ltoddy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u4e3a\u4ec0\u4e48\u8981\u5728\u670d\u52a1\u7aef\u751f\u6210\u554a\uff0c\u4e0d\u5e94\u8be5\u662f nginx \u6216\u8005 api-gateway \u90a3\u91cc\u751f\u6210\u561b\uff0c\u7136\u540e\u628a request id \u900f\u4f20\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259473\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : huifer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             spring boot \u91cc\u9762\u5199\u4e2a starter \u53ef\u4ee5\u5417. \u6216\u8005\u5f15\u5165\u7edf\u4e00\u62e6\u622a\u5668\uff0c\u6216\u8005\u7528 gateway \u4e0a\u9762\u52a0\u5165\u8fd9\u4e2a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259474\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : buliugu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u5982\u8001\u5b9e\u4e0a\u4e00\u5957 APM                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259475\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhiguang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             requestwrapper                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259476\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cs419 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u751f\u6210 reqId\uff1aFilter + ThreadLocal <br \/>resp \u6dfb\u52a0 reqId: HandlerMethodReturnValueHandler                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259477\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jaynos <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e4b\u524d\u597d\u50cf\u5b9e\u73b0\u8fc7\u4e00\u4e2a\uff0c\u901a\u8fc7\u58f0\u660e\u4e00\u4e2a Request \u4f5c\u7528\u57df\u7684 Bean \u5b9e\u73b0\uff0c\u6784\u9020\u51fd\u6570\u91cc\u76f4\u63a5\u968f\u673a\u4e00\u4e2a\u5b57\u7b26\u4e32                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259478\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lychs1998 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u6211\u4ee5\u524d\u505a\u8fc7\uff0c\u76f8\u5f53\u4e8e\u94fe\u8def\u8ffd\u8e2a\u7528\u7684 ID \u5457\uff1f<\/p>\n<p>\u5927\u81f4\u7684\u903b\u8f91\u7ed9\u4f60\u4e00\u4e2a\u53c2\u8003\uff1a<br \/>1.\u8fd4\u56de\u7684 response \u7ed3\u6784\u662f\u6211\u81ea\u5df1\u5b9a\u4e49\u7684\uff0c\u91cc\u9762\u4e00\u4e2a\u5c5e\u6027\u662f TraceId \u3002<br \/>2.\u7136\u540e\u5b9e\u73b0\u4e00\u4e2a Context \u7c7b\uff0c\u89c1\u4ee3\u7801\u7247\u6bb5\uff1a https:\/\/gist.github.com\/lychs1998\/1679bc9c155744ba23ea99307244dad8 <button onclick=\"lazyGist(this)\">\u00a0\u663e\u793a Gist \u4ee3\u7801\u00a0<\/button> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259479\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : choice4 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             filter+threadlocal \u4fdd\u5b58 id,responsebodyadvice \u5305\u88c5\u54cd\u5e94                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259480\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Geraltt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u751f\u6210 logId\uff0c\u653e\u5230 threadlocal \u548c mdc \u91cc\u9762 \u76f4\u63a5\u6253\u5370\uff0c\u6700\u540e\u5305\u88c5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259481\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luozic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9664\u4e86 context \u52a0\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 jvm \u7684\u540e\u95e8\u52a0\u8fdb\u53bb\uff0cinstrument \u673a\u5236\uff0c\u4e0d\u8fc7\u63a8\u8350\u5bf9\u63a5 metrics \u67b6\u6784\uff0c\u53ef\u4ee5\u5e73\u6ed1\u7531\u65e5\u5fd7\u5230 apm \u5e73\u53f0\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259482\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : samun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u7684 nginx+lua \u5904\u7406\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5259483\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : axbx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             filte \u6dfb\u52a0\u4e00\u4e2a trace_id,\u7136\u540e\u4ece Context \u4e2d\u83b7\u53d6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Spring MVC: \u5982\u4f55\u81ea\u52a8\u751f&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/340377"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340377"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/340377\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340377"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}