1. 程式人生 > >01 . OpenResty簡介部署,優缺點,壓測,適用場景及用Lua實現服務灰度釋出

01 . OpenResty簡介部署,優缺點,壓測,適用場景及用Lua實現服務灰度釋出

#### 簡介 > OpenResty® 是一個基於 [Nginx](http://openresty.org/cn/nginx.html) 與 Lua 的高效能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模組以及大多數的依賴項。用於方便地搭建能夠處理超高併發、擴充套件性極高的動態 Web 應用、Web 服務和動態閘道器。 > > OpenResty® 通過匯聚各種設計精良的 [Nginx](http://openresty.org/cn/nginx.html) 模組(主要由 OpenResty 團隊自主開發),從而將 [Nginx](http://openresty.org/cn/nginx.html) 有效地變成一個強大的通用 Web 應用平臺。這樣,Web 開發人員和系統工程師可以使用 Lua 指令碼語言調動 [Nginx](http://openresty.org/cn/nginx.html) 支援的各種 C 以及 Lua 模組,快速構造出足以勝任 10K 乃至 1000K 以上單機併發連線的高效能 Web 應用系統。 > > OpenResty® 的目標是讓你的Web服務直接跑在 [Nginx](http://openresty.org/cn/nginx.html) 服務內部,充分利用 [Nginx](http://openresty.org/cn/nginx.html) 的非阻塞 I/O 模型,不僅僅對 HTTP 客戶端請求,甚至於對遠端後端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高效能響應。 #### 下載部署 ```python wget https://openresty.org/download/openresty-1.13.6.2.tar.gz cd openresty-1.13.6.2 ./configure make && make install ``` #### 目錄結構 ```python [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是預設是沒有在編譯版本中的 ``` #### 新增Lua程式碼啟動 ##### 新增lua注意 ```go /* 在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響應返回給使用者 */ ``` ##### 修改配置檔案 ```nginx [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/nginx ``` ##### 訪問 ![](https://img2020.cnblogs.com/blog/1871335/202010/1871335-20201009225456787-1252096603.png) #### Linux下ab效能測試 ##### 下載配置ab ```go [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/ ``` ##### 1W併發壓力測試 ```shell [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 3566 # 所有服務請求的百分比佔用時間,這裡50%的請求用時512ms, 一般看90%的部分 Percentage of the requests served within a certain time (ms) 50% 512 66% 541 75% 545 80% 547 90% 556 95% 1463 98% 1524 99% 1555 100% 3566 (longest request) ``` ##### 1.5W併發壓力測試 ```shell [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) 50% 808 66% 818 75% 832 80% 856 90% 888 95% 1750 98% 1812 99% 1832 100% 4077 (longest request) ``` ##### 2W併發壓力測試 ```shell [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) 50% 984 66% 1005 75% 1010 80% 1019 90% 1949 95% 1997 98% 2131 99% 2421 100% 4362 (longest request) ``` #### 通過lua實現灰度釋出 `nginx.conf` ```nginx [root@server conf]# cat nginx.conf user root; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream up1{ server 127.0.0.1:8081; } upstream up2{ server 127.0.0.1:8082; } upstream all{ ip_hash; serever 127.0.0.1:8081; serever 127.0.0.1:8082; } server { listen 80; server_name localhost; # 請求交給lua指令碼處理 location / { lua_code_cache off; content_by_lua_file /usr/local/nginx/lua/proxy.lua; } # location @ 用於nginx內部跳轉 location @up1 { proxy_pass http://up1; } location @up2 { proxy_pass http://up2; } location @all { proxy_pass http://all; } } } ``` `proxy.lua` ```lua ngx.header.content_type="text/html;charset=utf8" redis = require('resty.redis') redis = redis.new() redis:set_timeout(1000) ok,err = redis:connect('127.0.0.1', 6379) if not ok then ngx.say('connect to redis failed ! reason: ' .. err) end -- 從redis中檢查是否存在即將更新的upstream主機 check_up1 = redis:get('update1') --(up1) check_up2 = redis:get('update2') --(up2) redis:close() -- 注意返回的資料型別來判斷 if check_up1 == "1" then ngx.exec("@up2") elseif check_up2 == "1" then ngx.exec("@up1") else ngx.exec("@all") end -- 原理就是利用redis中設定指定key,比如要更新主機1,則redis中新增key=update1,value=1,當瀏覽器請求進入nginx的content階段後執行lua指令碼, -- 指令碼中檢查redis中是否存在要更新的主機,如果發現某個主機要更新則通過Nginx API for Lua中的ngx.exec介面內部跳轉到另一臺主機。 -- 如果兩個都不更新,則根據nginx自己的方式(預設輪詢)分發請求。 -- nginx裡使用了ip_hash保持會話,主要是為了使用者的請求在後端記錄的日誌中保持完整。 ``` #### 優點 ##### 高併發 > 根據以上的測試來看,**一臺1核700兆的linux虛擬機器即可跑出2W的併發**,足以說明OpenResty處理高併發的能力,由於ab工具的限制,只能測試上限2W的併發,因此,這裡也只做到了2W的測試。 ##### 熱更新 > Lua屬於指令碼語言,編譯之後即可執行;Nginx具備優雅重啟的功能;因此,線上業務如果出現問題需要及時修復的,更新修改Lua指令碼之後,重啟Nginx即可完成更新發布;如果沒有配置Lua快取功能的話,只要更新Lua指令碼即可更新修復線上問題,不重啟Nginx也可以。 #### 缺點 ##### 不適合處理複雜的業務邏輯 > 由於功能性的東西主要使用Lua進行開發,受Lua語言特性的影響,不太適合做業務比較複雜的功能。 ##### 網際網路上資料少 > 網際網路上可以找的資料比較少;不過,本身Lua的學習過程不是特別的複雜,所以,這基本不會成為你學習它的障礙 #### 適用場景 ##### 高仿問下應用及官網主頁 > 如商城,諮詢類的應用首頁,會存在大量的請求,由於涉及到的內容比較多;所以可是私用預載入的形式,將主頁的資料放置在redis中;使用OpenResty+redis實現首頁,官網主頁的高併發載入. ##### 商城類的秒殺功能 > 秒殺功能會存在短時間的請求洪峰,如果處理不當可能會造成down機的風險,可以結合OpenResty+redis實現秒殺的功能. ##### ip限流 > 網際網路系統可能存在非法使用者惡意暴力請求,導致正常的使用者無法使用,可以通過OpenResty+redis實現ip的白名單機制,去攔截非法的使用者ip ##### APP灰度升級釋出 > 可以根據系統的資料及條件實現APP的灰度升