架構系列二:使用Nginx+tomcat實現叢集部署
在前面的一篇文章《架構系列一:系統架構的演變》中,簡單介紹了系統架構的演變,從單機到叢集部署,現在的企業級專案,可以說99.99%以上的專案,都是叢集部署,當叢集中的一個節點出現故障,會自動切換另一個節點,實現故障自動轉移,現在就基於Nginx+tomcat搭建一套完整的叢集
一、環境介紹
VM1:Ubuntu-S100 IP:192.168.130.128 部署Tomcat應用及Nginx
VM2:Ubuntu-S101 IP:192.168.130.129 部署Tomcat應用
物理機:192.168.1.101 用於作客戶端
JDK:1.8.0_171
Tomcat:apache-tomcat-7.0.62
首先要保證VM1,VM2及物理機之間能相互通訊,VM1,VM2中安裝好Java執行環境及Tomcat
二、叢集架構圖
在開始配置之前,先介紹一下整體的架構圖,如下
Nginx作為反向代理,接收使用者的請求,將請求傳送給伺服器端,伺服器端處理後返回結果,當其中一個伺服器宕機後,Nginx自動切換另一臺伺服器響應使用者請求
三、Tomcat配置
在Ubuntu-S100,Ubuntu-S101上安裝Tomcat,因為是兩個不同的機器,不會存在Tomcat埠衝突的問題,因此不需要修改Tomcat下server.xml配置檔案中的的埠,為了方便測試,分別在兩臺伺服器上的tomcat目錄webapps下面建立mynginx專案(假設就是部署在Tomcat上的應用),並在mynginx目錄下面建立index.html檔案,後面就基於這個檔案的響應來測試叢集
建立mynginx目錄,作為測試的專案根目錄
cd apache-tomcat-7.0.62/webapps/
mkdir mynginx
進入mynginx目錄,分別建立index.html檔案,並寫入如下內容
Ubuntu-S100的index.html檔案內容:
<h1>This is first server,Server name is : ubuntu_S100</h1>
Ubuntu-S101的index.html檔案內容:
<h1>This is second server,Server name is : ubuntu_S101</h1>
分別進入到Tomcat的bin目錄下,執行startup.sh檔案啟動tomcat
ubuntu@ubuntu-virtual-machine:~/dev_tools/apache-tomcat-7.0.62/bin$ ./startup.sh
在物理機上分別訪問Ubuntu-S100,Ubuntu-S101上的應用
這說明Tomcat的應用已經部署完成,並能成功訪問,接下來開始配置Nginx
四、Nginx安裝
在Ubuntu_S100上安裝nginx
Nginx需要依賴openssl,pcre,zlib三個包,需要先安裝這三個包
# 安裝openssl
sudo apt-get install openssl libssl-dev
# 安裝pcre
sudo apt-get install libpcre3 libpcre3-dev
# 安裝zlib
sudo apt-get install zlib1g-dev
解壓nginx-1.14.0.tar,重新命名nginx-1.14.0為nginx(這裡可以不需要重新命名)
#解壓
[email protected]virtual-machine:~/dev_tools$ tar -xf nginx-1.14.0.tar
#將nginx-1.14.0目錄重新命名為nginx
[email protected]virtual-machine:~/dev_tools$ mv nginx-1.14.0 nginx
進入到nginx目錄,配置並安裝nginx
cd nginx
# 配置nginx
sudo ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
# 編譯nginx
sudo make
# 安裝nginx
sudo make install
安裝完成後使用whereis 檢查nginx的安裝路徑,發現nginx安裝在/usr/local/nginx目錄下
ubuntu@ubuntu-virtual-machine:~/dev_tools/nginx$ whereis nginx
nginx: /usr/local/nginx
進入到/usr/local/nginx/sbin,執行./nginx -t命令,檢查nginx是否安裝成功,出現如下結果,證明nginx安裝成功
[email protected]-virtual-machine:~/dev_tools/nginx$ cd /usr/local/nginx/sbin
[email protected]-virtual-machine:/usr/local/nginx/sbin$ ls
nginx
[email protected]-virtual-machine:/usr/local/nginx/sbin$ sudo ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
五、啟動Nginx服務
進入到/usr/local/nginx/sbin/,直接執行nginx檔案,啟動Nginx服務
[email protected]-virtual-machine:/usr/local/nginx/sbin$ sudo ./nginx
檢查nginx程序,發現啟動了兩個nginx程序,一個master,一個worker
[email protected]-virtual-machine:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 34738 7996 0 17:40 ? 00:00:00 nginx: master process ./nginx
nobody 34739 34738 0 17:40 ? 00:00:00 nginx: worker process
五、配置Nginx+Tomcat叢集
nginx的配置檔案主要是nginx.conf,在安裝路徑/usr/local/nginx/下的conf目錄中,並沒有nginx.conf檔案,只有nginx.conf.default檔案,因此需要根據nginx.conf.default檔案,copy一份生成nginx的配置檔案nginx.conf
[email protected]:/usr/local/nginx/conf$ ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[email protected]:/usr/local/nginx/conf$ sudo cp nginx.conf.default nginx.conf
修改nginx.conf配置檔案,如下
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream myserver{
server 192.168.130.129:8080 weight=5 max_fails=2;
server 192.168.130.128:8080 weight=1 max_fails=2;
}
server {
listen 80;#監聽80埠
server_name myserver;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://myserver; #這裡的myserver必須要和upstream中指定的一致
#proxy_redirect on;
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重新檢查並載入配置檔案
[email protected]-virtual-machine:/usr/local/nginx/sbin$ sudo ./nginx -t
[email protected]-virtual-machine:/usr/local/nginx/sbin$ sudo ./nginx -s reload
由於我用的域名訪問,因此還需要修改hosts檔案,在/etc/hosts中增加如下記錄
192.168.130.128 myserver
驗證
驗證1:同時啟動ubuntu_S100,Ubuntu_S101上的tomcat服務,在瀏覽器上輸入驗證地址:http://myserver/mynginx/index.html,多重新整理幾次,會發現有時請求Ubuntu_S101上的服務,有時請求Ubuntu_S100上的服務,請求Ubuntu_S101的服務比Ubuntu_S100上的次數多,因為權重比例是5:1的嘛
驗證2:停掉Ubuntu_S101的服務,繼續傳送請求,會發Ubuntu_S100上的服務會自動響應使用者的請求
#停掉Ubuntu_S101上的服務
ubuntu@ubuntu-virtual-machine:~/dev_tools/apache-tomcat-7.0.62/bin$ ./shutdown.sh
Ubuntu_S100上的服務會自動響應使用者請求
驗證3:再次啟動Ubuntu_S101上的服務,Nginx自動將Ubuntu_S101加入到叢集中,Ubuntu_S101上的服務又可以訪問了
#啟動Ubuntu_S101上的服務
ubuntu@ubuntu-virtual-machine:~/dev_tools/apache-tomcat-7.0.62/bin$ ./startup.sh
再次請求,Ubuntu_S101上的服務又可以訪問了
到此,Tomcat的叢集就已經配置完成,Nginx會自動識別,如果有未響的伺服器,Nginx會自動將此伺服器踢出叢集中,當故障恢復後,Nginx會自動將它加入到叢集中,整個過程都自動化,我們唯一要做的是修復故障的伺服器
那如果Nginx出現故障不能正常工作了呢,我們的應用就無法訪問了,這時就需要引入keepalived,保證Nginx的高可用,請看下一篇文章《架構系列三:使用Keepalived+Nginx+tomcat實現叢集部署》