Tomcat

一,調優配置

1,內存調優javascript

在bin/catalina.sh中(大概97行)加入以下配置:css

2,併發調優html

在conf/server.xml文件中加入:java

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
        URIEncoding="UTF-8"
        maxConnections="10000"
        maxThreads="2000"
        acceptCount="2000"
        minSpareThreads="100"
        compression="no"
        compressionMinSize="2048"
        compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
        enableLookups="false"
        disableUploadTimeout="true"
        connectionTimeout="20000"
        redirectPort="8443" 
/>

此處的protocol默認爲protocol="HTTP/1.1",即nio模式。linux

參數解釋:git

  • maxThreads 最大線程數,即同時處理任務的個數。maxThreads的設置既與應用的特色有關,也與服務器的CPU核心數量有關。經過前面介紹能夠知道,maxThreads數量應該遠大於CPU核心數量;並且CPU核心數越大,maxThreads應該越大;應用中CPU越不密集(IO越密集),maxThreads應該越大,以便可以充分利用CPU。固然,maxThreads的值並非越大越好,若是maxThreads過大,那麼CPU會花費大量的時間用於線程的切換,總體效率會下降。
  • Tomcat在任意時刻接收和處理的最大鏈接數。當Tomcat接收的鏈接數達到maxConnections時,Acceptor線程不會讀取accept隊列中的鏈接;這時accept隊列中的線程會一直阻塞着,直到Tomcat接收的鏈接數小於maxConnections。若是設置爲-1,則鏈接數不受限制。maxConnections的設置與Tomcat的運行模式有關。若是tomcat使用的是BIO,那麼maxConnections的值應該與maxThreads一致;若是tomcat使用的是NIO,maxConnections值應該遠大於maxThreads。
  • minSpareThreads tomcat初始化時建立的線程數,最小備用線程數
  • acceptCount 等待隊列大小。若是這個隊列也滿了,則refuse connetion
  • disableUploadTimeout 是否禁止tomcat單獨設置上傳時間限制
  • enableLookups 是否容許DNS查詢
  • compression 設置是否開啓GZip壓縮

二,安裝APR以及開啓tomcat讀APR協議

apr下載地址:github

http://apr.apache.org/download.cgi

下載APR,APR-util,APR iconvapache

安裝aprubuntu

在解壓目錄下執行tomcat

./configure --prefix=/usr/local/apr

分別執行 make,make install

安裝apr-iconv:

在解壓目錄下執行

./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr

分別執行 make,make install

安裝apr-util:

在解壓目錄下執行

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv

分別執行 make,make install

這時可能會報錯:expat.h: No such file or directory,需安裝libexpat1-dev:apt-get install libexpat1-dev

接下來從新編譯安裝tomcat-native:

在tomcat目錄下/bin下解壓 tar -zxvf tomcat-native.tar.gz

進入bin/tomcat-native-1.2.23-src/native下執行:

./configure --with-apr=/usr/local/apr

而後make   make install

在bin/catalina.sh中添加:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib export LD_LIBRARY_PATH

將conf/server.xml中讀協議改成APR:

<Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"

三,AB壓測

安裝openssl:

在安裝以前須要確認openssl的版本,最好先卸載掉原有的,安裝最新的,不然在./configure時會報錯too old(版本太低)。能夠在github上下載最新版本,在解壓目錄下執行:

./configure --prefix=/usr/local/openssl

以後make,make install

查看版本信息:

openssl version

會報錯:

openssl version
openssl: /usr/lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by openssl)
openssl: /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1: version `OPENSSL_1_1_1' not found (required by openssl)

更改環境變量:

echo "export LD_LIBRARY_PATH=/usr/local/openssl/lib" >> ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/openssl/lib

安裝ab壓測工具httpd:

下載httpd:

http://www.apache.org/dist/httpd/

在./configure時不能指定安裝好讀apr以及apr-util路徑,make時會報錯:

Makefile:48: recipe for target 'htpasswd' failed
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory '/etc/httpd-2.4.27/support'
/etc/httpd-2.4.27/build/rules.mk:75: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/etc/httpd-2.4.27/support'
/etc/httpd-2.4.27/build/rules.mk:75: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1

須要把以前下載好的apr以及apr-util壓縮包解壓到httpd解壓目錄下讀srclib下,而後在解壓目錄下執行:

./configure --prefix=/usr/local/httpd --sysconfdir=/usr/local/httpd --enable-so --enable-ssl --with-ssl=/usr/local/openssl  --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-zlib --with-pcre --with-mpm=prefork --with-included-apr

接着執行make和make install

進入httpd/bin目錄,執行:

./ab -c1000 -n10 http://localhost:8080/

進行壓測

四,一些問題

ubuntu開放8080端口:iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

相關文章
相關標籤/搜索