1. 程式人生 > >web容器(01):Apache配置監控

web容器(01):Apache配置監控

圖解 dns 切換 技術 最好的 erl ret 錯誤日誌 ddd

響應時間長可能原因:

硬件、應用服務器、網絡、負載機、中間件線程池排隊、數據庫連接池排隊、sql語句、jvmGc、代碼邏輯

如何分析:

分析系統架構節點

開發在日誌中打印方法在調用時消耗的時間

web容器:Nginx apache tomcat resin weblogic websphere

apache:處理靜態資源性能好些,處理servlet這樣的動態請求性能會差些

tomcat:處理動態請求性能會好些

apache:

1、通渠配置文件httpd.conf

1)位置:

yum安裝:/etc/httpd/conf/

編譯安裝:/opt/lampp/etc/  

etc/extra目錄下有多個conf文件,在通渠配置文件中,如果要用到extra中的conf文件,需要在通渠配置文件中include對應的extra中的conf文件,或者將要使用的配置文件的內容拷到通渠配置文件中。

2)通渠配置文件相關配置解釋:

ServerRoot "/etc/httpd" → apache的啟動路徑

PidFile run/httpd.pid → apache啟動的時候會產生一個pid

Timeout 60 → 接收和發送的超時時間是60s

KeepAlive Off → 長連接(建立握手,通道一直在),off此處標識短連接,長連接的性能較好,但是弊端也是顯而易見的,線程和線程池之間的通道一直存在,建立連接的線程不用的時候連接還是存在,那麽其它的線程,無法和線程池進行連接,導致系統無響應。

在jmeter中默認使用use keepalive長連接,lr中默認使用短連接,使用長短連接對性能測試的tps影響可能會相差很大。使用長連接測試出來的tps可能會大很多。

MaxKeepAliveRequests 100 → 最大使用多少個長連接

KeepAliveTimeout 15 → 長連接最大的等待時間,如果這個時間超過配置的時間,這個連接不生效

# prefork MPM → 進程工作模式。多路處理模塊(MPM)實現了一個非線程型的、預派生的web服務器,它適合於沒有線程安全庫,需要避免線程兼容性問題的系統。它是要求將每個請求相互獨立的情況下最好的MPM,這樣若一個請求出現問題就不會影響到其他請求。

StartServers 8 → 初始化啟動進程數
MinSpareServers 5 → 最小服務器空閑進程數(預派生)
MaxSpareServers 20 → 最大服務器空閑進程數
ServerLimit 256 → apache同時處理服務進程數
MaxClients 256 → apache可以同時處理的請求數,其缺省值150是遠遠不夠的,如果請求總數已達到這個值(可通過ps -ef|grep http|wc -l來確認),那麽後面的請求就要排隊,直到某個已處理請求完畢。這就是系統資源還剩下很多而HTTP訪問卻很慢的主要原因。Apache默認的限制不能大於256。
MaxRequestsPerChild 4000 → 一個進程在其生存周期內,最多處理4000次請求,如果處理4000次請求後,該進程被銷毀,避免僵屍進程,防止意外的內存泄漏,在服務器負載下降的時候,自動減少進程數

# worker MPM → 混合的多進程多線程工作模式。使用了多進程,每個進程又有多個線程,以獲得基於進程的MPM的穩定性。每個進程可以擁有的線程數量是固定的。服務器會根據負載情況增加或減少進程數量。

StartServers 4 → 初始化啟動進程數
MaxClients 300 → 最大啟動的進程數
MinSpareThreads 25 → 最小空閑線程數
MaxSpareThreads 75 → 最大空閑線程數
ThreadsPerChild 25 → 每個進程下有25個線程
MaxRequestsPerChild 0 → 進程永遠存活

Listen 80 → 監聽的端口號

LoadModule → 加載動態庫 .so標識動態庫

