1. 程式人生 > >day77_淘淘商城專案_10_ Linux下的Nginx代理詳解(配置虛擬主機+實現反向代理+實現負載均衡+高可用) + 單點登入系統工程搭建 + SSO系統介面文件講解_匠心筆記

day77_淘淘商城專案_10_ Linux下的Nginx代理詳解(配置虛擬主機+實現反向代理+實現負載均衡+高可用) + 單點登入系統工程搭建 + SSO系統介面文件講解_匠心筆記

淘淘商城專案_10


課程計劃

  • 使用Linux下的nginx實現網頁靜態化的測試
  • nginx的安裝
  • nginx配置虛擬主機
  • nginx實現反向代理
  • nginx實現負載均衡
  • SSO系統工程搭建

1、目前訪問系統使用的方式存在的問題

目前訪問後臺系統:
  http://localhost:8081/
目前訪問首頁系統:
  http://localhost:8082/
目前訪問搜尋結果頁面:
  http://localhost:8085/
目前訪問商品詳情頁面:
  http://localhost:8086/
在搜尋結果頁面search.jsp中,程式碼中寫死了其他的系統的全路路徑。如下:

問題:
  1、localhost只能訪問本地,不能訪問其他的伺服器;系統應當要部署在測試環境生產環境。可以使用ip地址
  2、開發環境的ip地址和測試環境的ip地址是不一樣的。每次環境變化的時候,都需要修改訪問的ip地址
  3、頁面載入資源或者請求其他系統的URL時,使用了全路徑,一旦環境發生改變,資源無法載入,請求無法訪問。
  4、ip地址沒有意義,不容易記憶,使用者不會通過ip地址進行訪問,一般通過域名訪問。可以使用nginx進行配置,達到使用域名訪問的目的。

2、什麼是nginx

  Nginx是一款高效能的http伺服器/反向代理伺服器電子郵件(IMAP/POP3)代理伺服器。由俄羅斯的程式設計師Igor Sysoev用c語言所開發,官方測試nginx能夠支撐5萬併發連結(tomcat支援500併發連結),並且cpu、記憶體等資源消耗卻非常低,執行非常穩定。開源。

3、nginx的應用場景

1、http伺服器。Nginx是一個http服務可以獨立提供http服務。可以做靜態網頁伺服器
2、虛擬主機伺服器。可以實現在一臺伺服器虛擬出多個網站。例如:個人網站使用的虛擬主機
3、反向代理,負載均衡伺服器。當網站的訪問量達到一定程度後,單臺伺服器不能滿足使用者的請求時,需要用多臺伺服器叢集,可以使用nginx做反向代理。並且多臺伺服器可以平均分擔負載,不會因為某臺伺服器負載高宕機而某臺伺服器閒置的情況。

4、nginx的安裝

下載nginx,官方網站:http://nginx.org/

我們使用的版本是1.8.0版本。最新版本是1.15.7。
由於市面上linux發行版本過多,版本之間的壓縮包格式不同,所以nginx提供nginx的原始碼安裝。需要我們在Linux上進行編譯。

4.1、要求的安裝環境

0、先有一個乾淨的Linux系統

[[email protected] ~]# ll
總用量 0
[[email protected] ~]# 

1、需要安裝gcc、g++的環境。

[[email protected] ~]# yum -y install gcc
[[email protected] ~]# yum -y install g++

2、需要安裝第三方的開發包。

  • PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 相容的正則表示式庫。nginx的http模組使用pcre來解析正則表示式,所以需要在linux上安裝pcre庫。
yum install -y pcre
yum install -y pcre-devel

  注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。

  • zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y zlib
yum install -y zlib-devel
  • OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程式供測試或其它目的使用。
    nginx不僅支援http協議,還支援https(即在SSL協議上傳輸http),所以需要在linux安裝openssl庫。
yum install -y openssl
yum install -y openssl-devel

4.2、安裝步驟

第一步:把nginx的原始碼包上傳到linux系統。
第二步:解壓縮後刪除原始碼包。

[[email protected] ~]# ll
總用量 816
-rw-r--r--. 1 root root 832104 9月  26 23:20 nginx-1.8.0.tar.gz
[[email protected] ~]# tar zxf nginx-1.8.0.tar.gz 
[[email protected] ~]# ll
總用量 816
drwxr-xr-x. 8 1001 1001    158 4月  21 2015 nginx-1.8.0
-rw-r--r--. 1 root root 832104 9月  26 23:20 nginx-1.8.0.tar.gz
[[email protected] ~]# rm -rf nginx-1.8.0.tar.gz 
[[email protected] ~]# ll
總用量 0
drwxr-xr-x. 8 1001 1001 158 4月  21 2015 nginx-1.8.0
[[email protected] ~]# 

