如何大幅提升web前端性能之看tengine在大公司架構實踐

来源:http://www.cnblogs.com/huangxincheng/archive/2017/09/03/7469328.html
-Advertisement-
Play Games

在一個項目還是單體架構的時候,所有的js,css,image都會在一個web網站上,看起來並沒有什麼問題,比如下麵這樣: 但是當web網站流量起來的時候,這個單體架構必須要進行橫向擴展,而在原來的架構中靜態資源這羊毛是出在單體架構這頭羊身上,所以橫向多少 個單體,就有多少個靜態資源文件夾,比如下麵這 ...


 

  在一個項目還是單體架構的時候,所有的js,css,image都會在一個web網站上,看起來並沒有什麼問題,比如下麵這樣:

    但是當web網站流量起來的時候,這個單體架構必須要進行橫向擴展,而在原來的架構中靜態資源這羊毛是出在單體架構這頭羊身上,所以橫向多少

個單體,就有多少個靜態資源文件夾,比如下麵這樣的架構。

那這種架構有什麼問題呢? 總的來說會有如下二個問題:

 

1.   瀏覽器對單一功能變數名稱的請求有併發限制。

     在同一個功能變數名稱下,一般來說有js,css,img,media,html等等靜態資源,如果資源都掛在同一個功能變數名稱下,勢必會影響頁面的載入速度,而且單一功能變數名稱

下靜態資源還會帶上同功能變數名稱下的cookie等本不需要附加的信息。

 

2.  不方便管理和資源的浪費

     為什麼這麼說呢? 我們知道靜態資源一般來說都是很占資源空間的,尤其是用戶上傳的頭像,csv那更是占用web伺服器資源,為了應對突發情況,

一般會保持web代碼的3個版本的回滾策略,也就是說你需要在web1,web2,web3上同時進行3個靜態資源文件夾的copy,相比單獨用靜態資源服務

器統一管理的文件夾的情況對比,前者的磁碟資源的浪費可想而知。。。所以改正後的架構應該是這樣的。

 

有些同學可能會說,空口無憑,你得找點真實的案例給我看看,為了滿足你的胃口,下麵我就找下‘攜程’ 和 ‘淘寶’站點給你分享一下。

 

一:攜程首頁對這二個問題的解決

        啥也不想說,通過瀏覽器隨便抓取一個css文件給大家分享一下,詳細如下圖:

 

 

1. css的載入路徑

     從http://webresource.c-ctrip.com/ResCRMOnline/R5/css/index/private_index.A_v3.css?ts=20170828_pro 中可以看到這和www.ctrip.com

 功能變數名稱根本就不是一個功能變數名稱,除了有點像,不說話,甚至還有點想笑,所以這種方式的載入對頁面的快速呈現有很大的幫助。

 

2. 對R5的分析

     這個R5是什麼意思,就是有10個版本,R1-R10,每發佈一個新版本,R++,輪迴而已,所以你可以將R5 -> R6,那就是輪迴之前的css版本。

 

   攜程現在的靜態資源大部分也是使用到了tengine,這是一個淘寶開源的基於nginx的proxy伺服器,在nginx上面做了很多的優化,而且tengine很

大的一個亮點就是可以使用多文件合併載入,比如10個css文件,可以合併成一個css進行呈現,這就讓10個http請求變成了1個,同樣也是對頁面

快速呈現有非常大的幫助,很可惜在ctrip上面並沒有找到合併載入css,js的案例,或許還是歷史的原因吧,那隻能在taobao官網上找一下看看。

 

 

從上圖中可以清楚看到tengine的強大功能,將default-min.css 和 apply-min.css 進行了合併載入,很神奇吧,接下來分享一下tengine的安裝配置。

 

二:tengine安裝

