- 分布式系統架構:技術棧詳解與快速進階
- 張程
- 302字
- 2020-08-13 13:45:39
5.5 Varnish緩存
5.5.1 Varnish緩存狀態
通過瀏覽器訪問對應的網頁可查看Varnish緩存的狀態。如果Varnish緩存成功,第二次打開網頁的速度會明顯比第一次快,但是這種方式并不能充分說明緩存狀態。下面以命令行的方式,通過查看網頁頭部來查看命中情況,如代碼清單5-10所示。
代碼清單5-10 查看Varnish緩存狀態
[root@varnish ~]# curl -I http:// 127.0.0.1:2222/zachary/demo/showtime HTTP/1.1 200 OK Server: Tomcat/8.0 Last-Modified: Sat, 10 Jul 2019 11:25:15 GMT ETag: "5e850b-616d-48b06c6031cc0" Content-Type: application/json Content-Length: 24941 Date: Fri, 09 Jul 2019 08:29:16 GMT X-Varnish: 1364285597 Age: 0 Via: 1.1 varnish Connection: keep-alive X-Cache: MISS from 127.0.0.1
在代碼清單5-10中,X-Cache:MISS表示此次訪問未從緩存中讀取,當我們第二次訪問后,查看Varnish緩存狀態,如代碼清單5-11所示。
代碼清單5-11 查看Varnish緩存狀態
[root@varnish ~]# curl -I http:// 127.0.0.1:2222/zachary/demo/showtime HTTP/1.1 200 OK Server: Tomcat/8.0 Last-Modified: Sat, 10 Jul 2019 11:25:15 GMT ETag: "5e850b-616d-48b06c6031cc0" Content-Type: application/json Content-Length: 24941 Date: Fri, 09 Jul 2019 08:29:16 GMT X-Varnish: 1364285597 Age: 0 Via: 1.1 varnish Connection: keep-alive X-Cache: HIT from 127.0.0.1
在代碼清單5-11中,X-Cache:HIT表示此次訪問從緩存中讀取。通過查看請求的頭部和響應能看出數據是否已經被Varnish緩存,但只有通過緩存命中率的高低才能說明Varnish運行的效果。較高的緩存命中率說明Varnish運行良好,反之,說明Varnish的配置策略存在問題,需要調整。因此,從整體的命中率上可以直接反饋出Varnish的效果。Varnish提供了varnishstat命令,可以監控命中的過程。下面介紹varnishstat命令使用方法,如代碼清單5-12所示。
代碼清單5-12 varnishstat命令使用
[root@varnish ~]#/usr/local/varnish/bin/varnishstat -n /cache Hitrate ratio: 10 90 120 Hitrate avg: 0.9999 0.9964 0.9964 19960 98.92 1229.70 Client connections accepted 225820 660.84 8701.07 Client requests received 336802 530.78 6891.20 Cache hits 68 0.00 691.34 Cache misses 5688 33.96 220.37 Backend conn. success 6336 1.00 191.52 Backend conn. reuses 2642 33.96 47.14 Backend conn. was closed 8978 29.96 29.67 Backend conn. recycles 6389 1.00 70.79 Fetch with Length 2630 32.96 69.08 Fetch chunked 444 . . N struct sess_mem 23 . . N struct sess 64 . . N struct object 78 . . N struct objectcore 78 . . N struct objecthead 132 . . N struct smf 2 . . N small free smf 3 . . N large free smf 6 . . N struct vbe_conn 14 . . N worker threads 68 1.00 0.34 N worker threads created 0 0.00 0.00 N queued work requests 1201 11.99 5.98 N overflowed work requests 1 . . N backends 4 . . N expired objects 3701 . . N LRU moved objects 118109 942.85 587.61 Objects sent with write 9985 71.91 49.68 Total Sessions 121820 953.84 606.07 Total Requests
其中的參數詳解如下。
- Client connections accepted:表示客戶端向反向代理服務器成功發送HTTP請求的總數量。
- Client requests received:表示到現在為止,瀏覽器向反向代理服務器發送HTTP請求的累計次數。由于可能會使用長連接,因此這個值一般會大于Client connections accepted的值。
- Cache hits:表示反向代理服務器在緩存區中查找并且命中緩存的次數。
- Cache misses:表示直接訪問后端主機的請求數量,也就是非命中數。
- N struct object:表示當前被緩存的數量。
- N expired objects:表示過期的緩存內容數量。
- N LRU moved objects:表示被淘汰的緩存內容數量。
5.5.2 Varnish緩存管理
Varnish緩存管理的主要工作是迅速有效地控制和清除指定的緩存內容。Varnish清除緩存的操作相對比較復雜,可以通過Varnish的管理端口發送purge指令來清除不需要的緩存。清除緩存的命令如代碼清單5-13所示。
代碼清單5-13 Varnish清除緩存命令
#清除指定URL的緩存 /usr/local/varnish/bin/varnishadm -T 192.168.10.101:2000 purge.url /xxx相對路徑 #例如清除緩存(http:// 192.168.10.101:2222/zachary/demo/showtime) /usr/local/varnish/bin/varnishadm -T 192.168.10.101:2000 purge.url /zachary/demo/showtime #如/demo/下面有很多訪問連接,需要批量清除 /usr/local/varnish/bin/varnishadm -T 192.168.10.101:2000 purge.url^/zachary/demo/*$ #清除所有的緩存 /usr/local/varnish/bin/varnishadm -T 192.168.10.101:2000 purge.url^.*$ #查看最近清除的緩存 /usr/local/varnish/bin/varnishadm -T 192.168.10.101:2000 purge.list
有時不想通過Linux命令清除緩存,此時可以通過telnet到管理端口來清除緩存,如代碼清單5-14所示。
代碼清單5-14 后臺管理清除緩存
[root@varnish ~]#telnet 192.168.10.101 2000 Trying 192.168.10.101... Connected to localhost.localdomain (192.168.10.101). Escape character is '^]'. 200 154 ----------------------------- Varnish HTTP accelerator CLI. ----------------------------- Type 'help' for command list. Type 'quit' to close CLI session. purge.url /zachary/demo/query #清除這個連接的緩存數據 200 0 purge.url ^/nodes/data/*$ #清除/nodes/data/目錄下緩存數據 200 0