星空网 > 软件开发 > Java

windows 环境下nginx + tomcat群 + redis 实现session共享

 

 

本例采用的是

nginx 1.9.13  

tomcat 7.0.64

redis 2.8

 

操作步骤:

① 准备两个tomcat,并修改端口,修改tomcat/webapps/ROOT中index.jsp内容为:

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%><%  String username = request.getParameter("username");  System.out.println("username:"+username);    if(username!=null){    session.setAttribute("userSession", username);    System.out.println("value in session is :" + session.getAttribute("userSession"));  }%><!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>ngix+tomcat+redis session共享验证</title></head><body><form action="index.jsp">   <input type="text" name="username" value=""/>   <br/>   <input type="submit" value="保存信息到session"/></form><hr/><!-- 不同tomcat请使用不同mark 以便识别 -->你访问的tomcat 端口为:8898  tomcat001<br/>session值为: <%=session.getAttribute("userSession") %></body></html>

并保证两个tomcat都能正常启动,并能正常访问

 

② 下载ngix并修改conf文件如下

windows 环境下nginx + tomcat群 + redis 实现session共享windows 环境下nginx + tomcat群 + redis 实现session共享
#Nginx所用用户和组#user niumd niumd;#工作的子进程数量(通常等于CPU数量或者2倍于CPU)worker_processes 1;#错误日志存放路径#error_log logs/error.log;#error_log logs/error.log notice;error_log logs/error.log info;#指定pid存放文件pid    logs/nginx.pid;events {    #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue  #use epoll;    #允许最大连接数  worker_connections 1024;}http {  include    mime.types;  default_type application/octet-stream;    #定义日志格式  #log_format main '$remote_addr - $remote_user [$time_local] $request '  #         '"$status" $body_bytes_sent "$http_referer" '  #         '"$http_user_agent" "$http_x_forwarded_for"';  #access_log off;  access_log logs/access.log;  client_header_timeout 3m;  client_body_timeout  3m;  send_timeout      3m;   client_header_buffer_size  1k;  large_client_header_buffers 4 4k;  sendfile    on;  tcp_nopush   on;  tcp_nodelay   on;  #keepalive_timeout 75 20;  include  gzip.conf;    upstream localhost {     #ip_hash   #ip_hash; #测试时请不要打开ip_hash 否则不会在不同server间跳转,第一次访问的哪一个就会一直访问哪一个server      #下面为配置的server列表,weight表示权重,值越大分配到的请求就越多,默认为1.   server localhost:8898 weight=1;   server localhost:8899 weight=1;   }  server {      listen    8888; #8888为监听的端口,所以访问时就要通过 ip:8888来访问      server_name localhost;        location / {          proxy_connect_timeout  3;          proxy_send_timeout   30;          proxy_read_timeout   30;          proxy_pass http://localhost; #这里的名字要和上面upstream后面跟的名字一致      }        }}

View Code

 

③ cmd窗口中启动nginx  停止用nginx -s stop命令

 

④下载redis并在cmd中执行server.exe启动,可根据需要修改redis.conf中的端口 port 6379 ,默认端口为6379 停止使用 redis-cli shutdown

 

⑤ 新建maven工程并选择和本地java_home版本一致的jdk,将https://github.com/jcoleman/tomcat-redis-session-manager 代码加入依赖并打包

 

⑥ 修改tomcat/conf下context.windows 环境下nginx + tomcat群 + redis 实现session共享windows 环境下nginx + tomcat群 + redis 实现session共享

<??><!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at   http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--><!-- The contents of this file will be loaded for each web application --><Context>  <!-- Default set of monitored resources -->  <WatchedResource>WEB-INF/web.</WatchedResource>  <!-- Uncomment this to disable session persistence across Tomcat restarts -->  <!--  <Manager pathname="" />  -->  <!-- Uncomment this to enable Comet connection tacking (provides events     on session expiration as well as webapp lifecycle) -->  <!--  <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />  -->  <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"     host="localhost"     port="6379"     database="0"     maxInactiveInterval="60" /></Context>

View Code

 

⑦修改tomcat/conf/server.

<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm8899">

 

⑧ 拷贝第⑤步打好的jar包以及依赖的jedis-2.7.2.jar commons-pool2-2.4.1.jar到每个tomcat的lib目录下

 

⑨ 先启动redis,再启动所有tomcat,最后启动nginx 输入nginx监听的端口进行访问,结果如下:

最开始没有输入session信息时,多次刷新将显示访问到了两个不同的tomcat

windows 环境下nginx + tomcat群 + redis 实现session共享      windows 环境下nginx + tomcat群 + redis 实现session共享

 

 

在输入框中输入信息并保存,再不断刷新

windows 环境下nginx + tomcat群 + redis 实现session共享   windows 环境下nginx + tomcat群 + redis 实现session共享

 

 

 

在执行过程中,也会看见redis控制台不停的有相应信息输出

windows 环境下nginx + tomcat群 + redis 实现session共享

 

后期我会将所有的实例代码及配置文件全部放到我的网盘,欢迎下载验证,相互交流

 下载地址: http://pan.baidu.com/s/1pK7GIbp




原标题:windows 环境下nginx + tomcat群 + redis 实现session共享

关键词:Windows

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

英国税务:https://www.goluckyvip.com/tag/3934.html
鹏程David:https://www.goluckyvip.com/tag/39340.html
鹏翔海外仓:https://www.goluckyvip.com/tag/39341.html
捧油人:https://www.goluckyvip.com/tag/39342.html
批发B2B:https://www.goluckyvip.com/tag/39343.html
批发户PFHOO:https://www.goluckyvip.com/tag/39344.html
加拿大本地账户开立是否需要加拿大身份? :https://www.kjdsnews.com/a/1842166.html
加拿大本地账户开立是否需要加拿大身份? :https://www.xlkjsw.com/news/92266.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流