欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-13 10:37:34
...

参考: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内容。
基于Nginx分布式Tomcat+Redis 实现Session共享 for window
            
    
    博客分类: TomcatRedis  

其次,采用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间共享;

 

 

 

 

 

 

 

 

  • 基于Nginx分布式Tomcat+Redis 实现Session共享 for window
            
    
    博客分类: TomcatRedis  
  • 大小: 12.5 KB