假设已经有个2个tomcat,分别是:8012、8022,也安装好了redis。
一、在每个tomcat下的lib文件下加入:
tomcat-redis-session-manage-tomcat7
jedis-2.5.2.jar
commons-pool2-2.2.jar
二、打开每个tomcat下的context.xml, 加入正面代码(参数都是redis的):
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost" port="6379" database="0" maxInactiveInterval="120" />
三、修改电脑的
hosts文件,加入:
127.0.0.1 localhost
四、修改nginx.conf(主要是:upstream、listen、server_name、proxy_pass ):
http {
# xxx其他信息
# xxx其他信息
# xxx其他信息
# 集群服务器地址
upstream webApp{
server 127.0.0.1:8012;
server 127.0.0.1:8022;
}
server {
listen 80;#监听端口
server_name localhost;#页面访问地址
location / {
proxy_pass http://webApp; #访问这个网址时,会请求webApp里的其中一个服务器
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /images/ { #当访问http://webApp/images/1.png时,相当于请求nginx下的static/images/1.png
root static;
}
}
# xxx其他信息
# xxx其他信息
# xxx其他信息
}
五、双击启动nginx.exe(注:cmd下:start nginx,关闭:nginx -s stop,重启:nginx -s reload),再启动两个tomcat
六、通过访问 http://localhost,多刷新几次,发现要么跳转的是8012服务器主页,要么是8022服务器主页,然后8012上的session在8022上也能拿到
七、OK
八、更方便的方式:spring session!!!s