nginx+tomcat+redis負載均衡及session共享

在網上google好多文章,好多文章不是缺這就是缺那,最終還是順利搞定,在之前一個項目上測試成功。親自動手嘗試過,對這套架構才能更加了解,并遇到一些小坑,特以此文記錄。

一、安裝nginx

創建安裝目錄:mkdir /usr/local/soft && cd /usr/local/soft
命令下載Nginx壓縮包:wget http://nginx.org/download/nginx-1.10.3.tar.gz
解壓壓縮包:tar zxvf nginx-1.10.3.tar.gz
進入安裝目錄: cd /usr/local/nginx-1.10.3
嘗試安裝:./configure

在最后提示:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

Nginx共依賴以下三個包:
1.gzip 模塊需要 zlib 庫 ( 下載: http://www.zlib.net/ )
2.rewrite 模塊需要 pcre 庫 ( 下載: http://www.pcre.org/ )
3.ssl 功能需要 openssl 庫 ( 下載: http://www.openssl.org/ )

依賴包安裝順序依次為:openssl、zlib、pcre, 最后安裝Nginx包
1)下載并安裝openssl

wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
解壓壓縮包:tar zxvf openssl-1.0.1t.tar.gz
安裝:cd openssl-1.0.1t && ./config && make && make install

2)下載并安裝zlib

wget http://zlib.net/zlib-1.2.11.tar.gz
解壓:tar zxvf zlib-1.2.8.tar.gz 
安裝:cd zlib-1.2.8/ && ./configure && make && make install

3)下載并安裝PCRE

wget https://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
解壓:tar zxvf pcre-8.37.tar.gz
安裝:cd pcre-8.37/ && ./configure && make && make install

4)再次嘗試安裝Nginx

安裝命令:cd nginx-1.10.3/ && ./configure && make && make install

5)測試開啟Nginx

嘗試啟動:cd /usr/local/nginx/sbin/ && ./nginx
搜索該文件:whereis libpcre.so.1
提示:libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so /usr/local/lib/libpcre.so.1
 或 libpcre.so: /usr/local/lib/libpcre.so.1 /usr/local/lib/libpcre.so
執行:ln -s /usr/local/lib/libpcre.so.1 /lib64
 或:ln -s /usr/local/lib/libpcre.so.1 /lib
再次嘗試啟動:./nginx

打開瀏覽器,顯示如下則成功。

welcome.png

二、安裝redis

1)安裝

下載:wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar xzf redis-3.2.1.tar.gz
cd redis-3.2.1
make && make install

完成后啟動redis服務

redis-server

2)修改配置文件
redis.conf配置文件修改,先復制一份。

cp redis.conf /usr/local/redis.conf

redis.conf文件

protected-mode yes修改為no
daemonize no 修改為yes
#屏蔽bind信息
#bind 127.0.0.1

3)測試
啟動指定配置文件

redis-server /usr/local/redis.conf

客戶端訪問

$ redis-cli   
127.0.0.1:6379> exit

4)開放端口
配置好后,需要開放redis端口6379

iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

保存規則

service iptables save

重啟防火墻

service iptables restart

三、配置tomcat基于redis的session共享

1)配置第三方類庫,支持session共享
準備的包
commons-pool-1.6.jar,(http://commons.apache.org/proper/commons-pool/download_pool.cgi
tomcat-redis-session-manager-1.2-tomcat-7,(https://github.com/jcoleman/tomcat-redis-session-manager/downloads
jedis-2.1.0.jar(http://central.maven.org/maven2/redis/clients/jedis/2.1.0/jedis-2.1.0.jar

將這個三個包放入tomcat的/lib中

修改context.xml
新增redis session管理第三方支持

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" /> 
<Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="192.168.110.87" port="6379" database="0" maxInactiveInterval="60" />
context.xml.png

2)配置跨域訪問
準備包:
java-property-utils-1.10.jar
cors-filter-2.5.jar

修改web.xml,新增如下配置。(web.xml可為具體項目的web.xml。也可以為tomcat/conf/web.xml,二者的作用范圍不同)

<filter>  
  <filter-name>CORS</filter-name>  
  <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>  
  <init-param>  
      <param-name>cors.allowOrigin</param-name>  
      <param-value>*</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.supportedMethods</param-name>  
      <param-value>GET, POST, HEAD, PUT, DELETE</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.supportedHeaders</param-name>  
      <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified,app_key</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.exposedHeaders</param-name>  
      <param-value>Set-Cookie</param-value>  
  </init-param>  
  <init-param>  
      <param-name>cors.supportsCredentials</param-name>  
      <param-value>true</param-value>  
  </init-param>  
  </filter>  
  <filter-mapping>  
      <filter-name>CORS</filter-name>  
      <url-pattern>/*</url-pattern>  
  </filter-mapping>

前端需要修改

var utils = angular.module('Utils', []);    
    utils.config(['$httpProvider', config]);    
    function config($httpProvider) {    
            $httpProvider.defaults.withCredentials = true;    
    }  

四、配置nginx負載均衡

部署圖:

部署圖.png

修改nginx配置文件
在http{ }內新增

http {
    include       mime.types;
    default_type  application/octet-stream;
    #Tomcat
    upstream gs {
     #server 192.168.110.238:9080 max_fails=1 fail_timeout=10s;
     server 192.168.110.250:9080 max_fails=1 fail_timeout=10s;
     server 192.168.110.151:8080 max_fails=1 fail_timeout=10s;
    }  

    location ~ \.(do|jsp)$ {#/do,jsp結尾都轉發到tomcat集群
        proxy_pass http://gs;
        proxy_redirect off;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For
        $proxy_add_x_forwarded_for;
    }

    location /webapp{
        root  /html;#/html為前端工程根目錄
        index index.html index.htm;
    }

    location /arcgis {/arcgis
        root /html;
    }
}

重啟nginx:

/usr/local/nginx/sbin/nginx -s reload

五、注意

1、Tomcat里面web.xml的session-timeout不能設為-1,不然session保存到redis會馬上失效,不能實現session共享。
2、在tomcat/conf中修改了配置文件,但是還是用eclipse啟動的tomcat,會出現修改配置文件被沖掉的情況,可以
a、不在eclipse中啟動
b、在eclipse,server工程中修改配置文件

3、存放到session里面的對象,必須進行序列化,實現Serializable接口,不然保存不到redis中去。

4、后續需要優化的是nginx,redis注冊服務,和nginx的Index頁面配置等。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容