nginx+tomcat+redis實現session共享

RedisMemcached的區別:
內存利用率:使用簡單的key-value存儲的話,Memcached的內存利用率更高,而若是Redis採用hash結構來作key-value存儲,因爲其組合式的壓縮,其內存利用率會高於Memcachedhtml

性能對比:因爲Redis只使用單核,而Memcached可使用多核,因此平均每個核上Redis在存儲小數據時比Memcached性能更高。而在100k以上的數據中,Memcached性能要高於Redis,雖然Redis最近也在存儲大數據的性能上進行優化,可是比起Memcached,仍是稍有遜色。java

 

Redis支持數據的持久化,能夠將內存中的數據保持在磁盤中,重啓的時候能夠再次加載進行使用node

Redis支持數據的備份,即master-slave模式的數據備份。linux

Redis不單單支持簡單的key-Value類型的數據,同時還提供listsetzsethash數據結構的存儲。nginx

 

 

 

 

 

 

 

部署準備:

Nginx192.168.1.11web

Tomcat1    192.168.1.12redis

Tomcat2    192.168.1.13算法

Redis192.168.1.14apache

如下環境都必需要關閉防火牆和SElinux安全機制vim

service iptables stop

setenforce 0

1.安裝部署nginx

yum -y install zlib-devel pcre-devel openssl-devel  //安裝依賴包

tar xf nginx-1.6.2.tar.gz

tar xf nginx-1.6.2.tar.gz

cd nginx-1.6.2

./configure && make && make install

cd

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

修改配置文件實現tomcat負載均衡

vim /usr/local/nginx/conf/nginx.conf

在原配置基礎添加如下紅色部分:

upstream backend {

    server 192.168.1.12:8080 weight=1 max_fails=1 fail_timeout=10s;

    server 192.168.1.13:8080 weight=1 max_fails=1 fail_timeout=10s;

}

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

            proxy_pass http://backend;

        }

保存退出,使用nginx –t 檢查配置是否正確,確認無誤啓動nginx

nginx

Nginx部署完畢

2.安裝部署tomcat1

爲了不繫統的open-jdk環境與新安裝jdk環境衝突,咱們須要卸載open-jdk 能夠不作,直接使用系統自帶JAVA環境也沒有影響

rpm -e java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86_64 --nodeps

rpm -e java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6.x86_64 --nodeps

rpm -e java-1.7.0-openjdk-devel-1.7.0.45-2.4.3.3.el6.x86_64 --nodeps

rpm -e java-1.6.0-openjdk-devel-1.6.0.0-1.66.1.13.0.el6.x86_64 --nodeps

1)解壓jdk

tar xf jdk-7u65-linux-x64.gz

mv jdk1.7.0_65 /usr/local/java

2)配置環境變量

vim /etc/profile.d/java.sh  #添加如下內容

export JAVA_HOME=/usr/local/java  #設置java的根目錄

export PATH=$PATH:$JAVA_HOME/bin

3)生效腳本

source /etc/profile.d/java.sh

安裝

tar xf apache-tomcat-7.0.54.tar.gz

mv apache-tomcat-7.0.54 /usr/local/tomcat

tomcat須要調用redisjar包放入tomcat/lib

cp tomcat-redis-session-manage-tomcat7.jar tomcat-juli.jar commons-logging-1.1.3.jar commons-pool2-2.2.jar jedis-2.5.2.jar /usr/local/tomcat/lib

修改context.xml文件以支持調用redis

vim /usr/local/tomcat/conf/context.xml

Context中加入如下內容

<Context>

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />

 <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  

host="192.168.1.14"   //redisIP地址

port="6379"    //redis端口

database="0"  

maxInactiveInterval="60" />

</Context>

修改server.xml文件以支持調取建立測試

vim /usr/local/tomcat/conf/server.xml

126加入如下內容:

126  <Context docBase="/web/webapp" path="" reloadable="false" >

127              </Context>

建立測試頁:

mkdir -p /web/webapp

cd /web/webapp/

vim index.jsp

 

Session ID:<%= session.getId() %><BR>

SessionPort:<%= request.getServerPort() %>

<% out.println("This tomcat server 192.168.1.12");%>

啓動tomcat

/usr/local/tomcat7/bin/startup.sh

[root@centos5 nginx]# netstat -anpt |grep 8080

tcp        0  0 :::8080           :::*                        LISTEN      48726/java  

 

3.tomcat2的安裝部署

tomcat1相同,惟一區別就是爲區分測試效果tomcat2測試頁面爲:

mkdir -p /web/webapp

cd /web/webapp/

vim index.jsp

 

Session ID:<%= session.getId() %><BR>

SessionPort:<%= request.getServerPort() %>

<% out.println("This tomcat server 192.168.1.13");%>

4.安裝部署redis

tar xf redis-3.2.5.tar.gz

cd redis-3.2.5

make

這裏若是報錯,tclsh8.5: not found 發現少了tcl 報錯 
  解決方法:下載,安裝tcl

