功能
归档
616
2017 年 01 月 01 日
Nginx+Tomcat+Redis实现Session共享

一:实验平台:CentOS 6.5

二:tomcat版本:apache-tomcat-7.0.73.tar.gz

三:jdk版本:jdk-7u67-linux-x64.tar.gz

四:redis版本:redis-3.0.0-rc2.tar.gz

五:Nginx版本:nginx-1.11.8.tar.gz

六:存储插件:tomcat-redis-session-manager-master.zip

七:jar包:commons-pool2-2.2.jar tomcat-redis-session-manager1.2.jar jedis-2.6.2.jar

八:使用IP地址为:192.168.7.221


步骤:


一、环境搭建


1.jdk环境的搭建

mkdir /ane/jdk
tar -xzvf jdk-7u67-linux-x64.tar.gz -C /ane/jdk #解压缩

添加Java环境变量。编辑/etc/profile/jdk.sh

#!/bin/bash
JAVA_HOME=/ane/java/jdk1.7.0_67
JAVA_BIN=$JAVA_HOME/bin
PATH=$PATH:$JAVA_BIN
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export  JAVA_HOME JAVA_BIN PATH CLASSPATH

source /etc/profile  #使环境变量生效
java -version  #查看jdk版本

java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

二、安装session


1.下载tomcat使用redis作为session存储的插件

https://github.com/jcoleman/tomcat-redis-session-manager

2.解压session

mkdir /ane/session -p
unzip tomcat-redis-session-manager-master.zip /ane/session
ll

total 44
-rw-r--r-- 1 root root  2726 Apr  8  2015 build.gradle
drwxr-xr-x 3 root root  4096 Apr  8  2015 example-app
-rw-r--r-- 1 root root    70 Apr  8  2015 Gemfile
-rw-r--r-- 1 root root   592 Apr  8  2015 Gemfile.lock
-rw-r--r-- 1 root root  1061 Apr  8  2015 license.txt
-rw-r--r-- 1 root root 11057 Apr  8  2015 README.markdown
drwxr-xr-x 4 root root  4096 Apr  8  2015 spec
drwxr-xr-x 3 root root  4096 Apr  8  2015 src
drwxr-xr-x 3 root root  4096 Apr  8  2015 vagrant

3.编辑编译的构建文件build.gradle

vim build.gradle

注释这些内容
//signing {
//  sign configurations.archives
//}

//  repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
//  authentication(userName: sonatypeUsername, password: sonatypePassword)
//  }
//  repository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
//  authentication(userName: sonatypeUsername, password: sonatypePassword)
//}

三、安装tomcat


1.解压tomcat压缩文件

mkdir /ane/
tar -zxvf apache-tomcat-7.0.73.tar.gz

2.更名tomcat,因为需要两个tomcat

mv apache-tomcat-7.0.73 tomcat1
tar -zxvf apache-tomcat-7.0.73.tar.gz  #再次解压
ll

total 8740
drwxr-xr-x 9 root root    4096 Jan 11 11:07 tomcat1
drwxr-xr-x 9 root root    4096 Jan 11 11:06 tomcat2
-rw-r--r-- 1 root root 8938514 Jan 11 11:06 apache-tomcat-7.0.73.tar.gz

3.编辑tomcat配置文件 修改其端口

pwd
/ane/tomca1/conf/

编辑server.xml

vim server.xml

<Server port="8005" shutdown="SHUTDOWN">
修改为
<Server port="8055" shutdown="SHUTDOWN">

<Connector port="8080" protocol="HTTP/1.1"
修改为
<Connector port="8088" protocol="HTTP/1.1"

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改为
<Connector port="8099" protocol="AJP/1.3" redirectPort="8443" />

4.启用tomcat

pwd
/ane/tomcat1/bin
./startup.sh 
netstat -tln

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::ffff:127.0.0.1:8055       :::*                        LISTEN      
tcp        0      0 :::8088                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
tcp        0      0 :::8099                     :::*                        LISTEN 

5.启用第二个tomcat

