{"id":231593,"date":"2021-12-17T10:12:24","date_gmt":"2021-12-17T02:12:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=231593"},"modified":"2021-12-09T10:13:05","modified_gmt":"2021-12-09T02:13:05","slug":"improve-api-performance","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/improve-api-performance.html","title":{"rendered":"\u4e03\u5927\u5999\u62db\u52a9\u4f60\u63d0\u5347API\u7684\u6027\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6df1\u601d\u719f\u8651\u7684\u8bbe\u8ba1\u8fd8\u5fc5\u987b\u8003\u8651\u5230 API \u7684\u6027\u80fd\uff0c\u5982\u679c API \u4e0d\u80fd\u54cd\u5e94\u8d8a\u6765\u8d8a\u591a\u7684\u8bf7\u6c42\uff0c\u4e0d\u80fd\u6ee1\u8db3\u4e0d\u65ad\u53d8\u5316\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4e0d\u80fd\u6309\u9884\u671f\u8fd0\u884c\uff0c\u826f\u597d\u7684\u8bbe\u8ba1\u5c31\u6beb\u65e0\u610f\u4e49\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6df1\u601d\u719f\u8651\u7684\u8bbe\u8ba1\u8fd8\u5fc5\u987b\u8003\u8651\u5230 API \u7684\u6027\u80fd\uff0c\u5982\u679c API \u4e0d\u80fd\u54cd\u5e94\u8d8a\u6765\u8d8a\u591a\u7684\u8bf7\u6c42\uff0c\u4e0d\u80fd\u6ee1\u8db3\u4e0d\u65ad\u53d8\u5316\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4e0d\u80fd\u6309\u9884\u671f\u8fd0\u884c\uff0c\u826f\u597d\u7684\u8bbe\u8ba1\u5c31\u6beb\u65e0\u610f\u4e49\u3002
\n\"\"<\/p>\n

\u90a3\u4ec0\u4e48\u662f API \u7684\u6027\u80fd?<\/strong><\/div>\n

\u4e0e\u4efb\u4f55\u6027\u80fd\u4e00\u6837\uff0cAPI \u6027\u80fd\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u53d6\u51b3\u4e8e API \u5982\u4f55\u54cd\u5e94\u5b83\u6536\u5230\u7684\u4e0d\u540c\u7c7b\u578b\u7684\u8bf7\u6c42\u3002<\/p>\n

\u6bd4\u5982\uff1a\u6211\u4eec\u6709\u4e00\u4e2a\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f\uff0c\u663e\u793a\u5ba2\u6237\u7684\u5f53\u524d\u8ba2\u5355\u3002\u5e94\u7528\u7a0b\u5e8f\u4ece API \u83b7\u53d6\u8ba2\u5355\u8be6\u7ec6\u4fe1\u606f\u3002\u4f46\u73b0\u5728\uff0c\u5ba2\u6237\u8868\u793a\uff0c\u4ed6\u4eec\u60f3\u67e5\u770b\u6240\u6709\u8ba2\u5355\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u6784\u5efa\u4e86\u4e00\u4e2a\u201c\u6211\u7684\u8ba2\u5355\u201d\u9875\u9762\uff0c\u7528\u4e8e\u663e\u793a\u5ba2\u6237\u7684\u6240\u6709\u8ba2\u5355\u3002\u8fd9\u610f\u5473\u7740\uff0c\u6211\u4eec\u7684 API \u5c06\u8fd4\u56de\u6bd4\u4ee5\u524d\u66f4\u591a\u7684\u6570\u636e\uff0c\u6bd4\u4ee5\u524d\u627f\u53d7\u66f4\u5927\u7684\u8d1f\u8f7d\u3002<\/p>\n

\u5982\u4f55\u786e\u4fdd\u6211\u4eec\u7684 API \u80fd\u591f\u8fd4\u56de\u6240\u6709\u6570\u636e\u800c\u4e0d\u4f1a\u51fa\u73b0\u5ef6\u8fdf\u3001\u670d\u52a1\u5668\u7aef\u9519\u8bef\u548c\u8fc7\u591a\u8bf7\u6c42\u7b49\u95ee\u9898?\u8fd9\u91cc\u6709\u4e00\u4e9b\u6027\u80fd\u63d0\u5347\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n

1\u3001\u7f29\u5c0f\u548c\u9650\u5236\u6709\u6548\u8d1f\u8f7d\u5927\u5c0f<\/strong><\/span><\/div>\n

\u4f20\u8f93\u6570\u636e\u91cf\u5927\u7684\u65f6\u5019\uff0c\u5fc5\u7136\u4f1a\u5bfc\u81f4 API \u6027\u80fd\u4e0b\u964d\uff0c\u800c\u6700\u76f4\u63a5\u7684\u529e\u6cd5\u5c31\u662f\u964d\u4f4e API \u4f20\u8f93\u7684\u8d1f\u8f7d(payload)\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 GZip \u538b\u7f29\u6765\u7f29\u5c0f\u6709\u6548\u8f7d\u8377\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u5728 Web API \u4e0a\u4f7f\u7528 Deflate compression\u3002\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Accept-Encoding \u6807\u9898\u66f4\u65b0\u4e3a gzip\u3002<\/p>\n

2\u3001\u542f\u7528\u7f13\u5b58<\/strong><\/span><\/div>\n

\u7f13\u5b58\u662f\u63d0\u9ad8 API \u6027\u80fd\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u4e4b\u4e00\u3002\u5982\u679c\u6211\u4eec\u7684\u8bf7\u6c42\u76f8\u540c\u7684 API\uff0c\u90a3\u4e48\u8be5\u54cd\u5e94\u7684\u7f13\u5b58\u7248\u672c\u6709\u52a9\u4e8e\u907f\u514d\u989d\u5916\u7684\u670d\u52a1\u8c03\u7528\u6216\u6570\u636e\u5e93\u67e5\u8be2\u3002<\/p>\n

\u5728\u4f7f\u7528\u7f13\u5b58\u65f6\uff0c\u60a8\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7f13\u5b58\u6dd8\u6c70\u7b97\u6cd5\uff0c\u5728\u53d1\u751f\u65b0\u6570\u636e\u66f4\u65b0\u65f6\uff0c\u7f13\u5b58\u4e5f\u8981\u53ca\u65f6\u66f4\u65b0\u3002<\/p>\n

3\u3001\u63d0\u4f9b\u8db3\u591f\u7684\u7f51\u7edc\u5e26\u5bbd<\/strong><\/span><\/div>\n

\u5373\u4f7f\u662f\u8bbe\u8ba1\u6700\u5f3a\u5927\u7684 API\uff0c\u7f13\u6162\u7684\u7f51\u7edc\u4e5f\u4f1a\u964d\u4f4e\u6027\u80fd\u3002\u4e0d\u53ef\u9760\u7684\u7f51\u7edc\u53ef\u80fd\u4f1a\u5bfc\u81f4\u505c\u673a\uff0c\u89e3\u51b3\u8fd9\u4e2a\u4e5f\u76f8\u5bf9\u7b80\u5355\uff0c\u591a\u82b1\u94b1\u6295\u8d44\u4e8e\u9002\u5f53\u7684\u7f51\u7edc\u57fa\u7840\u8bbe\u65bd\uff0c\u8fd9\u6837\u6211\u4eec\u624d\u80fd\u4fdd\u6301\u7406\u60f3\u7684\u6027\u80fd\u6c34\u5e73\u3002<\/p>\n

\u6b64\u5916\uff0c\u5982\u679c\u60a8\u6709\u5927\u91cf\u540e\u53f0\u8fdb\u7a0b\uff0c\u8bf7\u5728\u5355\u72ec\u7684\u7ebf\u7a0b\u4e0a\u8fd0\u884c\u8fd9\u4e9b\u8fdb\u7a0b\uff0c\u4ee5\u907f\u514d\u963b\u6b62\u8bf7\u6c42\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u955c\u50cf\u548c CDN\u5728\u5168\u7403\u4e0d\u540c\u5730\u533a\u66f4\u5feb\u5730\u670d\u52a1\u8bf7\u6c42\u3002<\/p>\n

4\u3001\u9650\u901f\u548c\u9632\u6b62\u6076\u610f\u653b\u51fb<\/strong><\/span><\/div>\n

API \u53ef\u80fd\u4f1a\u53d7\u5230 DDoS \u653b\u51fb\uff0c\u8be5\u653b\u51fb\u53ef\u80fd\u662f\u6076\u610f\u548c\u6545\u610f\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u5de5\u7a0b\u5e08\u8c03\u7528API\u5728\u67d0\u4e9b\u672c\u5730\u5e94\u7528\u7a0b\u5e8f\u7684\u5faa\u73af\u4e2d\u6267\u884c\u65f6\u6545\u610f\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u6d4b\u91cf\u4ea4\u6613\u5e76\u76d1\u63a7\u6bcf\u4e2a IP \u5730\u5740\u6216\u6bcf\u4e2aSSO\/JWT\u4ee4\u724c\u7684\u6bcf\u79d2\u8c03\u7528\u6b21\u6570\uff0c\u5bf9\u6076\u610f\u8bf7\u6c42\u8fdb\u884c\u5c4f\u853d\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n

\u8fd9\u79cd\u901f\u7387\u9650\u5236\u65b9\u6cd5\u6709\u52a9\u4e8e\u51cf\u5c11\u5bf9 API \u7684\u8fc7\u5ea6\u8bf7\u6c42\uff0c\u5e76\u4e3b\u52a8\u76d1\u63a7\u548c\u8bc6\u522b\u53ef\u80fd\u7684\u6076\u610f\u6d3b\u52a8\u3002<\/p>\n

5\u3001\u5c1d\u8bd5\u4f7f\u7528 PATCH<\/strong><\/span><\/div>\n

\u5de5\u7a0b\u5e08\u4eec\u666e\u904d\u8ba4\u4e3a\uff0cPUT \u548c PATCH \u64cd\u4f5c\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\u3002\u4ed6\u4eec\u5728\u66f4\u65b0\u8d44\u6e90\u65b9\u9762\u76f8\u4f3c\uff0c\u4f46\u4ed6\u4eec\u5404\u81ea\u6267\u884c\u66f4\u65b0\u7684\u65b9\u5f0f\u4e0d\u540c\uff1aPUT \u64cd\u4f5c\u901a\u8fc7\u5411\u6574\u4e2a\u8d44\u6e90\u53d1\u9001\u66f4\u65b0\u6765\u66f4\u65b0\u8d44\u6e90\u3002PATCH \u64cd\u4f5c\u4ec5\u5bf9\u9700\u8981\u66f4\u65b0\u7684\u8d44\u6e90\u5e94\u7528\u90e8\u5206\u66f4\u65b0\u3002\u56e0\u6b64 PATCH \u8c03\u7528\u4ea7\u751f\u8f83\u5c0f\u7684\u8d1f\u8f7d\uff0c\u5e76\u5927\u89c4\u6a21\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n

\u4e0d\u8fc7\uff0c\u5373\u4f7f PATCH \u8c03\u7528\u53ef\u4ee5\u9650\u5236\u8bf7\u6c42\u5927\u5c0f\uff0c\u4e5f\u5e94\u8be5\u6ce8\u610f\u5b83\u4e0d\u662f\u5e42\u7b49\u7684\u3002PATCH \u53ef\u4ee5\u901a\u8fc7\u4e00\u7cfb\u5217\u591a\u4e2a\u8c03\u7528\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5e94\u8be5\u4ed4\u7ec6\u548c\u6545\u610f\u5730\u8003\u8651\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u4f7f\u7528 PATCH \u8bf7\u6c42\uff0c\u5e76\u786e\u4fdd\u5728\u9700\u8981\u65f6\u5b83\u4eec\u53ef\u4ee5\u5e42\u7b49\u5730\u5b9e\u73b0\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u4f7f\u7528 PUT \u8bf7\u6c42\u3002<\/p>\n

6\u3001\u542f\u7528\u65e5\u5fd7\u8bb0\u5f55\u3001\u76d1\u63a7\u548c\u8b66\u62a5<\/strong><\/span><\/div>\n

\u5982\u679c\u4f60\u5e94\u8be5\u4ece\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u5b66\u5230\u4e00\u4ef6\u4e8b\uff0c\u90a3\u5c31\u662f\u8fd9\u4e2a!\u65e5\u5fd7\u8bb0\u5f55\u3001\u76d1\u63a7\u548c\u8b66\u62a5\u662f API \u6700\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u6ca1\u6709\u4e4b\u4e00\u3002<\/p>\n

\u62e5\u6709\u65e5\u5fd7\u3001\u76d1\u63a7\u548c\u8b66\u62a5\u6709\u52a9\u4e8e\u5de5\u7a0b\u5e08\u5728\u53d1\u751f\u95ee\u9898\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u8bca\u65ad\u548c\u8865\u6551\u3002\u8bb8\u591aAPI(\u57fa\u4e8eExpress\/Node\u3001Java\u3001Go)\u90fd\u6709\u9884\u5b9a\u4e49\u7684\u63a5\u53e3\u6765\u8bc4\u4f30\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n

\r\n\/health \r\n \r\n\/metrics\r\n<\/pre>\n

\u5982\u679c\u6ca1\u6709\u542f\u7528\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5e76\u4e14\u5b58\u5728\u6f5c\u5728\u95ee\u9898\uff0c\u5c06\u65e0\u6cd5\u8ddf\u8e2a\u6765\u6e90\uff0c\u6216\u7279\u5b9a\u8bf7\u6c42\u4e2d\u51fa\u73b0\u95ee\u9898\u7684\u5730\u70b9\u3002\u5982\u679c\u6ca1\u6709\u542f\u7528\u76d1\u63a7\uff0c\u5c06\u65e0\u6cd5\u4ece\u5206\u6790\u89d2\u5ea6\u77e5\u9053\u4e00\u4e9b\u95ee\u9898\u6216\u9519\u8bef\u7684\u53d1\u751f\u9891\u7387\u3002\u8fd9\u5c06\u4e0d\u5229\u4e8e\u505a\u51fa\u5408\u7406\u7684\u89e3\u51b3\u65b9\u6848\u3002\u800c\u4e14\uff0c\u5982\u679c\u6ca1\u6709\u542f\u7528\u8b66\u62a5\uff0c\u5c06\u4e0d\u77e5\u9053\u662f\u5426\u6709\u95ee\u9898\uff0c\u76f4\u5230\u5ba2\u6237(\u6216\u66f4\u7cdf\u7cd5\u7684\u662f\u5ba2\u6237)\u62a5\u544a\u5b83\u3002\u8fd9\u5c31\u6bd4\u8f83\u4e25\u91cd\u4e86!<\/p>\n

7\u3001\u542f\u7528\u5206\u9875<\/strong><\/span><\/div>\n

\u6570\u636e\u91cf\u5927\u65f6\uff0c\u5206\u9875\u662f\u4e2a\u5f88\u597d\u7684\u7b56\u7565\uff0c\u4e0d\u8fc7\u5206\u9875\u4e5f\u4e0d\u662f\u94f6\u5f39\uff0c\u6570\u636e\u91cf\u5927\u65f6\u4f9d\u7136\u4f1a\u975e\u5e38\u6162\u3002\u4e00\u4e2a\u6709\u6548\u7684\u7b56\u7565\u662f\u6700\u591a\u663e\u793a\u524d 100 \u9875\uff0c\u51e0\u4e4e\u6ca1\u6709\u4eba\u4f1a\u7ffb\u5230 100 \u9875\u4e4b\u540e\u3002<\/p>\n

\u6700\u540e\u7684\u8bdd <\/strong><\/div>\n

\u524d\u540e\u7aef\u5206\u79bb\u5df2\u662f\u5e38\u6001\uff0c\u5bf9\u4e8e\u540e\u7aef\u5f00\u53d1\u6765\u8bf4\uff0c\u6700\u91cd\u8981\u7684\u5c31\u662f\u8bbe\u8ba1\u4e00\u4e2a\u5f3a\u5927\u7684 API\uff0c\u9488\u5bf9 APi \u7684\u6027\u80fd\u8fdb\u884c\u9002\u5f53\u4f18\u5316\u548c\u589e\u5f3a\uff0c\u5b83\u53ef\u4ee5\u975e\u5e38\u5f3a\u5927\uff0c\u4e3a\u4f01\u4e1a\u548c\u5ba2\u6237\u63d0\u4f9b\u51fa\u8272\u7684\u4f53\u9a8c\u3002\u4f5c\u4e3a\u8d1f\u8d23\u4efb\u7684\u5de5\u7a0b\u5e08\uff0c\u6211\u4eec\u6709\u8d23\u4efb\u51b3\u5b9a\u5982\u4f55\u4ee5\u9ad8\u6027\u80fd\u7684\u65b9\u5f0f\u6784\u5efa\u6211\u4eec\u7684 API\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u548c\u8d85\u8d8a\u6211\u4eec\u7684\u76ee\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6df1\u601d\u719f\u8651\u7684\u8bbe\u8ba1\u8fd8\u5fc5\u987b\u8003\u8651\u5230 API \u7684\u6027\u80fd\uff0c\u5982\u679c API \u4e0d\u80fd\u54cd\u5e94\u8d8a\u6765\u8d8a\u591a\u7684\u8bf7\u6c42\uff0c\u4e0d\u80fd\u6ee1\u8db3\u4e0d\u65ad\u53d8\u5316\u7684\u4e1a\u52a1\u9700\u6c42\uff0c […]<\/p>\n","protected":false},"author":1898,"featured_media":231595,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[783],"class_list":["post-231593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-api"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231593","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\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=231593"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231593\/revisions"}],"predecessor-version":[{"id":231596,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231593\/revisions\/231596"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231595"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=231593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=231593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=231593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}