{"id":4272,"date":"2019-05-24T16:22:06","date_gmt":"2019-05-24T08:22:06","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=4272"},"modified":"2019-05-30T14:03:11","modified_gmt":"2019-05-30T06:03:11","slug":"%e5%88%a9%e7%94%a8-openrestry-%e5%ae%9e%e7%8e%b0%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e3%80%81%e9%99%90%e6%b5%81%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4272","title":{"rendered":"\u5229\u7528 OpenResty \u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u3001\u9650\u6d41\u529f\u80fd"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">OpenResty \u5f88\u591a\u4eba\u53ef\u80fd\u90fd\u6ca1\u6709\u542c\u8bf4\u8fc7\uff0c\u4f46\u662f\u6700\u8fd1\u51e0\u5e74\u5b83\u53d1\u5c55\u5f88\u5feb\uff0c\u5f88\u591a\u5927\u5382\u90fd\u6709\u4f7f\u7528\uff0c\u5305\u62ec 360\u3001BAT\u3001\u4eac\u4e1c\u3001\u9524\u5b50\u7b49\u3002\u6240\u4ee5\uff0c\u4e0d\u5f97\u4e0d\u8bf4\u7684\u662f\uff0c\u4e00\u6ce2\u5b66\u4e60 OpenResty \u7684\u9ad8\u6f6e\u5373\u5c06\u6765\u4e34\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">OpenResty \u53ef\u4ee5\u901a\u8fc7 lua \u811a\u672c\u6269\u5c55 nginx \u529f\u80fd\uff0c\u5305\u62ec\u8ba9\u4f60\u611f\u89c9 nginx \u5b9e\u73b0\u7684\u4e0d\u80fd\u6ee1\u8db3\u4f60\u8981\u6c42\u7684\u529f\u80fd\uff0c\u4f60\u90fd\u53ef\u4ee5\u901a\u8fc7 OpenResty \u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx \u672c\u8eab\u662f\u7528 C \u6765\u7f16\u5199\u7684\uff0c\u4f46\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u7279\u6709\u7684\u529f\u80fd\uff0c\u4f60\u53bb\u91cd\u65b0\u5b66\u4e60\u4e00\u4e0b C \u53c8\u4e0d\u592a\u73b0\u5b9e\uff0c\u56e0\u6b64\u5f53\u4f60\u4f1a OpenResty \u7684\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u6d3e\u4e0a\u7528\u573a\u4e86\u3002lua \u7684\u5b66\u4e60\u6210\u672c\u76f8\u6bd4 C \u6765\u8bf4\u80fd\u4f4e\u5f88\u591a\uff0c\u800c\u4e14 lua \u5728 Redis \u4e2d\u90fd\u6709\u4f7f\u7528\u3002\u56e0\u6b64\u6211\u8ba4\u4e3a\u5b66\u4e60 OpenResty \u662f\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u6027\u4ef7\u6bd4\u975e\u5e38\u7684\u9ad8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4eca\u5929\u7ed9\u5927\u5bb6\u7b80\u5355\u7684\u4ecb\u7ecd\u4e24\u4e2a\u4f7f\u7528 OpenResty \u7684\u573a\u666f\uff1a\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u3001\u9650\u6d41\u3002\u5f53\u7136 OpenResty \u7684\u4f7f\u7528\u573a\u666f\u5b9e\u5728\u662f\u592a\u591a\uff0c\u5305\u62ec\u653b\u9632\u65b9\u9762\u7684\uff0c\u6bd4\u5982 cc \u653b\u51fb\u7b49\u3002\u4f46\u8fd9\u4e9b\u5185\u5bb9\u5efa\u8bae\u5927\u5bb6\u79c1\u4e0b\u91cc\uff0c\u611f\u5174\u8da3\u7684\u81ea\u5df1\u53bb\u5b66\u4e60\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">OpenResty \u5b89\u88c5\u6211\u5c31\u4e0d\u8bb2\u4e86\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a hello World \u7684 demo\uff0c\u8ba9\u6211\u4eec\u719f\u6089\u5b83\u662f\u600e\u4e48\u4f7f\u7528\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>worker_processes  1;\nerror_log logs\/error.log;\nevents {\n    worker_connections 1024;\n}\nhttp {\n    server {\n        listen 9000;\n        location \/ {\n            default_type text\/html;\n            content_by_lua '\n                ngx.say(\"&lt;p&gt;Hello, World!&lt;\/p&gt;\")\n            ';\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 nginx.conf \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u52a0\u5165 content_by_lua \u8fd9\u6bb5\u4ee3\u7801\u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u4eec\u6765\u770b\u901a\u8fc7\u4f7f\u7528 OpenResty \u914d\u7f6e\uff0c\u914d\u5408 lua \u811a\u672c\u6765\u5b9e\u73b0\u81ea\u5df1\u7684\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>upstream xttblog {\n    server 192.168.10.1:8080;\n    server 192.168.10.2:8080;\n    balancer_by_lua_file lua\/balancer.lua;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">balancer.lua \u91cc\u7684\u4ee3\u7801\u4f60\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u3002\u4e0b\u9762\u7ed9\u4f60\u4e00\u4e2a\u6211\u7684 demo \u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>local balancer = require \"ngx.balancer\"\nlocal upstream = require \"ngx.upstream\"\nlocal upstream_name = 'mysvr3'\nlocal srvs = upstream.get_servers(upstream_name)\nfunction is_down(server)\nlocal down = false\nlocal perrs = upstream.get_primary_peers(upstream_name)\nfor i = 1, #perrs do\nlocal peer = perrs[i]\nif server == peer.name and peer.down == true then\n    down = true\nend\nend\nreturn down\nend\nlocal remote_ip = ngx.var.remote_addr\nlocal hash = ngx.crc32_long(remote_ip);\nhash = (hash % 2) + 1\nlocal backend = srvs[hash].addr\nlocal index = string.find(backend, ':')\nlocal host = string.sub(backend, 1, index - 1)\nlocal port = string.sub(backend, index + 1)\nngx.log(ngx.DEBUG, \"current peer \", host, \":\", port)\nbalancer.set_current_peer(host, tonumber(port))\nif not ok then\n    ngx.log(ngx.ERR, \"failed to set the current peer: \", err)\nreturn ngx.exit(500)\nend<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u53c2\u8003\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=4012\">\u624b\u628a\u624b\u6559\u4f60\u5199\u51fa 6 \u79cd\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5<\/a>\u300b\u3002 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u518d\u6765\u770b\u4e00\u4e2a\u9650\u6d41 demo\uff0cnginx.conf \u4e2d\u914d\u7f6e\u4fe1\u606f\u5982\u4e0b\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>location \/xttblog\/limit {\n    access_by_lua_file lua\/limit_conn.lua;\n    default_type text\/plain;\n    content_by_lua_block {\n    ngx.print('www.xttblog.com')\n        ngx.sleep(0.01)\n    }\n    log_by_lua_file src\/log.lua;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">limit_conn.lua \u4e2d\u7684\u4ee3\u7801\u4e5f\u975e\u5e38\u7684\u7b80\u5355\uff0c\u7c98\u8d34\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>local limit_conn = require \"resty.limit.conn\"\n-- \u5bf9\u4e8e\u5185\u90e8\u91cd\u5b9a\u5411\u6216\u5b50\u8bf7\u6c42\uff0c\u4e0d\u8fdb\u884c\u9650\u5236\u3002\u56e0\u4e3a\u8fd9\u4e9b\u5e76\u4e0d\u662f\u771f\u6b63\u5bf9\u5916\u7684\u8bf7\u6c42\u3002\nif ngx.req.is_internal() then\n    return\nend\n-- \u9650\u5236\u4e00\u4e2a ip \u5ba2\u6237\u7aef\u6700\u5927 5 \u4e2a\u5e76\u53d1\u8bf7\u6c42\n-- burst \u8bbe\u7f6e\u4e3a 0\uff0c\u5982\u679c\u8d85\u8fc7\u6700\u5927\u7684\u5e76\u53d1\u8bf7\u6c42\u6570\uff0c\u5219\u76f4\u63a5\u8fd4\u56de503\uff0c\n-- \u5982\u679c\u6b64\u5904\u8981\u5141\u8bb8\u7a81\u589e\u7684\u5e76\u53d1\u6570\uff0c\u53ef\u4ee5\u4fee\u6539 burst \u7684\u503c\uff08\u6f0f\u6876\u7684\u6876\u5bb9\u91cf\uff09\n-- \u6700\u540e\u4e00\u4e2a\u53c2\u6570\u5176\u5b9e\u662f\u4f60\u8981\u9884\u4f30\u8fd9\u4e9b\u5e76\u53d1\uff08\u6216\u8005\u8bf4\u5355\u4e2a\u8bf7\u6c42\uff09\u8981\u5904\u7406\u591a\u4e45\uff0c\u4ee5\u4fbf\u4e8e\u5bf9\u6876\u91cc\u9762\u7684\u8bf7\u6c42\u5e94\u7528\u6f0f\u6876\u7b97\u6cd5\nlocal lim, err = limit_conn.new(\"my_limit_conn_store\", 5, 5, 0.1)              \nif not lim then\n    ngx.log(ngx.ERR, \"failed to instantiate a resty.limit.conn object: \", err)\n    return ngx.exit(500)\nend\nlocal key = ngx.var.binary_remote_addr\n-- commit \u4e3atrue \u4ee3\u8868\u8981\u66f4\u65b0shared dict\u4e2dkey\u7684\u503c\uff0c\n-- false \u4ee3\u8868\u53ea\u662f\u67e5\u770b\u5f53\u524d\u8bf7\u6c42\u8981\u5904\u7406\u7684\u5ef6\u65f6\u60c5\u51b5\u548c\u524d\u9762\u8fd8\u672a\u88ab\u5904\u7406\u7684\u8bf7\u6c42\u6570\nlocal delay, err = lim:incoming(key, true)\nif not delay then\n    if err == \"rejected\" then\n        return ngx.exit(503)\n    end\n    ngx.log(ngx.ERR, \"failed to limit req: \", err)\n    return ngx.exit(500)\nend\n-- \u5982\u679c\u8bf7\u6c42\u8fde\u63a5\u8ba1\u6570\u7b49\u4fe1\u606f\u88ab\u52a0\u5230shared dict\u4e2d\uff0c\u5219\u5728ctx\u4e2d\u8bb0\u5f55\u4e0b\uff0c\n-- \u56e0\u4e3a\u540e\u9762\u8981\u544a\u77e5\u8fde\u63a5\u65ad\u5f00\uff0c\u4ee5\u5904\u7406\u5176\u4ed6\u8fde\u63a5\nif lim:is_committed() then\n    local ctx = ngx.ctx\n    ctx.limit_conn = lim\n    ctx.limit_conn_key = key\n    ctx.limit_conn_delay = delay\nend\nlocal conn = err\n-- \u5176\u5b9e\u8fd9\u91cc\u7684 delay \u80af\u5b9a\u662f\u4e0a\u9762\u8bf4\u7684\u5e76\u53d1\u5904\u7406\u65f6\u95f4\u7684\u6574\u6570\u500d\uff0c\n-- \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bcf\u79d2\u5904\u7406100\u5e76\u53d1\uff0c\u6876\u5bb9\u91cf200\u4e2a\uff0c\u5f53\u65f6\u540c\u65f6\u6765500\u4e2a\u5e76\u53d1\uff0c\u5219200\u4e2a\u62d2\u6389\n-- 100\u4e2a\u5728\u88ab\u5904\u7406\uff0c\u7136\u540e200\u4e2a\u8fdb\u5165\u6876\u4e2d\u6682\u5b58\uff0c\u88ab\u6682\u5b58\u7684\u8fd9200\u4e2a\u8fde\u63a5\u4e2d\uff0c0-100\u4e2a\u8fde\u63a5\u5176\u5b9e\u5e94\u8be5\u5ef6\u540e0.5\u79d2\u5904\u7406\uff0c\n-- 101-200\u4e2a\u5219\u5e94\u8be5\u5ef6\u540e0.5*2=1\u79d2\u5904\u7406\uff080.5\u662f\u4e0a\u9762\u9884\u4f30\u7684\u5e76\u53d1\u5904\u7406\u65f6\u95f4\uff09\nif delay &gt;= 0.001 then\n    ngx.sleep(delay)\nend<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\u501f\u52a9 Lua \u8fd9\u79cd\u811a\u672c\u8bed\u8a00\uff0c\u7ed3\u5408 OpenRestry\uff0c\u60f3\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u529f\u80fd\u5c31\u663e\u5f97\u5f88\u7b80\u5355\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">OpenResty \u975e\u5e38\u7684\u5f3a\u5927\uff0c\u5b83\u8fd8\u80fd\u64cd\u4f5c MySQL\u3001PostgreSQL\u3001Memcached \u4ee5\u53ca Redis \u7b49\u540e\u7aef\u5e94\u7528\u3002\u5982\u679c\u4f60\u5bf9\u5b83\u611f\u5174\u8da3\uff0c\u4e0d\u59a8\u52a0\u6211\u5fae\u4fe1\u597d\u53cb xttblog\uff0c\u6211\u4eec\u4e00\u8d77\u7cbe\u8fdb\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u60f3\u8981\u4e0a\u9762\u4ee3\u7801\u7684\u7f51\u53cb\u4e5f\u53ef\u4ee5\u52a0\u6211\u597d\u53cb\uff0c\u6211\u514d\u8d39\u9001\u3002\u53e6\u5916\u5982\u679c\u60f3\u66f4\u6df1\u5165\u7684\u5b66\u4e60\uff0c\u53ef\u4ee5\u901a\u8fc7\u8d2d\u4e70 OpenResty \u7684\u5165\u95e8\u5b9e\u6218\u8bfe\u7a0b\uff0c\u8fd9\u95e8\u8bfe\u7a0b\u662f\u6781\u5ba2\u65f6\u95f4\u7684\u300aOpenResty\u4ece\u5165\u95e8\u5230\u5b9e\u6218\u300b\uff0c\u901a\u8fc7\u6211\u4e0b\u65b9\u7684\u6d77\u62a5\u4e0a\u7684\u4e8c\u7ef4\u7801\uff0c\u626b\u7801\u8d2d\u4e70\uff0c\u52a0\u6211\u597d\u53cb\uff0c\u6211\u8fd4\u73b0\u7ed9\u4f60 24 \u54e6\uff01<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g3b6qukriqj20u00nt77y.jpg\" alt=\"\" \/><figcaption>OpenResty\u4ece\u5165\u95e8\u5230\u5b9e\u6218<\/figcaption><\/figure><\/div>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4272\">\u5229\u7528 OpenResty \u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u3001\u9650\u6d41\u529f\u80fd<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>OpenResty \u5f88\u591a\u4eba\u53ef\u80fd\u90fd\u6ca1\u6709\u542c\u8bf4\u8fc7\uff0c\u4f46\u662f\u6700\u8fd1\u51e0\u5e74\u5b83\u53d1\u5c55\u5f88\u5feb\uff0c\u5f88\u591a\u5927\u5382\u90fd\u6709\u4f7f\u7528\uff0c\u5305\u62ec 360\u3001BAT\u3001\u4eac [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[1972,1152,1603,1876,1877],"class_list":["post-4272","post","type-post","status-publish","format-standard","hentry","category-yyzt","tag-lua","tag-nginx","tag-openresty","tag-redis-hgetall-","tag-redis-hgetall"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4272"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4272\/revisions"}],"predecessor-version":[{"id":4280,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4272\/revisions\/4280"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}