pwd
/ane/tomcat2/bin
./startup.sh 
netstat -tln

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 :::8080                     :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::ffff:127.0.0.1:8055       :::*                        LISTEN      
tcp        0      0 :::8088                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
tcp        0      0 :::8099                     :::*                        LISTEN      
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      
tcp        0      0 :::8009                     :::*                        LISTEN      

6.访问两个tomcat

192.168.7.222:8080
192.168.7.222:8088

7.拷贝替换jar包

cp commons-pool2-2.2.jar jedis-2.6.2.jar tomcat-redis-session-manager1.2.jar /ane/tomcat1/lib/
cp commons-pool2-2.2.jar jedis-2.6.2.jar tomcat-redis-session-manager1.2.jar /ane/tomcat2/lib/

8.重启tomcat

cd /ane/tomcat1/bin/
./startup.sh restart
cd ../../tomcat2/bin/
./startup.sh restart

四、redis


1.解压redis

mkdir /ane/redis
tar -zxvf redis-3.0.0-rc2.tar.gz -C /ane/redis

2.安装编译环境

yum -y install gcc gcc-c++ perl ncurses-devel #安装编译环境

3.编译及安装

make && make install

4.修改redis配置文件

编辑redis.conf

cd /ane/redis
vim redis.conf 

daemonize no
修改为
daemonize yes   #设置为后台运行

5.启动redis

redis-server redis.conf 
ps -ef | grep redis

root      6054     1  0 13:51 ?        00:00:00 redis-server *:6379 

五、tomcat配置文件


1.修改两个tomcat的context.xml配置文件

pwd
/ane/tomcat1/conf

编辑context.xml

在<context></context>标签内加入以下代码:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
         host="192.168.7.221"  
         port="6379"  
         database="0"  
         maxInactiveInterval="60" />
pwd
/ane/tomcat2/conf

编辑context.xml

在<context></context>标签内加入以下代码:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
         host="192.168.7.221"  
         port="6379"  
         database="0"  
         maxInactiveInterval="60" />

2.重启两个tomcat

pwd
/ane/tomcat1/bin/
./startup.sh restart
cd ../../tomcat2/bin/
./startup.sh restart

六、测试使用redis实现tomcat多机的session共享


1.新建test工程,为web模式(两个tomcat)

pwd
/ane/tomcat1/webapps/ROOT

为tomcat新建工程test.jsp

vim test.jsp

<%@paoge language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Nginx+tomcat</title>
</head>
<body>
<h1><font color="blue">J</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
 <p>
</body>
</html>

为第二个tomcat新建工程test.jsp

pwd
/ane/tomcat2/webapps/ROOT
vim test.jsp

<%@paoge language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Nginx+tomcat</title>
</head>
<body>
<h1><font color="blue">B</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
 <p>
</body>
</html>

八、Nginx


1.解压Nginx

mkdir /ane/nginx
tar -zxvf nginx-1.11.8.tar.gz -C /ane/nginx

2编译安装

 ./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.

因为缺少PCRE组件,安装pcre组件

yum -y install pcre-devel
./configure 

反馈如下信息:

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

因为缺少zlib插件

yum -y install zlib-devel
./configure 
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译安装

make && make install

3.编辑Nginx配置文件

pwd
/ane/nginx/nginx-1.11.8/conf

修改配置文件nginx.conf

vim nginx.com

#gzip  on;
#这个地方要写ip,不要写localhost  server localhost:8080;
upstream local_tomcat {
    server 192.168.7.221:8080;
    server 192.168.7.221:8088;  
}
server {
    listen       80;
    #这个地方要写ip,不要写localhost  server_name  localhost;
    server_name  192.168.7.221;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

location / {
    proxy_pass http://local_tomcat;
}

4.启动Nginx

cd /usr/local/nginx/sbin/
./nginx 
ps -ef | grep nginx

root     11221     1  0 14:14 ?        00:00:00 nginx: master process ./nginx
nobody   11222 11221  0 14:14 ?        00:00:00 nginx: worker process

5.访问测试

192.168.7.221/test.jsp

Over~

2条评论
Loading...

发表评论

电子邮件地址不会被公开。 必填项已用*标注