{"id":161745,"date":"2024-11-04T08:59:24","date_gmt":"2024-11-04T00:59:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/161745\/"},"modified":"2024-11-04T08:59:25","modified_gmt":"2024-11-04T00:59:25","slug":"%e5%9f%ba%e6%96%bc-zuul-redis-%e7%9a%84-api-%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%e6%8a%80%e8%a1%93%e7%a0%94%e7%a9%b6%ef%bc%88zuul-redis-%e9%99%90%e6%b5%81%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/161745\/","title":{"rendered":"\u57fa\u65bc Zuul Redis \u7684 API \u6d41\u91cf\u9650\u5236\u6280\u8853\u7814\u7a76\uff08zuul redis \u9650\u6d41\uff09"},"content":{"rendered":"<h1 id=\"%e5%9f%ba%e6%96%bc-zuul-redis-%e7%9a%84-api-%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%e6%8a%80%e8%a1%93%e7%a0%94%e7%a9%b6%ef%bc%88zuul-redis-%e9%99%90%e6%b5%81%ef%bc%89-PoNONooHdy\">\u57fa\u65bc Zuul Redis \u7684 API \u6d41\u91cf\u9650\u5236\u6280\u8853\u7814\u7a76\uff08zuul redis \u9650\u6d41\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0cAPI \u7684\u4f7f\u7528\u8d8a\u4f86\u8d8a\u666e\u904d\uff0c\u96a8\u4e4b\u800c\u4f86\u7684\u6d41\u91cf\u7ba1\u7406\u554f\u984c\u4e5f\u65e5\u76ca\u7a81\u51fa\u3002\u70ba\u4e86\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\uff0c\u907f\u514d\u56e0\u6d41\u91cf\u904e\u5927\u800c\u5c0e\u81f4\u7684\u7cfb\u7d71\u5d29\u6f70\uff0c\u6d41\u91cf\u9650\u5236\u6280\u8853\u61c9\u904b\u800c\u751f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u57fa\u65bc Zuul \u548c Redis \u7684 API \u6d41\u91cf\u9650\u5236\u6280\u8853\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u5be6\u73fe\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-api-%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%ef%bc%9f-PoNONooHdy\">\u4ec0\u9ebc\u662f API \u6d41\u91cf\u9650\u5236\uff1f<\/h2>\n<p>API \u6d41\u91cf\u9650\u5236\u662f\u6307\u5c0d API \u8acb\u6c42\u7684\u6578\u91cf\u9032\u884c\u63a7\u5236\uff0c\u4ee5\u9632\u6b62\u904e\u591a\u7684\u8acb\u6c42\u5f71\u97ff\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002\u6d41\u91cf\u9650\u5236\u53ef\u4ee5\u6839\u64da\u6642\u9593\u7a97\u53e3\uff08\u5982\u6bcf\u79d2\u3001\u6bcf\u5206\u9418\u6216\u6bcf\u5c0f\u6642\uff09\u4f86\u8a2d\u7f6e\uff0c\u78ba\u4fdd\u6bcf\u500b\u7528\u6236\u6216\u61c9\u7528\u7a0b\u5e8f\u5728\u7279\u5b9a\u6642\u9593\u5167\u4e0d\u6703\u8d85\u904e\u9810\u5b9a\u7684\u8acb\u6c42\u6578\u91cf\u3002<\/p>\n<h2 id=\"zuul-%e7%9a%84%e8%a7%92%e8%89%b2-PoNONooHdy\">Zuul \u7684\u89d2\u8272<\/h2>\n<p>Zuul \u662f\u4e00\u500b\u7531 Netflix \u958b\u767c\u7684 API \u9598\u9053\u5668\uff0c\u4e3b\u8981\u7528\u65bc\u8def\u7531\u8acb\u6c42\u3001\u8ca0\u8f09\u5747\u8861\u548c\u5b89\u5168\u6027\u7b49\u529f\u80fd\u3002\u5b83\u80fd\u5920\u4f5c\u70ba\u5fae\u670d\u52d9\u67b6\u69cb\u4e2d\u7684\u5165\u53e3\uff0c\u4e26\u63d0\u4f9b\u7d71\u4e00\u7684 API \u63a5\u53e3\u3002Zuul \u7684\u4e00\u500b\u91cd\u8981\u7279\u6027\u662f\u5176\u53ef\u64f4\u5c55\u6027\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u901a\u904e\u81ea\u5b9a\u7fa9\u904e\u6ffe\u5668\u4f86\u5be6\u73fe\u5404\u7a2e\u529f\u80fd\uff0c\u5305\u62ec\u6d41\u91cf\u9650\u5236\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e5%84%aa%e5%8b%a2-PoNONooHdy\">Redis \u7684\u512a\u52e2<\/h2>\n<p>Redis \u662f\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u8b80\u5beb\u901f\u5ea6\u548c\u8c50\u5bcc\u7684\u6578\u64da\u7d50\u69cb\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u5728\u6d41\u91cf\u9650\u5236\u7684\u5834\u666f\u4e2d\uff0cRedis \u53ef\u4ee5\u7528\u4f86\u5b58\u5132\u7528\u6236\u7684\u8acb\u6c42\u8a08\u6578\uff0c\u4e26\u5728\u77ed\u6642\u9593\u5167\u5feb\u901f\u6aa2\u67e5\u548c\u66f4\u65b0\u9019\u4e9b\u8a08\u6578\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%96%bc-zuul-%e5%92%8c-redis-%e7%9a%84%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%e5%af%a6%e7%8f%be-PoNONooHdy\">\u57fa\u65bc Zuul \u548c Redis \u7684\u6d41\u91cf\u9650\u5236\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u65bc Zuul \u548c Redis \u7684\u7c21\u55ae\u6d41\u91cf\u9650\u5236\u5be6\u73fe\u793a\u4f8b\uff1a<\/p>\n<pre><code>import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport redis.clients.jedis.Jedis;\n\n@Component\npublic class RateLimiter {\n    private static final int LIMIT = 100; \/\/ \u6bcf\u5206\u9418\u9650\u5236 100 \u6b21\u8acb\u6c42\n    private static final String RATE_LIMIT_KEY = \"rate_limit:\";\n\n    @Autowired\n    private Jedis jedis;\n\n    public boolean isAllowed(String userId) {\n        long currentTime = System.currentTimeMillis() \/ 1000;\n        String key = RATE_LIMIT_KEY + userId + \":\" + currentTime;\n\n        \/\/ \u589e\u52a0\u8acb\u6c42\u8a08\u6578\n        Long count = jedis.incr(key);\n        if (count == 1) {\n            \/\/ \u8a2d\u7f6e\u904e\u671f\u6642\u9593\n            jedis.expire(key, 60);\n        }\n        return count &lt;= LIMIT;\n    }\n}\n\n@RestController\npublic class ApiController {\n    @Autowired\n    private RateLimiter rateLimiter;\n\n    @RequestMapping(&quot;\/api\/resource&quot;)\n    public String getResource(String userId) {\n        if (!rateLimiter.isAllowed(userId)) {\n            return &quot;\u8acb\u6c42\u904e\u65bc\u983b\u7e41\uff0c\u8acb\u7a0d\u5f8c\u518d\u8a66\u3002&quot;;\n        }\n        return &quot;\u6210\u529f\u7372\u53d6\u8cc7\u6e90\uff01&quot;;\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u6d41\u91cf\u9650\u5236\u5668\uff0c\u4f7f\u7528 Redis \u4f86\u8ddf\u8e64\u6bcf\u500b\u7528\u6236\u7684\u8acb\u6c42\u8a08\u6578\u3002\u7576\u7528\u6236\u7684\u8acb\u6c42\u8d85\u904e\u9650\u5236\u6642\uff0c\u7cfb\u7d71\u5c07\u8fd4\u56de\u4e00\u500b\u932f\u8aa4\u6d88\u606f\u3002<\/p>\n<h2 id=\"%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%e7%9a%84%e5%a5%bd%e8%99%95-PoNONooHdy\">\u6d41\u91cf\u9650\u5236\u7684\u597d\u8655<\/h2>\n<ul>\n<li><strong>\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\uff1a<\/strong>\u901a\u904e\u9650\u5236\u8acb\u6c42\u6578\u91cf\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u670d\u52d9\u5668\u904e\u8f09\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u7cfb\u7d71\u7a69\u5b9a\u6027\uff1a<\/strong>\u6d41\u91cf\u9650\u5236\u53ef\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u4ecd\u7136\u80fd\u5920\u7a69\u5b9a\u904b\u884c\u3002<\/li>\n<li><strong>\u516c\u5e73\u4f7f\u7528\u8cc7\u6e90\uff1a<\/strong>\u901a\u904e\u9650\u5236\u6bcf\u500b\u7528\u6236\u7684\u8acb\u6c42\u6578\u91cf\uff0c\u53ef\u4ee5\u78ba\u4fdd\u6240\u6709\u7528\u6236\u90fd\u80fd\u516c\u5e73\u5730\u4f7f\u7528\u7cfb\u7d71\u8cc7\u6e90\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PoNONooHdy\">\u7e3d\u7d50<\/h2>\n<p>\u57fa\u65bc Zuul \u548c Redis \u7684 API \u6d41\u91cf\u9650\u5236\u6280\u8853\u662f\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u7ba1\u7406\u548c\u63a7\u5236 API \u8acb\u6c42\u6d41\u91cf\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u9019\u7a2e\u6280\u8853\u4e0d\u50c5\u80fd\u5920\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\uff0c\u9084\u80fd\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u7528\u6236\u9ad4\u9a57\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u57fa\u65bcZuul\u548cRedis\u7684API\u6d41\u91cf\u9650\u5236\u6280\u8853\uff0c\u63d0\u5347\u7cfb\u7d71\u7a69\u5b9a\u6027\u8207\u6027\u80fd\uff0c\u78ba\u4fdd\u9ad8\u6548\u80fd\u7684\u670d\u52d9\u4ea4\u4ed8\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-161745","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=161745"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161745\/revisions"}],"predecessor-version":[{"id":161746,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161745\/revisions\/161746"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=161745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=161745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=161745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}