{"id":2508,"date":"2018-02-16T10:26:48","date_gmt":"2018-02-16T02:26:48","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2508"},"modified":"2018-02-16T10:26:48","modified_gmt":"2018-02-16T02:26:48","slug":"openresty-%e7%bd%91%e9%a1%b5%e5%8a%a0%e9%80%9f%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2508","title":{"rendered":"Openresty \u7f51\u9875\u52a0\u901f\u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u4f7f\u7528\u591a\u7ea7\u7f13\u5b58\u6765\u51cf\u5c11\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u8fbe\u5230\u52a0\u5feb\u7f51\u9875\u7684\u901f\u5ea6\u3002\u4f46\u662f\u968f\u7740\u7528\u6237\u7684\u7ee7\u7eed\u4e0a\u6da8\uff0c\u7cfb\u7edf\u7684\u538b\u529b\u8d8a\u6765\u8d8a\u5927\u3002\u5355\u4e00\u7684\u7f13\u5b58\u6570\u636e\u51cf\u5c11\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u6548\u679c\u5c31\u4e0d\u662f\u7279\u522b\u7684\u660e\u663e\u4e86\u3002openresty&nbsp;\u80fd\u591f\u76f4\u63a5\u5728nginx\u5c42\u76f4\u63a5\u5bf9\u8bf7\u6c42\u5904\u7406\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u8bbf\u95eetomcat\uff0c\u4ece\u800c\u51cf\u8f7b\u670d\u52a1\u5668\u7684\u538b\u529b\u3002\u672c\u6587\u4ecb\u7ecd&nbsp;openresty \u7684\u76f8\u5173\u6559\u7a0b\u3002\n<\/p>\n<p>\n\t\u4e3a\u4e86\u5b66\u4e60&nbsp;openresty \u6211\u4eec\u51c6\u5907\u4e863\u53f0\u670d\u52a1\u5668\uff0c\u4fe1\u606f\u5982\u4e0b\uff1a\n<\/p>\n<ul>\n<li>\n\t\tA:(\u6df1\u5733\uff0cNginx\u73af\u5883\uff0c\u672c\u5730Redis\uff0ctomcat\u670d\u52a1\u5668)\n\t<\/li>\n<li>\n\t\tB:(\u5e7f\u5dde\uff0ctomcat\u670d\u52a1\u5668)\n\t<\/li>\n<li>\n\t\tC:(\u5317\u4eac\uff0ctomact\u670d\u52a1\u5668)\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e3a\u4e86\u51cf\u5c11\u540e\u7aef\u7684\u76f8\u5e94\u65f6\u95f4\uff0c\u4e4b\u524d\u4f7f\u7528\u7684\u662f\u5728\u5e94\u7528\u91cc\u96c6\u6210ehcache\u4f5c\u4e3a\u4e00\u7ea7\u7f13\u5b58\uff0credis\u4f5c\u4e3a\u4e8c\u7ea7\u7f13\u5b58\u3002\u8fd9\u79cd\u67b6\u6784\u5b58\u5728\u4e00\u79cd\u7279\u6b8a\u7684\u60c5\u51b5\uff1a\u5f53Nginx\u5c06\u9996\u9875\u7684\u8bf7\u6c42\u5206\u53d1\u7ed9\u5317\u4eac\u8282\u70b9\u7684\u65f6\u5019\uff0c\u54cd\u5e94\u5c06\u53d8\u5f97\u6781\u5176\u7f13\u6162\uff0c\u7528\u6237\u7684\u8bf7\u6c42\u9700\u8981\u4ece\u6df1\u5733\u5230\u5317\u4eac\uff0c\u518d\u4ece\u5317\u4eac\u56de\u5230\u6df1\u5733\uff0c\u5149\u662f\u5ef6\u65f6\u5c31\u8981\u8017\u8d3940ms(\u6700\u597d\u7684\u60c5\u51b5)\uff0c\u7531\u4e8e\u7f51\u901f\u662f1M\/s\uff0c\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u54cd\u5e94\u7528\u6237\u7684\u8bf7\u6c42\u4e5f\u5f97\u8017\u8d39\u51e0\u79d2\u3002\u6240\u4ee5\uff0c\u4e3a\u4e86\u51cf\u5c11\u8fd9\u79cd\u6781\u7aef\u60c5\u51b5\uff0c\u8bbe\u8ba1\u4e86\u8fd9\u6b3e\u67b6\u6784\u3002\n<\/p>\n<p>\n\t\u6b65\u9aa4\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u8bf7\u6c42\u5230\u8fbenginx\u540e\uff0copenresty\u901a\u8fc7lua\u8bfb\u53d6\u672c\u5730\u7f13\u5b58\uff0c\u5982\u679c\u4e0d\u547d\u4e2d\uff0c\u5219\u56de\u6e90\u5230tomcat\u96c6\u7fa4\u3002\n\t<\/li>\n<li>\n\t\ttomcat\u96c6\u7fa4\u9996\u5148\u4ece\u81ea\u5df1\u7684\u670d\u52a1\u5668\u4e2d\u8bfb\u53d6\u4e00\u7ea7\u7f13\u5b58Ehcache\uff0c\u5982\u679c\u6ca1\u6709\u547d\u4e2d\uff0c\u5219\u7ee7\u7eed\u56de\u6e90\u5230\u4e8c\u7ea7\u7f13\u5b58\u3002\n\t<\/li>\n<li>\n\t\t\u8bfb\u53d6\u4e8c\u7ea7\u7f13\u5b58Redis\uff0c\u5982\u679c\u4f9d\u65e7\u6ca1\u6709\u547d\u4e2d\uff0c\u5219\u56de\u6e90\u5230MySQL\u670d\u52a1\u5668\u3002\n\t<\/li>\n<\/ol>\n<h2>\n\tOpenresty<br \/>\n<\/h2>\n<p>\n\t\u5b89\u88c5\u8fc7\u7a0b\u53ef\u4ee5\u76f4\u63a5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1a<a href=\"http:\/\/openresty.org\/cn\/download.html\" name=\"Openresty\" target=\"_blank\" title=\"Openresty\">http:\/\/openresty.org\/cn\/download.html<\/a>\uff0c\u5b89\u88c5\u524d\u8fd8\u9700\u5b89\u88c5\u4ee5\u4e0b\u5f00\u53d1\u5e93\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\nyum install pcre-devel openssl-devel gcc curl<\/pre>\n<p>\n\t\u7136\u540e\u8fdb\u884c\u7f16\u8bd1\u5b89\u88c5\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\ntar -xzvf openresty-VERSION.tar.gz\r\ncd openresty-VERSION\/\r\n.\/configure\r\nmake\r\nsudo make install<\/pre>\n<h2>\n\tNginx \u76f8\u5173\u914d\u7f6e<br \/>\n<\/h2>\n<p>\n\tOpenresty\u81ea\u5e26\u4e86Nginx\u3002\u6240\u4ee5\uff0c\u53ea\u8981\u5b89\u88c5\u597d\u4e86Openresty\uff0c\u5373\u53ef\u76f4\u63a5\u4f7f\u7528nginx\u6765\u914d\u7f6e\u3002\u4ee5\u4e0b\u53ea\u662f\u90e8\u5206\uff0c\u9700\u8981\u5168\u90e8\u7684\u8bf7\u67e5\u770b&nbsp;<strong><a href=\"https:\/\/github.com\/Zephery\/micro-service\/blob\/master\/conf\/nginx\/mynginxconfig.ngx\" target=\"_blank\">mynginxconfig.ngx<\/a>\u3002<\/strong>\n<\/p>\n<pre class=\"brush:plain;\">\r\nhttp {\r\n    include       mime.types;\r\n    default_type  application\/octet-stream;\r\n    # \u9700\u8981\u6dfb\u52a0lua\u7684\u76f8\u5173\u5e93\r\n    lua_package_path &quot;\/opt\/openresty\/lualib\/?.lua;;&quot;;\r\n    lua_package_cpath &quot;\/opt\/openresty\/lualib\/?.so;;&quot;;\r\n    ...\r\n\t# \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    access_log  logs\/access.log  main;\r\n    sendfile        on;\r\n    keepalive_timeout  65;\r\n\t# \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    upstream backend {\r\n        #consistent_hash was not configured\r\n        hash $uri;\r\n        server 47.95.10.139:8080;\r\n        server 119.23.46.71:8080;\r\n        server 119.29.188.224:8080;\r\n    }\r\n    server {\r\n        listen       80;\r\n        server_name  www.wenzhihuai.com;\r\n        # \u7cbe\u786e\u5339\u914d\uff0c\u6253\u5f00\u9996\u9875\u7684\u65f6\u5019\u8fdb\u5165\r\n        location  = \/ {\r\n            default_type    text\/html;\r\n            root   html;\r\n            index  index.html index.htm;\r\n            ...\r\n            # \u5173\u95ed\u7f13\u5b58lua\u811a\u672c\uff0c\u8c03\u8bd5\u7684\u65f6\u5019\u4e13\u7528\r\n            lua_code_cache off;\r\n            content_by_lua_file \/opt\/lua\/hello.lua;\r\n            # \u6b64\u5904\u4e0d\u8981proxy_pass\u4e86\uff0c\u5426\u5219lua\u811a\u672c\u6ca1\u7528\r\n            # proxy_pass http:\/\/backend;\r\n\t\t\t# \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n        }\r\n        # \u5982\u679c\u4e0a\u9762\u7684\u4e0d\u7b26\u5408\uff0c\u5219\u5339\u914d\u4e0b\u9762\u7684\r\n        location \/ {\r\n            default_type    text\/html;\r\n            root   html;\r\n            index  index.html index.htm;\r\n            # \u5bf9\u8bf7\u6c42\u8fdb\u884c\u53cd\u5411\u4ee3\u7406\r\n            proxy_pass http:\/\/backend;\r\n        }\r\n    }\r\n    ...\r\n}<\/pre>\n<h2>\n\tlua \u811a\u672c<br \/>\n<\/h2>\n<p>\n\t\u811a\u672c\u8bb0\u5f97\u653e\u5728 \/opt\/lua\/hello.lua \u76ee\u5f55\u4e0b\uff0c\u5bf9\u5e94 nginx \u7684\u914d\u7f6e\uff0c\u540c\u65f6\u9700\u8981\u5f15\u5165 redis \u6a21\u5757\u3002\n<\/p>\n<pre class=\"brush:plain;\">\r\nlocal redis = require &quot;resty.redis&quot;\r\nlocal red = redis:new()\r\n-- \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\nlocal request_uri = ngx.var.request_uri\r\n-- \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\nif (request_uri == &quot;\/&quot; or request_uri == &quot;\/index.html&quot;) then\r\n    red:set_timeout(1000) -- 1 sec\r\n    red:connect(&quot;119.23.46.71&quot;, 6340)\r\n    local ok, err = red:auth(&quot;root&quot;)\r\n    if not ok then\r\n        ngx.say(&quot;failed to connect: &quot;, err)\r\n        return\r\n    end\r\n    --\u7f13\u5b58\u7684\u9996\u9875\u653e\u5728key\u4e3aindex\u91cc\r\n    local resp, errr = red:get(&quot;index&quot;)\r\n    if not resp then\r\n        return\r\n    end\r\n    if resp == ngx.null then\r\n        resp = &quot;&lt;h1&gt;hello world&lt;\/h1&gt;&quot;\r\n    end\r\n    --\u5982\u679c\u627e\u5230\uff0c\u5219\u8f93\u51fa\u5185\u5bb9\r\n    ngx.print(resp)\r\n    red:close()\r\n    return\r\nend\r\nlocal pagenum = ngx.req.get_uri_args()[\"pagenum\"]\r\n--\u56e0\u4e3a\u5728nginx\u4e2d\u8bbe\u7f6e\u4e86proxy_pass_request_headers off\uff0c\u5373\u4e0d\u8bb2\u8bf7\u6c42\u5934\u90e8\u4f20\u5230lua\uff0c\u6240\u4ee5\u5934\u90e8\u9700\u8981\u91cd\u65b0\u8bbe\u7f6e\r\nngx.req.set_header(&quot;Accept&quot;, &quot;text\/html,application\/xhtml+xml,application\/xml;&quot;)\r\n--\u8fd9\u91cc\u56de\u6e90\u5230tomcat\u7684\u65f6\u5019\uff0cAccept-Encoding\u9ed8\u8ba4\u4e3agzip\uff0c\u5373\u8fd4\u56de\u6765\u6570\u636e\u5df2\u7ecf\u662fgzip\u538b\u7f29\u8fc7\u4e86\u7684\uff0c\u8fd4\u56de\u5230\u7528\u6237\u7684\u65f6\u5019\u53c8\u88ab\u538b\u7f29\u4e86\u4e00\u6b21\uff0c\u4f1a\u9020\u6210\u4e00\u5806\u4e71\u7801\u3002\u6240\u4ee5\u5c06Accept-Encoding\u8bbe\u7f6e\u4e3a\u7a7a\u3002\r\nngx.req.set_header(&quot;Accept-Encoding&quot;, &quot;&quot;)\r\nlocal respp = ngx.location.capture(&quot;\/index.do&quot;, { method = ngx.HTTP_GET, args = { pagenum = pagenum } })\r\n--\u6253\u5370\r\nngx.print(respp.body)\r\nreturn<\/pre>\n<h2>\n\t\u66f4\u65b0\u9996\u9875\u5230 redis<br \/>\n<\/h2>\n<p>\n\t\u6bcf\u969420\u79d2\u76f4\u63a5\u8bbf\u95ee\u540e\u7aef\u8fdb\u884c\u9996\u9875\u7684\u6293\u53d6\uff0c\u7136\u540e\u5b58\u50a8\u5230redis\u91cc\u9762\uff0c\u7b80\u5355\u7c97\u66b4\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n@Controller\r\n@SuppressWarnings(&quot;unchecked&quot;)\r\npublic class TimeController {\r\n    \/\/logger\r\n    private static final Logger logger = LoggerFactory.getLogger(TimeController.class);\r\n    @Scheduled(cron = &quot;0\/20 * * * * ?&quot;)\r\n    public void refreshIndex() throws Exception {\r\n        String ip = IPUtils.getServerIp().replaceAll(&quot;\\n&quot;, &quot;&quot;);\r\n        if (REGULARIP.equals(ip)) {\r\n            String content = HttpHelper.getInstance().get(&quot;http:\/\/119.29.188.224:8080&quot;);\r\n            JedisUtil.getInstance().set(&quot;index&quot;, content);\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u4ee5\u4e0a\u5c31\u662f\u603b\u7ed3\u4e86\u6211\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5bf9&nbsp;Openresty \u7684\u4f7f\u7528\uff0c\u5982\u6709\u4e0d\u61c2\u6b22\u8fce\u7559\u8a00\uff0c\u6216\u52a0\u4f5c\u8005\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\/yeyucao\u3002<\/p>\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=2508\">Openresty \u7f51\u9875\u52a0\u901f\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u591a\u7ea7\u7f13\u5b58\u6765\u51cf\u5c11\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u8fbe\u5230\u52a0\u5feb\u7f51\u9875\u7684\u901f\u5ea6\u3002\u4f46\u662f\u968f\u7740\u7528\u6237\u7684\u7ee7\u7eed\u4e0a\u6da8\uff0c\u7cfb\u7edf\u7684\u538b\u529b\u8d8a\u6765\u8d8a\u5927\u3002\u5355\u4e00\u7684\u7f13\u5b58\u6570\u636e\u51cf [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[1152,1603,669,112,430],"class_list":["post-2508","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-nginx","tag-openresty","tag-redis","tag-tomcat","tag-430"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2508","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=2508"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":2509,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2508\/revisions\/2509"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}