目标实现:Nginx作为负载均衡后端多Tomcat实例,通过Redis实现Session共享。
操作系统环境:CentOS 6.8
SSH:SecureCRT
其中
Nginx服务:80端口
Tomcat实例1:8080端口
Tomcat实例2:8060端口
Redis服务:6379端口
下载相关软件都放在百度云 密码:29ic ,下面的安装过程中需要的软件 都可以从这里直接下载。
安装rz/sz命令:便于文件传输
tar zxvf lrzsz-0.12.20.tar.gz && cd lrzsz-0.12.20
./configure && make && make install
安装JDK
安装Maven
安装Redis
安装Nginx
具体配置参见wangjiangnet.conf
安装Git
yum install git
安装Tomcat
mv apache-tomcat-7.0.73 tomcat8080
mv apache-tomcat-7.0.73 tomcat8060
cd /root/tomcat/tomcat8060/bin
vi catalina.sh
CATALINA_HOME=/root/tomcat/tomcat8060 cd /root/tomcat/tomcat8080/bin
vi catalina.sh
CATALINA_HOME=/root/tomcat/tomcat8080 cd /root/tomcat/tomcat8060/conf
vi server.xml #修改Server 、Connector(HTTP/1.1) 、Connector(AJP/1.3) 端口
<Server port="8065" shutdown="SHUTDOWN"> <!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8060" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" /> <!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8069" protocol="AJP/1.3" redirectPort="8443" />
编译tomcat-redis-session-manager项目【也可以直接从百度云下载】
1.git clone git@github.com:jcoleman/tomcat-redis-session-manager.git
2. cd tomcat-redis-session-manager && vi pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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> <groupId>com.ufind.session</groupId>
<artifactId>tomcat-redis-session</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.27</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build> </project>
3.执行mvn clean 和mvn install 将编译好的代码打包为:tomcat-redis-session-1.0-SNAPSHOT.jar,将tomcat-redis-session-1.0-SNAPSHOT.jar、jedis-2.7.2.jar、commons-pool2-2.0.jar 三个jar包分别放在tomcat8080和tomcat8060实例下的lib目录下。
4.修改配置/root/tomcat/tomcat8060/conf/context.xml、/root/tomcat/tomcat8080/conf/context.xml 配置redis session 共享
cd /root/tomcat/tomcat8060/conf
vi context.xml
注意 Redis 一定要配置密码的!
<?xml version='1.0' encoding='utf-8'?> <Context> <!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- tomcat-redis-session共享配置 -->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="127.0.0.1"
port="6379"
database="0"
password="abcd1234"
maxInactiveInterval="60" />
</Context>
5.复制百度云中tomcat8060目录内的index.html、login.jsp 到服务器 tomcat8060目录下/webapps/ROOT/目录下
复制百度云中tomcat8080目录内的index.html、login.jsp 到服务器 tomcat8080目录下/webapps/ROOT/目录下
6.分别启动两个tomcat实例
cd .tomcat8060/bin && ./catalina.sh start
cd .tomcat8080/bin && ./catalina.sh start
最终效果
1.第一次访问转向 端口是8060 的tomcat 实例
2.第二次访问转向 端口是8080 的tomcat 实例
3.点击上图中的login按钮,可以看到如下图,访问的其实是tomcat8060实例。
这其实就已经做到多tomcat实例的Session都是通过我们指定的Redis服务共享。
问题
1.git clone git@github.com:jcoleman/tomcat-redis-session-manager.git 不成功
[root@iZ38n4tck31thgZ ~]# git clone git@github.com:jcoleman/tomcat-redis-session-manager.git
Initialized empty Git repository in /root/tomcat-redis-session-manager/.git/
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
生成新的SSH key 参照 https://help.github.com/articles/connecting-to-github-with-ssh/