04OpenResty簡程式碼壞境村法解決,適用場景及用Lua實現服務灰度釋出
OpenResty® 是一個基於Nginx與 Lua 的高效能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模組以及大多數的依賴項。用於方便地搭建能夠處理超高併發、擴充套件性極高的動態 Web 應用、Web 服務和動態閘道器。
OpenResty® 通過匯聚各種設計精良的Nginx模組(主要由 OpenResty 團隊自主開發),從而將Nginx有效地變成一個強大的通用 Web 應用平臺。這樣,Web 開發人員和系統工程師可以使用 Lua 指令碼語言調動Nginx支援的各種 C 以及 Lua 模組,快速構造出足以勝任 10K 乃至 1000K 以上單機併發連線的高效能 Web 應用系統。
OpenResty® 的目標是讓你的Web服務直接跑在Nginx服務內部,充分利用Nginx的非阻塞 I/O 模型,不僅僅對 HTTP 客戶端請求,甚至於對遠端後端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高效能響應。
2|0下載部署
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz cd openresty-1.13.6.2 ./configure make && make install3|0目錄結構
[root@server openresty]# ls bin COPYRIGHT luajit lualib nginx pod resty.index site # 分析目錄結構 # 相比Nginx原始碼目錄相比少了很多東西,少了的東西在bundle目錄下。build是編譯後生成的目標中間檔案 # 在bundle目錄中有很多模組,最核心的是Nginx原始碼,nginx-相應的版本中,當前的openresty基於nginx-1.13.6.2這個版本進行二次開發。 /root/openresty-1.13.6.2/configure --help # 檢視configure指令碼幫助檔案 # 和nginx幫助檔案基本沒有太大的不同,只不過openresty集成了很多第三方模組 # 字首帶有without是預設是內建在編譯版本中的 # 字首帶有with是預設是沒有在編譯版本中的4|0新增Lua程式碼啟動
4|1新增lua注意
/* 在nginx.conf 中實際是可以直接新增Lua程式碼,但是不能把Lua的語法Lua的原始碼直接放在conf中,因為nginx的解析器它的配置語法是跟Lua程式碼是不相同的。 在openresty的nginx lua模組中,它提供了幾條指令,其中有一條指令是content_by_lua content_by_lua是在http請求處理的內容生成階段,我們用Lua程式碼來處理。 */ /* openresty的Lua模組中提供了一些API 如ngx.say,會去生成http響應,瀏覽器在發起http請求中,它會在User-Agent這樣的head中, 去添加當前瀏覽器的型別,我是xxx,我用了什麼樣的核心,用ngx.req.ge_headers把使用者請求中的頭部取出來,然後找出User-Agent,把User-Agent值通過這樣一種文字方式返回給瀏覽器中. 通過openresty的nginx lua模組,我們可以用它提供給我們的API完成很多功能,我們可以利用Lua本身的一些工具庫把Lua語言新增進來參加我們生成響應的這樣一個過程中。 直接使用openresty提供的API或者Lua程式碼生成響應,為瀏覽器客戶端提供服務。 我們可以使用Lua語言以及提供的相應的API庫直接去訪問Redis,Mysql,Tomcat等這樣的服務,然後把不同的響應通過程式邏輯組成相應的http響應返回給使用者 */4|2修改配置檔案
[root@server nginx]# cat /usr/local/openresty/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location /lua { default_type text/html; content_by_lua ' ngx.say("User-Agent: ", ngx.req.get_headers()["User-Agent"]) '; } location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } # 啟動 [root@server nginx]# cat /usr/local/openresty/nginx/sbin/nginx4|3訪問
5|0Linux下ab效能測試
5|1下載配置ab
[root@server openresty]# yum -y install httpd-tools [root@server openresty]# ab -V This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/5|21W併發壓力測試
[root@server openresty]# ab -c 10000 -n 100000 127.0.0.1/lua This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: openresty/1.13.6.2 # 伺服器資訊和版本 Server Hostname: 127.0.0.1 # 伺服器的域名 Server Port: 80 # 埠 Document Path: /lua # 訪問的路徑 Document Length: 28 bytes # 文件大小 Concurrency Level: 10000 # 併發請求數 Time taken for tests: 5.978 seconds # 整個測試持續的時間 Complete requests: 100000 # 完成的請求數 Failed requests: 196008 # 失敗的請求數 (Connect: 0, Receive: 0, Length: 98912, Exceptions: 97096) Write errors: 0 # 網路連線寫入錯誤數 Total transferred: 511104 bytes # 傳輸的總資料量 HTML transferred: 81312 bytes # 傳輸的HTML內容量 Requests per second: 16729.18 [#/sec] (mean) # 平均每秒請求數 Time per request: 597.758 [ms] (mean) # 所有使用者請求一次的平均時間 Time per request: 0.060 [ms] (mean, across all concurrent requests) # 單個使用者請求一次的時間 Transfer rate: 83.50 [Kbytes/sec] received # 傳輸速率 Connection Times (ms) min mean[+/-sd] median max Connect: 0 323 302.4 256 3359 Processing: 64 246 62.9 247 525 Waiting: 0 8 48.8 0 342 Total: 283 570 304.2 512 35665|31.5W併發壓力測試
[root@server openresty]# ab -c 15000 -n 100000 127.0.0.1/lua This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: openresty/1.13.6.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /lua Document Length: 28 bytes Concurrency Level: 15000 Time taken for tests: 6.128 seconds Complete requests: 100000 Failed requests: 201164 (Connect: 0, Receive: 0, Length: 102820, Exceptions: 98344) Write errors: 0 Total transferred: 291456 bytes HTML transferred: 46368 bytes Requests per second: 16317.62 [#/sec] (mean) Time per request: 919.252 [ms] (mean) Time per request: 0.061 [ms] (mean, across all concurrent requests) Transfer rate: 46.44 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 471 296.5 409 3498 Processing: 74 380 100.9 383 878 Waiting: 0 7 57.5 0 573 Total: 494 851 303.4 808 4077 Percentage of the requests served within a certain time (ms)5|42W併發壓力測試
[root@server openresty]# ab -c 20000 -n 100000 127.0.0.1/lua This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: openresty/1.13.6.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /lua Document Length: 28 bytes Concurrency Level: 20000 Time taken for tests: 6.101 seconds Complete requests: 100000 Failed requests: 194710 (Connect: 0, Receive: 0, Length: 97430, Exceptions: 97280) Write errors: 0 Total transferred: 478720 bytes HTML transferred: 76160 bytes Requests per second: 16389.97 [#/sec] (mean) Time per request: 1220.258 [ms] (mean) Time per request: 0.061 [ms] (mean, across all concurrent requests) Transfer rate: 76.62 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 625 432.2 507 3665 Processing: 107 474 149.2 441 1024 Waiting: 0 15 94.3 0 714 Total: 521 1099 443.9 984 4362 Percentage of the requests served within a certain time (ms)6|0通過lua實現灰度釋出
nginx.conf
proxy.lua
7|0優點
7|1高併發
根據以上的測試來看,一臺1核700兆的linux虛擬機器即可跑出2W的併發,足以說明OpenResty處理高併發的能力,由於ab工具的限制,只能測試上限2W的併發,因此,這裡也只做到了2W的測試。
7|2熱更新
Lua屬於指令碼語言,編譯之後即可執行;Nginx具備優雅重啟的功能;因此,線上業務如果出現問題需要及時修復的,更新修改Lua指令碼之後,重啟Nginx即可完成更新發布;如果沒有配置Lua快取功能的話,只要更新Lua指令碼即可更新修復線上問題,不重啟Nginx也可以。
8|0缺點
8|1不適合處理複雜的業務邏輯
由於功能性的東西主要使用Lua進行開發,受Lua語言特性的影響,不太適合做業務比較複雜的功能。
8|2網際網路上資料少
網際網路上可以找的資料比較少;不過,本身Lua的學習過程不是特別的複雜,所以,這基本不會成為你學習它的障礙
9|0適用場景
9|1高仿問下應用及官網主頁
如商城,諮詢類的應用首頁,會存在大量的請求,由於涉及到的內容比較多;所以可是私用預載入的形式,將主頁的資料放置在redis中;使用OpenResty+redis實現首頁,官網主頁的高併發載入.