1.  下載地址:

       可以去tengine官網(http://tengine.taobao.org/)上找到目前最新的 2.2.0的安裝包。

 

[root@localhost myapp]# wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
--2017-09-02 19:54:07--  http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
Resolving tengine.taobao.org (tengine.taobao.org)... 120.55.149.135
Connecting to tengine.taobao.org (tengine.taobao.org)|120.55.149.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2160648 (2.1M) [application/octet-stream]
Saving to: ‘tengine-2.2.0.tar.gz’

100%[===================================================================================================>] 2,160,648   2.11MB/s   in 1.0s   

2017-09-02 19:54:13 (2.11 MB/s) - ‘tengine-2.2.0.tar.gz’ saved [2160648/2160648]
[root@localhost myapp]# tar -xzvf tengine-2.2.0.tar.gz
[root@localhost myapp]# ls
tengine-2.2.0  tengine-2.2.0.tar.gz
[root@localhost myapp]# cd tengine-2.2.0
[root@localhost tengine-2.2.0]# ls
AUTHORS.te  CHANGES     CHANGES.ru  conf       contrib  html     man      packages  README.markdown  tests
auto        CHANGES.cn  CHANGES.te  configure  docs     LICENSE  modules  README    src              THANKS.te
[root@localhost tengine-2.2.0]# pwd
/root/myapp/tengine-2.2.0

 

2. 然後就是tengine的一些依賴包

[root@localhost myapp]# yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++

 

3. 常規的configure, make,make install

[root@localhost tengine-2.2.0]# ./configure --prefix=/usr/myapp/tengine
checking for OS
 + Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for compiler structure-packing pragma ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for sched_setaffinity() ... found
checking for crypt_r() ... found
checking for SO_REUSEPORT ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sysinfo() ... found
checking for getloadavg() ... found
checking for /proc/meminfo ... found
checking for sched_yield() ... found
checking for SO_SETFIB ... not found
checking for SO_ACCEPTFILTER ... not found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library
  + jemalloc library is disabled

  nginx path prefix: "/usr/myapp/tengine"
  nginx binary file: "/usr/myapp/tengine/sbin/nginx"
  nginx configuration prefix: "/usr/myapp/tengine/conf"
  nginx configuration file: "/usr/myapp/tengine/conf/nginx.conf"
  nginx pid file: "/usr/myapp/tengine/logs/nginx.pid"
  nginx error log file: "/usr/myapp/tengine/logs/error.log"
  nginx http access log file: "/usr/myapp/tengine/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx dso module path: "/usr/myapp/tengine/modules/"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@localhost tengine-2.2.0]# make && make install

 

4. 啟動tengine下的nginx進程,可以看到當我啟動之後,tengine的80埠已經打開了。

[root@localhost myapp]# ls
tengine  tengine-2.2.0  tengine-2.2.0.tar.gz
[root@localhost myapp]# cd tengine
[root@localhost tengine]# ls
conf  html  include  logs  modules  sbin
[root@localhost tengine]# cd conf
[root@localhost conf]# cd ../sbin
[root@localhost sbin]# ls
dso_tool  nginx
[root@localhost sbin]# ./nginx 
[root@localhost sbin]# netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
[root@localhost sbin]# 

 

三:css文件和合併載入的驗證

 

1. css文件驗證

         下麵在nginx.conf 中配置一下靜態資源的訪問路徑,所有靜態資源都是放在/usr目錄下。

        location ~ ^/(images|javascript|js|css|flash|media|static)/  {
          root        /usr;
        }

 

然後生成 /usr/css/top.css 文件,最終瀏覽可以發現配置已生效。

[root@localhost usr]# mkdir css
[root@localhost usr]# ls
bin  css  etc  games  include  lib  lib64  libexec  local  myapp  sbin  share  src  tmp
[root@localhost usr]# cd css
[root@localhost css]# rz

[root@localhost css]# ls
top.css

 

2. css 合併壓縮

    這個合併模式還需要在tengine下安裝一個ngx_http_concat_module 模塊,可以看下官網:http://tengine.taobao.org/document_cn/http_concat_cn.html。

我這裡採用靜態編譯,編譯之前先把之前的tengine進程給關閉掉(nginx -s stop)。

[root@localhost myapp]# cd tengine-2.2.0
[root@localhost tengine-2.2.0]# ls
AUTHORS.te  CHANGES     CHANGES.ru  conf       contrib  html     Makefile  modules  packages  README.markdown  tests
auto        CHANGES.cn  CHANGES.te  configure  docs     LICENSE  man       objs     README    src              THANKS.te
[root@localhost tengine-2.2.0]# ./configure --prefix=/usr/myapp/tengine --with-http_concat_module && make && make install

 

   接下來只要在nginx.conf 中配置concat on,最多合併20個文件,最後 nginx -s reload 重啟一下

        location ~ ^/(images|javascript|js|css|flash|media|static)/  {
          root        /usr;
          concat on;
          concat_max_files 20;    
          concat_types text/css application/x-javascript;
        }

 接下來再上傳一個tips.css到/usr/css目錄下,然後鍵入url:http://192.168.23.168/css/??top.css,tips.css ,,,終於大功告成啦。。。。

 

 

  好了,本篇就說到這裡,希望對你有幫助。。。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Python包含6種內建的序列:列表,元組,字元串,Unicode,buffer對象,xrange對象。 通用序列操作包括:索引,分片,加,乘,成員資格。 另外還有長度(len),最小值(min),最大值(mix)內建函數 列表 列表 基本的列表操作 列表方法 元組 元組 ...
  • 還看到有說這樣做的,不過使用第一次加進來了,clean一次後又不打包hbm.xml文件。 如果是 jar 包,在 Plugin 中配置: 如果是 war 包,在 Plugin 中配置: ...
  • 一:讀程式寫結果 二:根據題意寫出相應代碼 ...
  • Java程式員有許多應遵循的守則或最佳實踐方式。本文概述了每個開發者最應該遵循的10條守則或戒律,如果不遵循它們,將會導致災難性後果。 1. 為代碼添加註釋(Add comments to your code). – 每個人都知道這一點,但不是每個人都會這麼做。你有多少次“忘記”添加註釋了?確實,註 ...
  • 最近在做一個手機移動端的項目,Andrio手機和部分iphone手機上點擊事件都是好的,只有在老的型號上的iphone手機上點擊事件無效果。 後來在網上查了很多資料,發現有加樣式cursor:pointer的,有將click事件和touchstart事件並存的,結果嘗試了這兩個方法,都沒有效果。準備 ...
  • JAVA文件命名規範 1.類命名 抽象類以 Abstract 或者 Base 開頭。異常類以 Exception 作為尾碼。枚舉類以 Enum 作為尾碼。工具類以 Utils 作為尾碼(相應的包名最後以 .util 結尾)。常量類以 Consts 作為尾碼(相應的包名最後以 .constant 結尾 ...
  • 一 本章概覽 介紹Linux系統運維與架構設計的方方面面 二 Linux基礎入門 認識電腦核心硬體和伺服器 Linux發展歷史、系統組成、應用領域以及發行版 搭建運維環境:VMWareWorkStation、SecureCRT的使用 Linux系統的基本使用 Shell入門以及命令概述 三 Lin ...
  • 一直報這個錯誤。終於解決了。 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="ht ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...