Include conf.d/*.conf → 將conf.d/*.conf中的配置文件加載進來

DocumentRoot "/var/www/html" → apache的工作路徑,存放應用程序代碼

ErrorLog logs/error_log → 錯誤日誌路徑

LogLevel warn → 日誌級別,一般配置info級別

AddDefaultCharset UTF-8 → 默認字符集

# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html

配置監聽:

<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
# Allow from .example.com
</Location>

<Location /server-info>
SetHandler server-info
Order deny,allow
Deny from all
# Allow from .example.com
</Location>

修改好通渠配置文件後,重啟apache:

service httpd stop/service httpd start

2、Apache的監聽

1)監聽server-status

在通渠配置文件httpd.conf中配置監聽<Location /server-status>的Allow from .example.com註釋掉時,訪問http://192.168.20.129/server-status,頁面提示不允許訪問,如下所示:

技術分享

配置監聽<Location /server-status>的Allow from all,訪問http://192.168.20.129/server-status,頁面訪問如下圖所示:

技術分享

對照配置文件頁面內容釋義,畫圖解釋:技術分享

方便我們監控進程的狀態,進而分析系統的性能。

2)監聽server-info

在通渠配置文件中配置監聽<Location /server-info>的Allow from all,訪問http://192.168.20.129/server-info,頁面訪問如下圖所示:

技術分享

3、切換apache為線程工作模式

默認情況下,apache為進程工作模式,那麽如何將apache切換為線程工作模式呢?

1)進入/usr/sbin目錄

[[email protected] sbin]# cd /usr/sbin/
[[email protected] sbin]# ls http*
httpd httpd.event httpd.worker

2)重命名httpd文件

[[email protected] sbin]# mv httpd httpd_prefork
[[email protected] sbin]# mv httpd.worker httpd
[[email protected] sbin]# httpd -l → 查看apache的工作模式
Compiled in modules:
core.c
worker.c → 修改後,工作模式變為worker.c
http_core.c
mod_so.c

3)重啟apache

[[email protected] sbin]# service httpd stop
停止 httpd: [確定]
[[email protected] sbin]# service httpd start
正在啟動 httpd: [確定]

4)再次訪問http://192.168.20.129/server-status頁面

通渠配置文件中的配置如下:

# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

第一次訪問頁面:

1 requests currently being processed, 74 idle workers

第二次第三次... ...第N次訪問頁面:

1 requests currently being processed, 49 idle workers

根據通渠配置文件裏的配置,apache在啟動時,開啟了4個進程,每個進程裏有25個線程,那麽啟動後,總共是啟動了100個線程,又由於配置了MinSpareThreads和MaxSpareThreads,apache kill掉1個進程,那麽在頁面顯示中,就顯示了1個繁忙線程和74個空閑線程。

第N次訪問頁面後,最終穩定下來的是50個線程。

5)頁面釋義,類似進程工作模式

Apache Server Status for 192.168.20.129
1 requests currently being processed, 49 idle workers
_____________W___________.......................................
................................................................
_________________________.......................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................

Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process

PID Key:


2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: W , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ ,

6)進程和線程監控頁面的意義

舉例:

max:75線程

70左右的線程都處於工作狀態(發送、接收、寫日誌、解析、連接斷開再連接)

tps:3000  分布式負載1秒發送200個請求 wait1

web容器:75個線程 wait2

16顆cpu,同時處理16個請求 wait3

響應時間慢,看耗時地方,看線程池是否在排隊(線程池工作狀態,大部分都在工作)

最終發送到數據庫的請求越來越少

負載機越好,性能表現可能越好

需要檢測線程池請求是否存在排隊現象,如果存在,需要加大線程池

看線程池狀態耗時的地方

4、apache日誌

1)位置

[[email protected] httpd]# ls
conf conf.d logs modules run
[[email protected] httpd]# pwd
/etc/httpd
[[email protected] httpd]# cd logs/
[[email protected] logs]# ls
access_log access_log-20170702 error_log-20170618 error_log-20170702
access_log-20170618 error_log error_log-20170626

2)日誌

error_log → 錯誤日誌

access_log → 增長日誌

可以看到線上的功能訪問熱度,熱度:性能測試的功能點

每一秒功能最大的並發數,重點功能,用戶用得多的功能

5、apache的功能

和nginx一樣,既可以做web容器,也可以做負載均衡服務器

apache+tomcat做負載均衡,前端apache後端tomcat

web容器(01):Apache配置監控