{"id":645,"date":"2023-03-23T21:42:05","date_gmt":"2023-03-23T13:42:05","guid":{"rendered":""},"modified":"2023-03-23T21:42:05","modified_gmt":"2023-03-23T13:42:05","slug":"SpringCloud \u65e5\u5fd7\u8fc7\u6ee4","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/645.html","title":{"rendered":"SpringCloud \u65e5\u5fd7\u8fc7\u6ee4"},"content":{"rendered":"
\n
<\/p>\n
shouldFilter(): <\/strong> shouldFilter()\u65b9\u6cd5\u68c0\u67e5\u8bf7\u6c42\u5e76\u786e\u5b9a\u662f\u5426\u8981\u6267\u884c\u8fc7\u6ee4\u5668\u3002<\/span> <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/body>
\n run(): <\/strong>\u5982\u679c!isFilterDisabled()<\/strong>\u548c shouldFilter()<\/strong>\u65b9\u6cd5\u90fd\u8fd4\u56de\uff0c\u5219run()\u65b9\u6cd5\u5c06\u8c03\u7528\u662f<\/strong>\u3002<\/span>
\n filterType(): <\/strong> filterType()\u65b9\u6cd5\u6309\u7c7b\u578b\u5bf9\u8fc7\u6ee4\u5668\u8fdb\u884c\u5206\u7c7b\u3002 Zuul\u4e2d\u6709\u56db\u79cd\u7c7b\u578b\u7684\u6807\u51c6\u8fc7\u6ee4\u5668: pre <\/strong>\u7528\u4e8e\u8def\u7531\u524d\u8fc7\u6ee4<\/strong>\uff0c\u8def\u7531<\/strong>\u7528\u4e8e\u8def\u7531\u5230\u539f\u70b9<\/strong>\uff0c\u53d1\u5e03<\/strong>(\u7528\u4e8e\u8def\u7531\u540e\u8fc7\u6ee4\u5668<\/strong>)\u548c\u9519\u8bef<\/strong>(\u7528\u4e8e\u9519\u8bef\u5904\u7406<\/strong>)\u3002 Zuul\u8fd8\u652f\u6301\u9759\u6001\u54cd\u5e94\u7684\u9759\u6001<\/strong>\u7c7b\u578b\u3002<\/strong>\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u65b9\u6cd5 runFilters(type)\u6765\u521b\u5efa\u6216\u6dfb\u52a0\u548c\u8fd0\u884c\u4efb\u4f55\u8fc7\u6ee4\u5668\u7c7b\u578b\u3002<\/strong> <\/span>
\n filterOrder()<\/strong>: \u5fc5\u987b\u4e3a\u8fc7\u6ee4\u5668\u5b9a\u4e49\u8fc7\u6ee4\u5668\u987a\u5e8f\u3002\u5982\u679c\u4f18\u5148\u7ea7\u5bf9\u4e8e\u8fc7\u6ee4\u5668\u4e0d\u91cd\u8981\uff0c\u5219\u8fc7\u6ee4\u5668\u53ef\u80fd\u5177\u6709\u76f8\u540c\u7684\u8fc7\u6ee4\u5668\u987a\u5e8f\u3002\u8fc7\u6ee4\u5668\u987a\u5e8f\u4e0d\u5fc5\u662f\u987a\u5e8f\u7684\u3002<\/span> <\/p>\n
\n Logger <\/strong>\u7c7b\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528
\n getLogger()<\/strong>\u65b9\u6cd5\u6765\u521b\u5efa\u8bb0\u5f55\u5668\u3002\n <\/div>\n\r\nprivate <\/span>Logger logger=LoggerFactory.getLogger<\/span>(this<\/span>.getClass<\/span>());\r\n<\/pre>\n
\n org.slf4j <\/strong>\u5305\u7684
\n Logger <\/strong>\u7c7b\u3002\n <\/div>\n
\n run()<\/strong>\u65b9\u6cd5\u4e2d\u5b9e\u73b0\u903b\u8f91\u3002\n <\/div>\n\r\npublic <\/span>Object run() throws <\/span>ZuulException\r\n{\r\n\/\/getting the current HTTP request that is to be handle\r\nHttpServletRequest request=RequestContext.getCurrentContext<\/span>().getRequest<\/span>();\r\n\/\/printing the detail of the request\r\nlogger.info<\/span>(\"request -> {} request uri-> {}\"<\/span>, request, request.getRequestURI<\/span>());\r\nreturn <\/span>null;\r\n}\r\n<\/pre>\n
\r\npackage <\/span>com.lidihuo.microservices.netflixzuulapigatewayserver;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.stereotype.Component;\r\nimport com.netflix.zuul.ZuulFilter;\r\nimport com.netflix.zuul.context.RequestContext;\r\nimport com.netflix.zuul.exception.ZuulException;\r\n@Component\r\npublic <\/span>class <\/span>ZuulLoggingFilter extends <\/span>ZuulFilter\r\n{\r\n\/\/creating Logger object\r\nprivate <\/span>Logger logger=LoggerFactory.getLogger<\/span>(this<\/span>.getClass<\/span>());\r\n@Override\r\npublic <\/span>booleanshouldFilter() \r\n{\r\nreturn true<\/span><\/span>; \/\/executing filter for <\/span>every request\r\n}\r\n\/\/log the content of the request\r\n@Override\r\npublic <\/span>Object run() throws <\/span>ZuulException\r\n{\r\n\/\/getting the current HTTP request that is to be handle\r\nHttpServletRequest request=RequestContext.getCurrentContext<\/span>().getRequest<\/span>();\r\n\/\/prints the detail of the requestin the log\r\nlogger.info<\/span>(\"request -> {} request uri-> {}\"<\/span>, request, request.getRequestURI<\/span>());\r\nreturn <\/span>null;\r\n}\r\n@Override\r\npublic <\/span>String filterType() \r\n{\r\nreturn <\/span>\"pre\"<\/span>; \/\/intercept all the request before execution\r\n}\r\n@Override\r\npublic <\/span>intfilterOrder() \r\n{\r\nreturn <\/span>1; \/\/setting filter order to 1\r\n}\r\n}\r\n<\/pre>\n
\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringCloud \u65e5\u5fd7\u8fc7\u6ee4zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106],"tags":[],"class_list":["post-645","post","type-post","status-publish","format-standard","hentry","category-bc_springcloud_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/645"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=645"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/645\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}