第三步:C語言編譯需要一個Makefile檔案,我們使用configure命令建立一個Makefile檔案。

[[email protected] nginx-1.8.0]# make
make: *** 沒有指明目標並且找不到 makefile。 停止。
[[email protected] nginx-1.8.0]# pwd
/root/nginx-1.8.0
[[email protected] nginx-1.8.0]# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注意:啟動nginx之前,上邊將臨時檔案目錄指定為/var/temp/nginx,需要先在/var下建立temp及nginx目錄。命令如下:

[[email protected] nginx-1.8.0]# mkdir /var/temp/nginx -p       #-p表示建立多級目錄

第四步:編譯

[[email protected] nginx-1.8.0]# make

第五步:安裝

[[email protected] nginx-1.8.0]# make install

第六步:檢視安裝好的nginx

4.3、啟動nginx和關閉nginx

進入sbin目錄

[[email protected] sbin]# pwd
/usr/local/nginx/sbin
[[email protected] sbin]# ./nginx

檢視nginx的程序

關閉nginx

[[email protected] sbin]# ./nginx -s stop    #正常關閉
[[email protected] sbin]# kill 56612         #正常關閉
[[email protected] sbin]# kill -9 56612      #暴力關閉

在正常提供服務的時候,由於nginx是整個網站的入口,如果關閉了ngin,那麼整個網站受影響很大,所以我們不推薦關閉nginx。
我們可以使用命令reload在不關機的狀態下重新整理配置。

[[email protected] sbin]# ./nginx -s reload  #在不關機的狀態下重新整理配置

4.4、訪問nginx

訪問地址:http://192.168.25.141
預設是80埠,預設可以不用寫。
CentOS 7.X 預設的防火牆不是iptables,而是firewalld。我們可以試一下systemctl stop firewalld關閉防火牆,但是生產環境下不推薦該方式。

systemctl stop firewalld.service        #停止firewall
systemctl disable firewalld.service     #禁止firewall開機啟動
firewall-cmd --state                    #檢視預設防火牆狀態(關閉後顯示notrunning,開啟後顯示running)

CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables修改配置即可。
本博主的是CentOS 7.X 防火牆使用的是firewalld,我們使用命令的方式來新增埠(修改後需要重啟firewalld服務):

