设为首页 收藏本站
查看: 859|回复: 0

[经验分享] 基于Nginx分布式Tomcat+Redis 实现Session共享 for window

[复制链接]

尚未签到

发表于 2016-12-21 08:17:54 | 显示全部楼层 |阅读模式
  参考:http://www.cnblogs.com/lengfo/p/4260363.html
  近期为CAS-SSO准备搭建Session共享,小编研究了下基于Nginx负载均衡,分布式Tomcat+Redis 存储Session,实现分布式Tomcat的Session共享,for window下~~~不要问我为什么window下(linux的资源太多了~~~)
  原理:
  首先,sessionID的产生,当用户发起请求时,web服务器将产生一个sessionID返回给用户,由浏览器cookie记录该sessionID,下次再请求,浏览器将cookie记录的sessionID一并发给web服务器,web服务器判断请求头中含有sessionID,则不再新建sessionID,也说明该用户已登录or已访问.........
  注:浏览器发送的请求中有个Host字段,标记请求的目标服务器,即使访问的应用是相同的,但由于不同的目标请求服务器,产生并返回的sessionID肯定不一样的....所以这里必须有负载均衡器,如Nginx or Haproxy来做转发,确保请求的目标服务器都指向负载均衡那台,然后通过cookie=sessionID,发送给web服务器,获取对应的session内容。
DSC0000.png

  其次,采用Redis做session缓存服务器,将原本tomcat自身存储的session,转移存储到redis缓存服务器上,实现分布式的tomcat共享session。
 

  1.下载Nginx for winow(linux的请无视)
  2.下载tomcat7
  3.下载redis for window64 只有64bit,不要问为什么没有32位(linux的请无视)
  4.下载tomcat7依赖redis 必须的jar tomcat-redis-session-manager-1.2-tomcat-7.jar、jedis-2.1.0.jar、commons-pool-1.6.jar 注意版本必须配对..jedis-2.1对应commons-pool-1.6;
  详见附件
  5.配置nginx.conf,红色部分为新增修改地方


#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
#采用轮询方式,配置对应的tomcat访问,“site”可以自定义
upstream site {  
server 10.89.113.112:8080 weight=1;
server 10.92.2.23:8091 weight=1;
}

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  logs/access.log  main;
sendfile        on;
#tcp_nopush     on;
#keepalive_timeout  0;
keepalive_timeout  65;
#gzip  on;

server {
listen       80;
#server_name 用于访问Nginx的url,可以用域名如“www.xxx.com” 自定义
server_name  10.89.113.184;
#charset koi8-r;
#access_log  logs/host.access.log  main;
#指定web上下文
location /web-one {
#root   html;
#index  index.html index.htm index.jsp;
#proxy_pass "site"必须同upstream 配置的名称一致
proxy_pass        http://site;
#proxy_set_header  X-Real-IP  $remote_addr;   
#client_max_body_size  100m;
}

#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
}

  2.配置redis.windows.conf ,搜索maxheap 并新增maxheap 1024000000,可以写bat启动脚本startup.bat

redis-server  redis.windows.conf
  3.新建web-app,修改index.jsp,新增打印sessionId,用于判断是否session共享成功

<html>
<body>
<h2>Hello World!! web-one-tomcat.1 </h2>
SessionID:<%=session.getId()%>

</body>
</html>

  4. 将tomcat7依赖的redis 相关的jar tomcat-redis-session-manager-1.2-tomcat-7.jar、jedis-2.1.0.jar、commons- pool-1.6.jar copyto “lib”文件夹下

  5.修改“conf”文件夹下的context.xml,将原先的<Manager>内容注释掉;新增以下内容

<!--redis config-->
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<!-- host: optional: defaults to "localhost" -->  
<!-- port: optional: defaults to "6379" -->  
<!-- database: optional: defaults to "0" -->  
<!-- maxInactiveInterval:optional: defaults to "60" (in seconds) -->  
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="10.89.113.112" <!-- host: optional: defaults to "localhost" -->  
port="6379" <!-- port: optional: defaults to "6379" -->  
database="0"<!-- database: optional: defaults to "0" -->  
maxInactiveInterval="180" /> <!-- maxInactiveInterval:optional: defaults to "60" (in seconds) -->  
  6.依次按照,启动redis、tocmat、nginx~~
  7.访问http://10.89.113.184/web-one <---nginx所配置的访问地址,将自动负载路由到不同的tomcat上,根据现实出相同的sessionID,实现session间共享;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-317103-1-1.html 上篇帖子: Redis入门很简单之六【Jedis常见操作】 下篇帖子: Redis官方文档(3)——从入门到精通(上)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表