{"id":212748,"date":"2021-03-13T09:18:14","date_gmt":"2021-03-13T01:18:14","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=212748"},"modified":"2021-02-28T17:19:16","modified_gmt":"2021-02-28T09:19:16","slug":"springcloud-fusing-exception","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/springcloud-fusing-exception.html","title":{"rendered":"spring cloud\u4f18\u96c5\u7684\u5904\u7406feign\u7194\u65ad\u5f02\u5e38"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u5fae\u670d\u52a1\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7feign\u6765\u8c03\u7528\u5176\u4ed6\u5fae\u670d\u52a1\u7684\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u7194\u65ad\u6765\u907f\u514d\u96ea\u5d29\uff0c\u56e0\u4e3a\u6211\u4eec\u7194\u65ad\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u5feb\u901f\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\uff0c\u907f\u514d\u96ea\u5d29\uff1b\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u4e00\u822c\u4e0d\u5199\u7194\u65ad\u56de\u8c03\u65b9\u6cd5\uff0c\u76f4\u63a5\u6355\u83b7\u5f02\u5e38\uff0c\u8fd4\u56de\u9002\u5f53\u5f02\u5e38\u4fe1\u606f\u7ed9\u7528\u6237\u5373\u53ef\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1\u3001\u5b9e\u73b0<\/strong><\/div>\n
1-1\u3001\u914d\u7f6e<\/strong><\/span><\/div>\n

\u589e\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff0cfeign\u5f00\u542f\u7194\u65ad\u540e\uff0c\u8fdc\u7aef\u670d\u52a1\u7684\u5f02\u5e38\u4f1a\u88ab\u6d88\u5316\uff0c\u800c\u8d70\u7194\u65ad\u65b9\u6cd5\uff1b\u6240\u4ee5\uff0c\u9700\u8981\u4ee5\u4e0b\u914d\u7f6e\u914d\u7f6e\u6765\u5173\u95ed\u7194\u65ad\uff0c\u597d\u7edf\u4e00\u6355\u83b7\u670d\u52a1\u5f02\u5e38\uff1b<\/p>\n

\r\n# feign\u914d\u7f6e\r\nfeign.hystrix.enabled=false\r\n<\/pre>\n
1-2\u3001\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406<\/strong><\/span><\/div>\n

\u6355\u83b7feign\u5f02\u5e38\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u5728\u8c03\u7528\u5904 try...catch...\uff1b\u4e00\u79cd\u662f\u7edf\u4e00\u6355\u83b7\u5904\u7406\uff0c\u63a8\u8350\u540e\u8005\uff1b<\/p>\n

\u7edf\u4e00\u6355\u83b7\uff1a\u589e\u52a0\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5b9e\u73b0ErrorDecoder\u63a5\u53e3\uff0c\u5728\u91cc\u9762\u5c31\u53ef\u4ee5\u6355\u83b7feign\u5f02\u5e38\u5e76\u4e0a\u629b\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e86\uff1b<\/p>\n

\u7136\u540e\uff0c\u5728\u6211\u4eec\u4e4b\u524d\u5199\u7684\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u7684\u5c31\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38\uff0c\u5e76\u8fd4\u56de\u53cb\u597d\u7684\u4fe1\u606f\u7ed9\u7528\u6237\u4e86\uff1b<\/p>\n

\r\n@Configuration\r\npublic class FeignErrorDecoder implements ErrorDecoder\r\n{\r\n    public static final Logger log = LoggerFactory.getLogger(FeignErrorDecoder.class);\r\n    @Override\r\n    public Exception decode(String methodKey, Response response)\r\n    {\r\n        try\r\n        {\r\n            \/\/ \u83b7\u53d6\u5f02\u5e38\u4fe1\u606f\r\n            String message = Util.toString(response.body().asReader());\r\n            JSONObject jsonObject = JSONObject.parseObject(message);\r\n            log.error(message); \/\/\u8bb0\u5f55\u65e5\u5fd7\r\n            \/\/\u76f4\u63a5\u4e0a\u629b\u81ea\u5b9a\u4e49\u5f02\u5e38\r\n           \/\/ return new BizException(jsonObject.getInteger(\"code\"), jsonObject.getString(\"msg\"));\r\n            return new BizException(jsonObject.getInteger(\"resultCode\"), jsonObject.getString(\"resultMsg\"));\r\n        }\r\n        catch (Exception ex)\r\n        {\r\n            \/\/1\u3001\u8c03\u7528\u53c2\u6570\u5f02\u5e38\r\n            if (response.status() >= 400 && response.status() <= 500)\r\n            {\r\n                return new  BizException(ResultCode.INVALID_REQUEST);\r\n            }\r\n            else\r\n            {\r\n                return new BizException(ResultCode.INTERNAL_SERVER_ERROR);\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u589e\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff0cfeign\u5f00\u542f\u7194\u65ad\u540e\uff0c\u8fdc\u7aef\u670d\u52a1\u7684\u5f02\u5e38\u4f1a\u88ab\u6d88\u5316\uff0c\u800c\u8d70\u7194\u65ad\u65b9\u6cd5\uff1b\u6240\u4ee5\uff0c\u9700\u8981\u4ee5\u4e0b\u914d\u7f6e\u914d\u7f6e\u6765\u5173\u95ed\u7194\u65ad\uff0c\u597d […]<\/p>\n","protected":false},"author":1903,"featured_media":212762,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-212748","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=212748"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212748\/revisions"}],"predecessor-version":[{"id":212772,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212748\/revisions\/212772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/212762"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=212748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=212748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=212748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}