#wget http://downloads.sourceforge.net/tcl/tcl8.5.9-src.tar.gz

#cd /tcl8.5.9-src/unix

#./configure

#make

#make install

接着make就沒報錯了

mkdir -p /usr/local/redis/bin

mkdir -p /usr/local/redis/etc

mkdir -p /usr/local/redis/var

cd src/

src目錄下這些文件做用以下
    redis-serverRedis服務器的daemon啓動程序
    redis-cliRedis命令行操做工具.你也能夠用telnet根據其純文本協議來操做
    redis-benchmarkRedis性能測試工具,測試Redis在你的系統及你的配置下的讀寫性能.
    redis-statRedis狀態檢測工具,能夠檢測Redis當前狀態參數及延遲情況

cp redis-benchmark  redis-check-aof redis-cli redis-server  /usr/local/redis/bin/

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

vim /usr/local/redis/etc/redis.conf

修改配置文件

daemonize no  改成 daemonize yes  //是否把redis-server啓動在後臺,默認是。若改爲yes,會生成一個pid文件

bind 127.0.0.1 改成 bind 0.0.0.0   //任意主機均可訪問

其餘的看須要修改

關閉redis

killall -9 redis-server

啓動redis

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

查看是否啓動:

netstat -anpt |grep redis

tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      46390/redis-server

監控redis共享session

redis-cli -p 6379  monitor

5.測試

在客戶端訪問http://192.168.1.11刷新後會輪流出現如下頁面:

 

 

 

測試頁面IP地址發生變化,而session ID不變

能夠查看redis主機監控,會出現如下頁面:

至此nginx+2 tomcat+redis實現session共享成

 

使用keepalived來實現redis主從高可用

接上面的實驗,下面使用keepalived來實現redis主從高可用

部署準備

添加一臺Redis2服務器

Nginx192.168.1.11

Tomcat1    192.168.1.12

Tomcat2    192.168.1.13

Redis1192.168.1.14

Redis2192.168.1.15

關閉防火牆和SElinux安全機制

service iptables stop

setenforce 0

部署redis

Redis2上安裝部署redis2

安裝過程與redis1相同

修改redis2redis.conf

vim /usr/local/redis/etc/redis.conf

daemonize no  改成 daemonize yes

bind 127.0.0.1 改成 bind 0.0.0.0

slaveof 192.168.1.14 6379    //去掉此行註釋,並改成redis1ip地址和端口

而後啓動redis2

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

測試redis1redis2數據是否能同步

redis1上寫入數據:

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.1.14

192.168.1.14:6379> set name zhangsan

OK

192.168.1.14:6379> get name

"zhangsan"

redis2查看:

[root@localhost src]# /usr/local/redis/bin/redis-cli -h 192.168.1.15

192.168.1.15:6379> get name

"zhangsan"

此時兩臺redis服務器之間數據能同步

安裝keepalived

redis1安裝keepalived

直接使用yum方式安裝便可

yum -y install keepalived

chkconfig keepalived --add

chkconfig keepalived on

修改配置文件

vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script redis {

        script /opt/chk_redis.sh

        interval 2

        weight -10

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

}

track_script {

        redis

        }

    virtual_ipaddress {

        192.168.1.10

    }

}

redis2安裝keepalived

yum -y install keepalived

chkconfig keepalived --add

chkconfig keepalived on

 

vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script redis {

        script /opt/chk_redis.sh

        interval 2

        weight -10

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

}

track_script {

        redis

        }

    virtual_ipaddress {

        192.168.1.10

    }

}

分別redis1redis2部署redis健康檢測腳本

兩臺redis相同操做:

vim /opt/chk_redis.sh

 

#!/bin/bash

# check redis server st="/usr/local/nginx/sbin/nginx"

REDIS="/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf"

redispid=$(ps -C redis-server --no-header |wc -l)

if [ $redispid -eq 0 ];then

$REDIS

sleep 3

redispid=$(ps -C redis-server --no-header |wc -l)

if [ $redispid -eq 0 ];then

/etc/init.d/keepalived stop

echo "Keepalived stopped ,please check your redis !"|tee -a /var/log/messages

fi

fi

啓動keepalived

service keepalived start

分別修改tomcat1tomcat2context.xml支持VIP

兩臺tomcat相同操做:

vim /usr/local/tomcat/conf/context.xml

host="192.168.1.14"  改成 host="192.168.1.10"

重啓tomcat服務:

/usr/local/tomcat/bin/shutdown.sh

/usr/local/tomcat/bin/startup.sh

測試

關閉redis1keepalivedredis服務注意必定要先關閉keepalived,不然redis再次重啓,影響測試效果

/etc/init.d/keepalived stop

killall -9 redis-server

而後使用客戶端訪問http://192.168.1.1出現如下頁面:

至此nginx + 2tomcat + 2redis主從部署完畢

相關文章
相關標籤/搜索