先說一下為什么寫這個文章,在性能測試過程中,我們可能會關(guān)注很多指標,比如CPU、IO、網(wǎng)絡、磁盤等,通過這些指標大致可以判斷哪個環(huán)節(jié)遇到了性能瓶頸,但是當這些指標無法判斷出性能瓶頸時,我們可能就需要對一些中間件進行監(jiān)控,比如Nginx,Tomcat等,當然可能還有很多其他中間件,我們本章主要探討Nginx+Tomcat的部署及監(jiān)控,以及使用Jmeter對我們的服務器進行壓測,在壓測過程中,可能也會遇到Jmeter的一些瓶頸,話不多說,先搞起來。
關(guān)于Nginx實現(xiàn)負載均衡
Nginx作為反向代理服務器,實現(xiàn)負載均衡。首先瀏覽器發(fā)起請求,到達Nginx,由Nginx將請求地址轉(zhuǎn)發(fā)給相應的tomcat服務器,再由tomcat服務器將結(jié)果返回給Nginx,Nginx將結(jié)果再轉(zhuǎn)發(fā)給瀏覽器。大致流程如下(畫的比較粗糙,將就著看吧):

環(huán)境準備
- Ubuntu16.04
- JDK1.8
- Tomcat8
客戶端
- Vmware
- Xshell
- Xftp
Xsehll配置
我們安裝Ubuntu16.04后,使用Xshell進行連接,可能會連接失敗,我們輸入ps -e |grep ssh,如果沒有任何反應則是沒有安裝,使用apt-get install openssh-server進行安裝。Xshell主要作用是方便我們敲Linux命令。
Xftp配置
我們安裝Ubuntu16.04后,使用Xftp進行連接,我們使用普通用戶可以連接成功,但是使用root進行連接,可能會連接失敗,命令行輸入passwd,進行密碼設置,再次連接即可。Xftp主要作用是上傳和下載文件。
JDK環(huán)境配置
我們把jdk放到/usr/local 下,然后輸入vi /etc/profile,在文件末尾加入如下內(nèi)容:
export JAVA_HOME=/usr/local/jdk1.8.0_65
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
然后輸入source /etc/profile,在命令行輸入java -version,出現(xiàn)如下內(nèi)容下圖,說明JDK環(huán)境配置成功
root@ubuntu:/usr/local# java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
更換Ubuntu源
- 備份系統(tǒng)源:
cd /etc/apt
sudo cp sources.list sources.list_bak
- 添加新的源文件:
sudo vi sources.list,并添加以下內(nèi)容:
deb http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe
-
sudo apt-get update,更新成功
安裝Nginx
apt-get install nginx,安裝成功訪問localhost:8080,出現(xiàn)下面界面說明Nginx部署成功

Nginx常用命令
-
/etc/init.d/nginx stop關(guān)閉 -
/etc/init.d/nginx restart重啟 -
nginx -v查看版本
Tomcat部署
- 把2個Tomcat包放在
/usr/local,為了避免端口沖突,我們需要修改tomcat/conf/server.xml三處地方,
tomcat1如下:
<Server port="8006" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
tomcat2如下:
<Server port="8007" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
修改完成之后,tomcat1的端口位8081,tomcat2的端口為8082
- 修改完tomcat端口號之后,為了區(qū)分tomcat1和tomcat2,修改
tomcat/webapps/ROOT/index.jsp,tomcat1修改為如下內(nèi)容
<html>
<head>
<title>第一個 JSP 程序</title>
</head>
<body>
<%
out.println("111111111");
%>
</body>
</html>
tomcat2修改為如下內(nèi)容
<html>
<head>
<title>第一個 JSP 程序</title>
</head>
<body>
<%
out.println("22222222");
%>
</body>
</html>
- 進入
/usr/local/,輸入chmod 777 -R tomcat1 tomcat2給2個tomcat賦予權(quán)限(如果權(quán)限不夠,把bin也賦予權(quán)限),然后進入/usr/local/tomcat/bin,輸入./startup.sh啟動tomcat1和tomcat2,啟動成功截圖如下:
tomcat1.png
tomcat2.png
修改nginx.conf
首先我們使用find / -name nginx.conf找到nginx.conf的位置,我們使用/etc/nginx/nginx.conf這個路徑的nginx.conf文件,使用vim nginx.conf打開,找到http,在include /etc/nginx/sites-enabled/*下面新增如下內(nèi)容:
upstream tomcat_server {
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=1;
}
server {
listen 80 default_server;
server_name localhost;
location / {
proxy_pass http://tomcat_server/;
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Forward-For $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
做了如上配置后,重啟nginx,會報如下錯誤:
root@ubuntu:/usr/local/tomcat2/bin# /etc/init.d/nginx restart
[....] Restarting nginx (via systemctl): nginx.serviceJob for nginx.service failed because
the control process exited with error code. See "systemctl status nginx.service" and
"journalctl -xe" for details.failed!
我們把/etc/nginx/sites-enabled/default,所有內(nèi)容進行注釋,重啟nginx,啟動成功,當我們訪問localhost時,會發(fā)現(xiàn)不是Nginx的首頁了,而是出現(xiàn)/usr/local/tomcat1/webapps/ROOT/index.jsp或者是/usr/local/tomcat2/webapps/ROOT/index.jsp的頁面。當我們來回刷新,會發(fā)現(xiàn)在下面的2個頁面來回跳轉(zhuǎn),這就實現(xiàn)了負載均衡技術(shù)。


小結(jié):本章內(nèi)容主要介紹了如何通過Nginx+tomcat實現(xiàn)負載均衡,因為文章內(nèi)容字數(shù)超出預期,把Nginx和tomcat的監(jiān)控以及使用Jmeter壓測內(nèi)容放在下一章進行講解。關(guān)于負載均衡更多的內(nèi)容還需要大家自己去了解。下一章還會講一些關(guān)于CPU、IO、網(wǎng)絡、磁盤的監(jiān)控。當然如果我們想要在centos部署也是可以的,只有細微區(qū)別,這里不做演示。歡迎點贊~
歡迎關(guān)注微信公眾號:軟件測試汪。軟件測試交流群:809111560
轉(zhuǎn)載請注意出處,謝謝合作

