这篇文章主要介绍了nginx session共享问题解决方案解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
nginx解决session共享问题:
1.nginx或者haproxy做的负载均衡,用nginx做的负载均衡可以添加ip_hash这个配置;用haproxy做的负载均衡可以用balance source这个配置,从而使用一个ip的请求发到同一个服务器;
2.利用数据库同步session;
3.利用cookie同步session数据,但是安全性差,http请求都需要带参增加了带宽消耗;
4.tomcat配置session共享;
5利用session集群存放redis;
1:创建一个工程,启动两个tomcat
2:编写一个servlet测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.zn.servlet; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet ( "/nginxsessionservlet" ) public class sessionipservlet extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { system.out.println( "当前请求端口:" +request.getlocalport()); string action=request.getparameter( "action" ); //向session中存放一个数据 if (action.equals( "setsession" )){ request.getsession().setattribute( "username" , "zhangsan" ); } else if (action.equals( "getsession" )){ response.getwriter().write((string)request.getsession().getattribute( "username" )); } } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request,response); } } |
3、没有nginx的访问效果展示
分别访问8080和8081
4.配置nginx.conf文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
upstream myserver{ ip_hash; server 127.0.0.1:8080; server 127.0.0.1:8081; } server{ listen 81; server_name www.bproject.com; location / { root html; proxy_pass http: //myserver ; index index.html index.htm; } } |
5.再次访问
方法二、利用spring-session+redis实现session共享
1:导入依赖
1
2
3
4
5
6
7
8
9
10
11
12
|
<!--spring boot 与redis应用基本环境配置 --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis --> < dependency > < groupid >org.springframework.boot</ groupid > < artifactid >spring-boot-starter-redis</ artifactid > </ dependency > <!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动spring boot会报错 --> < dependency > < groupid >org.springframework.session</ groupid > < artifactid >spring-session-data-redis</ artifactid > </ dependency > |
2:创建controller测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@restcontroller public class sessioncontroller { @requestmapping ( "/setsession" ) public string setsession(httpservletresponse response, httpservletrequest request) throws ioexception { request.getsession().setattribute( "username" , "wang" ); return "success" ; } @requestmapping ( "/getsession" ) public string getsession(httpservletrequest request,httpservletresponse response){ string username = (string) request.getsession().getattribute( "username" ); return username; } } |
3:application.properties文件
1
2
3
4
5
|
server.port=8082 #server.port=8083 #redis配置 spring.redis.password: wang2003 |
4:启动项目测试
结论:该方案配置简单,数据安全且稳定,效率高,被普遍使用;
注意:在redis中删除这个数据包,8082和8083端口都get不到session了,说明了session没有存在在jvm中,而是转存在redis中;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/lowerma/p/12300348.html