欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

FastCGI中怎么利用Cache實(shí)現(xiàn)服務(wù)降級(jí)

本篇文章給大家分享的是有關(guān)FastCGI 中怎么利用Cache實(shí)現(xiàn)服務(wù)降級(jí),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

本篇文章給大家分享的是有關(guān)FastCGI 中怎么利用Cache實(shí)現(xiàn)服務(wù)降級(jí),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)建站專注于茶陵網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供茶陵?duì)I銷型網(wǎng)站建設(shè),茶陵網(wǎng)站制作、茶陵網(wǎng)頁(yè)設(shè)計(jì)、茶陵網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造茶陵網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供茶陵網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

架構(gòu)圖如下:

Degradation

實(shí)現(xiàn)的關(guān)鍵點(diǎn)在于通過error_page處理異常,并且完成服務(wù)降級(jí):

limit_conn_zone $server_name zone=perserver:1m; error_page 500 502 503 504 = @degradation; fastcgi_cache_path /tmp        levels=1:2        keys_zone=degradation:100m                    inactive=10d                    max_size=10g; upstream php {     server 127.0.0.1:9000;     server 127.0.0.1:9001; } server {     listen 80;     limit_conn perserver 1000;     server_name *.xip.io;     root /usr/local/www;     index index.html index.htm index.php;     location / {         try_files $uri $uri/ /index.php$is_args$args;     }     location ~ \.php$ {         set $cache_key $request_method://$host$request_uri;         set $cache_bypass "1";         if ($arg_degradation = "on") {             set $cache_bypass "0";         }         try_files $uri =404;         include fastcgi.conf;         fastcgi_pass php;         fastcgi_intercept_errors on;         fastcgi_next_upstream error timeout;         fastcgi_cache degradation;         fastcgi_cache_lock on;         fastcgi_cache_lock_timeout 1s;         fastcgi_cache_valid 200 301 302 10h;         fastcgi_cache_min_uses 10;         fastcgi_cache_use_stale error                                 timeout                                 invalid_header                                 updating                                 http_500                                 http_503;         fastcgi_cache_key $cache_key;         fastcgi_cache_bypass $cache_bypass;         add_header X-Cache-Status $upstream_cache_status;         add_header X-Response-Time $upstream_response_time;     }     location @degradation {         rewrite . $request_uri?degradation=on last;     } }

插播一個(gè)小技巧:設(shè)置域名時(shí)用到了xip.io,有了它就不用設(shè)置hosts了,方便調(diào)試。

代碼里用到的都是Nginx缺省包含的功能,我們可以看作是一個(gè)通用版,不過對(duì)照我們架構(gòu)圖中的目標(biāo)就會(huì)發(fā)現(xiàn):它沒有實(shí)現(xiàn)全局激活緩存的功能。如何實(shí)現(xiàn)呢?最簡(jiǎn)單的方法就是通過單位時(shí)間內(nèi)出錯(cuò)次數(shù)的多少來判斷系統(tǒng)健康以否,設(shè)置相應(yīng)的閾值,一旦超過限制就全局激活緩存,通過Lua我們可以實(shí)現(xiàn)一個(gè)定制版:

lua_shared_dict fault 1m;  limit_conn_zone $server_name zone=perserver:1m;  error_page 500 502 503 504 = @degradation;  fastcgi_cache_path /tmp                    levels=1:2                    keys_zone=degradation:100m                    inactive=10d                    max_size=10g;  upstream php {     server 127.0.0.1:9000;     server 127.0.0.1:9001; } init_by_lua '     get_fault_key = function(timestamp)         if not timestamp then             timestamp = ngx.time()         end         return os.date("fault:minute:%M", timestamp)     end     get_fault_num = function(timestamp)         local fault = ngx.shared.fault         local key = get_fault_key(timestamp)         return tonumber(fault:get(key)) or 0     end     incr_fault_num = function(timestamp)         local fault = ngx.shared.fault         local key = get_fault_key(timestamp)         if not fault:incr(key, 1) then             fault:set(key, 1, 600)         end     end '; server {     listen 80;     limit_conn perserver 1000;     server_name *.xip.io;     root /usr/local/www;     index index.html index.htm index.php;     location / {         rewrite_by_lua '             if ngx.var.arg_degradation then                 return ngx.exit(ngx.OK)             end              local ok = true              for i = 0, 1 do                 local num = get_fault_num(ngx.time() - i * 60)                 if num > 1000 then                     ok = false                     break                 end             end            if not ok then                 local query = "degradation=on"                 if ngx.var.args then                     ngxngx.var.args = ngx.var.args .. "&" .. query                 else                     ngx.var.args = query                 end             end         ';         try_files $uri $uri/ /index.php$is_args$args;     }     location ~ \.php$ {         set $cache_key $request_method://$host$request_uri;          set $cache_bypass "1";         if ($arg_degradation = "on") {             set $cache_bypass "0";         }         try_files $uri =404;         include fastcgi.conf;         fastcgi_pass php;         fastcgi_intercept_errors on;         fastcgi_next_upstream error timeout;         fastcgi_cache degradation;         fastcgi_cache_lock on;         fastcgi_cache_lock_timeout 1s;         fastcgi_cache_valid 200 301 302 10h;         fastcgi_cache_min_uses 10;         fastcgi_cache_use_stale error                                 timeout                                 invalid_header                                 updating                                 http_500                                 http_503;         fastcgi_cache_key $cache_key;         fastcgi_cache_bypass $cache_bypass;         add_header X-Cache-Status $upstream_cache_status;         add_header X-Response-Time $upstream_response_time;     }     location @degradation {         content_by_lua '             if ngx.var.arg_degradation then                 return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)             end             local res = ngx.location.capture(                 ngx.var.request_uri, {args = "degradation=on"}             )             ngx.status = res.status             for name, value in pairs(res.header) do                 ngx.header[name] = value             end             ngx.print(res.body)             incr_fault_num()         ';     } }

說明:實(shí)際上真實(shí)案例中緩存鍵名的獲取邏輯有點(diǎn)復(fù)雜,鑒于篇幅所限一切從簡(jiǎn)。

當(dāng)系統(tǒng)正常時(shí),運(yùn)行于動(dòng)態(tài)模式,數(shù)據(jù)通過PHP-FPM渲染;當(dāng)系統(tǒng)異常時(shí),全局緩存被激活,運(yùn)行于靜態(tài)模式,數(shù)據(jù)通過緩存渲染。通過測(cè)試發(fā)現(xiàn),系統(tǒng)在從正常切換到異常時(shí),因?yàn)樯釛壛薖HP-FPM,所以RPS從一千躍升到一萬。這讓我想起兒時(shí)看圣斗士的情景:每當(dāng)不死鳥一輝被敵人擊倒后,他總能重新站起來,并爆發(fā)出更大的能量。

此外需要說明的是:在發(fā)生故障的時(shí)候,如果出現(xiàn)大量緩存過期的情況,那么由于涉及到緩存的重建,所以依然會(huì)和PHP-FPM發(fā)生交互行為,這可能會(huì)影響性能,此時(shí)沒有特別好的解決辦法,如果Nginx版本夠的話,可以考慮激活fastcgi_cache_revalidate,如此一來,PHP-FPM一旦判斷系統(tǒng)處于異常情況,那么可以直接返回304實(shí)現(xiàn)緩存續(xù)期。

網(wǎng)站名稱:FastCGI中怎么利用Cache實(shí)現(xiàn)服務(wù)降級(jí)
本文網(wǎng)址:http://aaarwkj.com/article36/ejpdpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)公司、域名注冊(cè)外貿(mào)建站、關(guān)鍵詞優(yōu)化、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)
漂亮人妻中文字幕av| 亚洲成人久久久久久久| 国产日产精品久久一区| 成熟人妻一区二区三区人妻| 成人黄色av网站在线观看 | 国产日韩欧美亚洲中文国| 91精品蜜臀国产综合久久久久久| 俄罗斯少妇毛茸茸的高潮| 日本一区二区三在线观看| 欧美国产一级二级三级| 人妻av天堂综合一区| 国产老熟女高潮精品视频网站免费| 日本一区二区高清在线观看| 亚洲一区二区三区女同| 精品人妻av区天天看片| 国产青草视频免观看视频| 国产三级视频在线2022| 亚洲欧美日韩老汉影院| 亚洲大乳大丰满中文字幕| 亚洲一级特黄大片在线观看| 欧洲一区二区三区黄色| 久久精品亚洲一区二区| 国产男女在线视频观看| 日韩亚洲av一区二区| 国产丰满熟女视频免费| 中文字幕精品人妻在线| 蜜臀av首页在线观看| 五月开心婷婷中文字幕| 小明久久国内精品自线| 日韩亚洲一区二区免费| 素人人妻一区二区三区| 91福利免费在线看| 日本一区二区三区日韩欧美| 欧美日韩亚洲中文字幕| 午夜视频在线观看91| 国产中文字幕一区久久| 日本成人一区二区三区在线| 亚洲av毛片免费在线| 欧美一区二区在线精品| 国产精品伦理一区二区三区| 欧美国产免费高清视频|