[[email protected] zones]# pwd
/etc/firewalld/zones
[[email protected] zones]# firewall-cmd --permanent --add-port=80/tcp  
success
[[email protected] zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[[email protected] zones]# 

瀏覽器顯示效果如下:

5、nginx的3個命令

啟動命令

./nginx

關閉命令

./nginx -s stop 
或者
./nginx -s quit

重新整理配置檔案

./nginx -s reload

修改了nginx.conf檔案之後,重新整理配置檔案,可以不重啟nginx,能馬上生效。
我們重啟虛擬機器後,再次重啟nginx會報錯,解決問題連結:https://www.cnblogs.com/chenmingjun/p/10052205.html

6、nginx配置虛擬主機

就是在一臺伺服器上虛擬出多個網站。
如何區分不同的網站:
  1、埠不同
  2、域名不同

6.1、通過埠區分虛擬主機

nginx的配置檔案配置項如下:
檔案位置:/usr/local/nginx/conf/nginx.conf
檔案具體內容如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
pid         /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

我們需要關心的地方

可以配置多個server,即配置了多個虛擬的主機。

6.1.1、使用xftp配置遠端編輯器

直接使用vim命令不太方便,一般需要修改遠端工程中的檔案,除了使用vim命令外,還可以配置文字編輯器,直接作用於遠端檔案。
第一步:使用Xshell5連線到虛擬機器,先新建遠端檔案輸出。
第二步:選擇【工具】 --> 【選項】 --> 【高階】 --> 【設定(S)…】 --> 取消勾選“將記事本用作文字編輯器”

第三步:重啟Xshell 5,開啟Xftp 5選擇要開啟的檔案右鍵 --> 以Notepad++編輯

6.1.2、使用EditPlus編輯器配置連線FTP伺服器(推薦方式)

使用EditPlus安裝FTP外掛後,注意:高版本的EditPlus已經自帶FTP外掛了。
然後開啟EditPlus,點選【檔案】 --> 【FTP】–> 【設定FTP伺服器…】

點選【新增】,輸入要連結的FTP伺服器的資訊,如下圖:

再點選【高階設定(G)…】

然後點選【確定】,回到主面板,點選磁碟下拉框,出現

彈出是否信任該站點,點選【是】

這樣我們就連線上Linux了,如下圖:

然後可以開啟我們需要編輯的檔案,編輯後儲存,就會自動同步至伺服器Linux上了。

6.1.3、新增虛擬主機

第一步:修改配置檔案nginx.conf,新增如下內容:

################################################# nginx配置虛擬主機,通過埠區分虛擬主機
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
################################################# nginx配置虛擬主機,通過埠區分虛擬主機
    server {
        listen       81;
        server_name  localhost;
        
        location / {
            root   html81;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html81;
        }
    }

第二步:需要在伺服器Linux上nginx的目錄下新建測試目錄/html81,為了便於區分,稍微修改該目錄下的首頁內容

[[email protected] nginx]# ll
總用量 4
drwxr-xr-x. 2 root root 4096 12月  1 21:00 conf
drwxr-xr-x. 2 root root   40 12月  1 16:27 html
drwxr-xr-x. 2 root root   19 12月  1 16:27 sbin
[[email protected] nginx]# cp -r html/ html81
[[email protected] nginx]# ll
總用量 4
drwxr-xr-x. 2 root root 4096 12月  1 21:00 conf
drwxr-xr-x. 2 root root   40 12月  1 16:27 html
drwxr-xr-x. 2 root root   40 12月  1 21:05 html81
drwxr-xr-x. 2 root root   19 12月  1 16:27 sbin
[[email protected] nginx]# 

第三步:每次配置檔案修改之後都需要重新載入配置檔案。

[[email protected] nginx]# sbin/nginx -s reload

第四步:修改防火牆配置
CentOS 7.X 預設的防火牆不是iptables,而是firewalld。我們可以試一下systemctl stop firewalld關閉防火牆,但是不推薦該方式。
CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables修改配置即可。
本博主的是CentOS7,防火牆使用的是firewalld,我們使用命令的方式來新增埠(修改後需要重啟firewalld服務):

[[email protected] zones]# pwd
/etc/firewalld/zones
[[email protected] zones]# firewall-cmd --permanent --add-port=81/tcp  
success
[[email protected] zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[[email protected] zones]# 

第四步:在瀏覽器上進行測試
訪問地址:http://192.168.25.141:81/
瀏覽器顯示效果如下:

6.2、通過域名區分虛擬主機

6.2.1、什麼是域名

域名就是網站。
  www.baidu.com
  www.taobao.com
  www.jd.com
頂級域名:
  com
  cn
  org
一級域名:
  baidu.com
  taobao.com
  jd.com
二級域名:
  www.baidu.com
  image.baidu.com
  tieba.baidu.com
三級域名:
  1.image.baidu.com
  aaa.image.baidu.com
域名訪問網站,其本質還是通過tcp/ip協議訪問。

DNS伺服器:把域名解析為ip地址。儲存的就是域名和ip的對映關係,可以簡單的理解為一個MAP<KEY,VALUE>集合。
一個域名對應一個ip地址,一個ip地址可以被多個域名繫結。


本地測試可以修改hosts檔案。
修改window的hosts檔案,檔案位置:C:\Windows\System32\drivers\etc
可以配置域名和ip的對映關係,如果hosts檔案中配置了域名和ip的對應關係,就不走DNS伺服器了。
手動修改hosts檔案比較麻煩,一般企業中使用hosts切換的工具SwitchHosts,用來方便切換hosts檔案中的配置。

使用時必須以管理員許可權執行。開啟軟體,如下圖所示:

6.2.2、nginx的配置

################################################# nginx配置虛擬主機,通過埠區分虛擬主機
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
################################################# nginx配置虛擬主機,通過埠區分虛擬主機
    server {
        listen       81;
        server_name  localhost;
        
        location / {
            root   html81;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html81;
        }
    }
    
################################################# nginx配置虛擬主機,通過域名區分虛擬主機
    server {
        listen       80;
        server_name  www.sohu.com;
        
        location / {
            root   html-sohu;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html-sohu;
        }
    }
################################################# nginx配置虛擬主機,通過域名區分虛擬主機
    server {
        listen       80;
        server_name  www.163.com;
        
        location / {
            root   html-163;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html-163;
        }
    }

本地主機域名的配置:

    192.168.25.141 www.sohu.com
    192.168.25.141 www.163.com

7、nginx實現反向代理

7.1、什麼是反向代理


參考連結:https://www.cnblogs.com/chenmingjun/p/9187251.html
反向代理伺服器決定哪臺伺服器提供服務。
反向代理伺服器不提供服務。只是請求的轉發。

7.2、nginx實現反向代理

兩個域名指向同一臺nginx伺服器,使用者訪問不同的域名顯示不同的網頁內容。
兩個域名是www.163.com.cn和www.sohu.com
nginx伺服器使用虛擬機器,ip地址:192.168.25.141

第一步:安裝兩個tomcat,分別執行在8080和8081埠。
本博主的是CentOS7,防火牆使用的是firewalld,我們使用命令的方式來新增所用到的埠(修改後需要重啟firewalld服務):

[[email protected] zones]# pwd
/etc/firewalld/zones
[[email protected] zones]# firewall-cmd --permanent --add-port=8080/tcp
success
[[email protected] zones]# firewall-cmd --permanent --add-port=8081/tcp
success
[[email protected] zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[[email protected] zones]# 

為了區分方便,我們稍微修改下tomcat的歡迎頁。
第二步:分別啟動兩個tomcat。
訪問測試:
  http://192.168.25.141:8080/
  http://192.168.25.141:8081/
第三步:反向代理伺服器的配置,在nginx.conf中配置

################################################# nginx實現反向代理(先通過域名區分虛擬主機)
    upstream tomcat1 {
        server   192.168.25.141:8080;
    }
    upstream tomcat2 {
        server   192.168.25.141:8081;
    }
#################################################
    server {
        listen       80;
        server_name  www.test1.com;
        
        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
#################################################
    server {
        listen       80;
        server_name  www.test2.com;
        
        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }

第四步:nginx重新載入配置檔案

[[email protected] nginx]# sbin/nginx -s reload

第五步:在本地配置域名,在hosts檔案中新增域名和ip的對映關係

192.168.25.141 www.test1.com
192.168.25.141 www.test2.com

瀏覽器顯示效果:

8、使用域名訪問淘淘商城後臺系統

1、先配置本地域名和ip地址的對映:

127.0.0.1 www.taotao.com

2、使用本地Windows版本的nginx配置nginx.conf,新增server節點如下:

    upstream tomcat1 {
        server 127.0.0.1:8082;
    }	 
    
    server {
        listen       80;
        server_name  www.taotao.com;

    location / {
        proxy_pass   http://tomcat1;
        index  index.html index.htm;
    }

3、Windows下啟動nginx
雙擊nginx.exe
4、檢視測試效果

9、nginx實現負載均衡

如果一個服務由多臺伺服器(伺服器叢集)提供,需要把負載分配到不同的伺服器處理,需要負載均衡。
我們新新增一個tomcat,埠為8082,提供的服務跟tomcat(8081)提供的服務一樣。
在nginx.conf中配置如下:

    upstream tomcat2 {
        server 192.168.25.141:8081;
        server 192.168.25.141:8082;
    }

即預設的策略就是輪詢的方式。
可以根據伺服器的實際情況調整伺服器權重。權重越高分配的請求越多,權重越低,請求越少。預設是都是1。

    upstream tomcat2 {
        server   192.168.25.141:8081;
        server   192.168.25.141:8082 weight=5;
    }

詳細程式碼如下:

################################################# nginx實現反向代理(先通過域名區分虛擬主機)
    upstream tomcat1 {
        server   192.168.25.141:8080;
    }
    upstream tomcat2 { #如果一個服務由多臺伺服器(伺服器叢集)提供,需要把負載分配到不同的伺服器處理,需要nginx實現負載均衡。
        server   192.168.25.141:8081;
        server   192.168.25.141:8082 weight=10;
    }
#################################################
    server {
        listen       80;
        server_name  www.test1.com;
        
        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
#################################################
    server {
        listen       80;
        server_name  www.test2.com;
        
        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }

擴充套件小知識:
  負載均衡分為:軟負載、硬負載。
  nginx是第七層應用層負載,最高可以支援5萬併發,不支援搭建叢集,因為nginx負載是網站入口,軟負載。
  如果非得讓nginx搭建叢集,那麼網站入口需要使用硬負載F5,F5是第四層傳輸層負載,硬負載。
  如果非得讓nginx搭建叢集,還可以使用LVS(Linux虛擬伺服器,章文嵩),也是第四層傳輸層負載,但卻是軟負載。可以達到F5硬負載的60%的效能。
  還可以通過CDN,全稱是Content Delivery Network,即內容分發網路。即在網路節點的各個地方加快取。

10、nginx的高可用(瞭解)

要實現nginx的高可用,需要實現備份機

10.1、什麼是負載均衡高可用

  nginx作為負載均衡器,所有請求都到了nginx,可見nginx處於非常重點的位置,如果nginx伺服器宕機,則後端web服務將無法提供服務,影響嚴重。
  為了遮蔽負載均衡伺服器的宕機,需要建立一個備份機。主伺服器和備份機上都執行高可用(High Availability)監控程式,通過傳送諸如“I am alive”這樣的資訊來監控對方的執行狀況。當備份機不能在一定的時間內收到這樣的資訊時,它就接管主伺服器的服務IP並繼續提供負載均衡服務;當備份管理器又從主管理器收到“I am alive”這樣的資訊時,它就釋放服務IP地址,這樣的主伺服器就開始再次提供負載均衡服務。

10.2、keepalived + nginx 實現主備

10.2.1、什麼是keepalived

  keepalived是叢集管理中保證叢集高可用的一個服務軟體,用來防止單點故障
  keepalived的作用是檢測web伺服器的狀態,如果有一臺web伺服器宕機,或工作出現故障,keepalived將檢測到,並將有故障的web伺服器從系統中剔除,當web伺服器工作正常後Keepalived自動將web伺服器加入到伺服器群中,這些工作全部自動完成,不需要人工干涉,需要人工做的只是修復故障的web伺服器

10.2.2、keepalived的工作原理

  keepalived是以VRRP協議為實現基礎的,VRRP全稱 Virtual Router Redundancy Protocol,即虛擬路由冗餘協議
  虛擬路由冗餘協議,可以認為是實現路由器高可用的協議,即將N臺提供相同功能的路由器組成一個路由器組,這個組裡面有一個master和多個backup,master上面有一個對外提供服務的vip(VIP = Virtual IP Address,虛擬IP地址,該路由器所在區域網內其他機器的預設路由為該vip),master會發組播,當backup收不到VRRP包時就認為master宕掉了,這時就需要根據VRRP的優先順序來選舉一個backup當master。這樣的話就可以保證路由器的高可用了。
  keepalived主要有三個模組,分別是core、check和VRRP。core模組為keepalived的核心,負責主程序的啟動、維護以及全域性配置檔案的載入和解析。check負責健康檢查,包括常見的各種檢查方式。VRRP模組是來實現VRRP協議的。
  詳細參考:Keepalived權威指南中文.pdf

10.2.3、keepalived + nginx 實現主備過程

初始狀態

主機宕機

主機恢復

10.2.4、高可用環境

兩臺nginx,一主一備:192.168.101.3和192.168.101.4
兩臺tomcat伺服器:192.168.101.5、192.168.101.6

10.2.5、安裝keepalived

分別在主備nginx上安裝keepalived,參考“安裝手冊”進行安裝:

11、SSO系統的分析

11.1、什麼是SSO系統

  SSO英文全稱Single Sign On,單點登入。SSO是在多個應用系統中,使用者只需要登入一次就可以訪問所有相互信任的應用系統。它包括可以將這次主要的登入對映到其他應用中用於同一個使用者的登入的機制。它是目前比較流行的企業業務整合的解決方案之一。

11.2、為什麼要有單點登入系統

11.2.1、傳統的登入實現方式


此方式在只有一個web工程時是沒有問題。

11.2.2、叢集環境下的登入實現方式


叢集環境下會出現要求使用者多次登入的情況。
解決方案:
  1、配置tomcat叢集。配置tomcatSession複製。節點數不要超過5個。
  2、可以使用Session伺服器,儲存Session資訊,使每個節點是無狀態。需要模擬Session
  單點登入系統是使用Redis模擬Session,實現Session的統一管理,解決Session的共享問題.

12、SSO系統的實現


需要建立一個sso服務工程,可以參考taotao-manager建立。

12.1、服務層工程搭建

taotao-sso(pom聚合工程)
   |–taotao-sso-interface(jar)
   |–taotao-sso-service(war)
可以參考taotao-manager建立。

12.1.1、taotao-sso


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>taotao-sso</artifactId>
	<packaging>pom</packaging>
	<modules>
		<module>taotao-sso-interface</module>
		<module>taotao-sso-service</module>
	</modules>
    <dependencies>
        <!-- 配置對taotao-common的依賴-->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置Tomcat外掛  -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8087</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

12.1.2、taotao-sso-interface


jar

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-sso</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>taotao-sso-interface</artifactId>
    <dependencies>
        <!-- 配置對taotao-manager-pojo的依賴 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-manager-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

12.1.3、taotao-sso-service


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-sso</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>taotao-sso-service</artifactId>
	<packaging>war</packaging>
    <dependencies>
        <!-- 配置對taotao-manager-dao的依賴 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-manager-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 配置對taotao-sso-interface的依賴:服務層釋出服務要通過該介面 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-sso-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 配置對spring的依賴 -->
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context<