windows 环境下nginx + tomcat群 + redis 实现session共享
nginx作为负载均衡根据定义将不同的用户请求分发到不同的服务器,同时也解决了因单点部署服务器故障导致的整个应用不能访问的问题
在加入nginx之后,如果多个服务器中的一个或多个(不是全部)发生故障,均不影响用户的正常使用,会将用户请求分发到可以提供服务的服务器上
本节实例仅实现 一个nginx + 2个tomcat + redis实现java web应用的自由切换,并保持用户会话信息,对前端用户完全不可见。
会话的共享是为了保证用户在首次登录系统后已经后续的会话不会因为nginx重新分发到其他服务器而再次登录,达到会话信息共享
本例在windows下采用的是
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文件如下
#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后面跟的名字一致
} }
}
2016-06-06增加静态资源缓存设置和获取真实客户端
#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; #charset koi8-r;
#access_log logs/host.access.log main; location / {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://localhost; #这里的名字要和上面upstream后面跟的名字一致 root html;
index index.aspx index.html index.htm; #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} #静态资源缓存设置
location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
{
expires 30d;
root /nginx-1.9.3/html;#root:
break;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
③ 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 代码加入依赖并打包(打好的jar包在文章末尾有下载地址)
⑥ 修改tomcat/conf下context.xml为
<?xml version='1.0' encoding='utf-8'?>
<!--
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.xml</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>
⑦修改tomcat/conf/server.xml中engine信息 增加jvmRoute 标示
<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

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

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

后期我会将所有的实例代码及配置文件全部放到我的网盘,欢迎下载验证,相互交流
下载地址: http://pan.baidu.com/s/1pK7GIbp
下一篇文章我将以具体demo方式展示在linux环境下,采用keepalived+mysql主主双活, 在某个mysql遇见故障不能提供服务时进行mysql自动切换
mysql主从同步请参考我之前的文章:http://www.cnblogs.com/xiaochangwei/p/4824355.html
windows 环境下nginx + tomcat群 + redis 实现session共享的更多相关文章
- 搭建windows环境下(nginx+mysql+php)开发环境
搭建windows环境下(nginx+mysql+php)开发环境 1. 所需准备应用程序包 1.1 nginx 程序包nginx-1.0.4.zip或其他版本(下载地址: http ...
- Windows环境下部署Tomcat服务器图文教程
Tomcat是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. 本文将详细介绍在Windows环境下 ...
- JavaWeb入门——在Windows环境下安装Tomcat服务器
JavaWeb入门——在Windows环境下安装Tomcat服务器 摘要:本文主要学习如何在Windows环境中下载并安装Tomcat服务器. 下载 获取安装包 推荐去官网上下载Tomcat: htt ...
- 【数据库开发】windows环境下通过c++使用redis
1.Windows下Redis的安装使用 Redis是一个key-value存储系统.Redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起 ...
- Tomcat通过Redis实现session共享的完整部署记录
对于生产环境有了一定规模的tomcat集群业务来说,要实现session会话共享,比较稳妥的方式就是使用数据库持久化session.为什么要持久化session(共享session)呢?因为在客户端每 ...
- windows环境下配置php和redis
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 一.Window 下安装 下载地址:https://github.com/dmajkic/redis/downl ...
- nginx+tomcat集群+redis(memcache)session共享!
常用保持session的方式: 1.一些代理(比如nginxIP_hash) 1.使用数据库来存储Session 2.使用Cookie来存储Session ...
- windows环境下nginx的启动、停止
Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...
- 在windows环境下 nginx + .net core 3.1 实现反向代理和负载均衡
一.创建.net core web 应用 1.首先打开vs2019创建好.net core web应用,简单的注入IConfiguration 便于打印端口号展示效果. 1 private reado ...
随机推荐
- python自动化测试(2)-自动化基本技术原理
python自动化测试(2) 自动化基本技术原理 1 概述 在之前的文章里面提到过:做自动化的首要本领就是要会 透过现象看本质 ,落实到实际的IT工作中就是 透过界面看数据. 掌握上面的这样的本领 ...
- CI Weekly #10 | 2017 DevOps 趋势预测
2016 年的最后几个工作日,我们对 flow.ci Android & iOS 项目做了一些优化与修复: iOS 镜像 cocoapods 版本更新: fir iOS上传插件时间问题修复: ...
- C# 正则表达式大全
文章导读 正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET提供的Regex类实现了验证正则表达式的方法.Regex 类表示不可变(只读)的 ...
- C#中5步完成word文档打印的方法
在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选 ...
- 使用RequireJS并实现一个自己的模块加载器 (一)
RequireJS & SeaJS 在 模块化开发 开发以前,都是直接在页面上引入 script 标签来引用脚本的,当项目变得比较复杂,就会带来很多问题. JS项目中的依赖只有通过引入JS的顺 ...
- C++ 拷贝构造函数和赋值运算符
本文主要介绍了拷贝构造函数和赋值运算符的区别,以及在什么时候调用拷贝构造函数.什么情况下调用赋值运算符.最后,简单的分析了下深拷贝和浅拷贝的问题. 拷贝构造函数和赋值运算符 在默认情况下(用户没有定义 ...
- C++ 11 多线程--线程管理
说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段.并行是指两个或多个独立的操作同时进行.注意这里是同时进行,区别于并发,在一个时间段内执行多个操作.在单核时代,多个线程是并 ...
- 端盘子的服务生到月薪一万五的IT精英,你能相信吗
一直以来,我都觉得自己不是一个有故事的人. 以前的我,是个乖宝宝,对父母言听计从,特别内向,甚至一度感觉到自卑.不上学之后,我干过送货员,去工地除泥搬砖,当过油漆工,去过工厂,还去饭店当过端盘子的服务 ...
- 关于sqlmap的使用
好记性不如烂笔头,记录一下. 带cookie的注入 python sqlmap.py -u "http://www.xxx.com?id=1" --cookie="coo ...
- Ubuntu下利用Mono,Jexus搭建Asp.Net(MVC) Web服务器
最近在Ubuntu上搭建了Asp.Net的Web服务器,其中遇到很多问题,整理一下思路,以备后用. 搭建环境以及配套软件 Ubuntu: 11.10 Mono:3.0.6 下载地址(http://do ...