两份properties配置, 仅端口不同
配置文件添加redis 链接
application-9001.properties | application-9002.properties | ||
server.port=9001 server.servlet.context-path=/jpa #server.servlet.session.cookie.domain= server.servlet.session.cookie.name=sisisession #server.servlet.session.cookie.path=/sessionpath1 #server.servlet.session.cookie.http-only=true spring.redis.database=11 spring.redis.host=47.96.181.192 spring.redis.port=6379 spring.redis.password=123456 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.min-idle=0 spring.redis.timeout=5000 spring.session.store-type=redis |
server.port=9002 server.servlet.context-path=/jpa #server.servlet.session.cookie.domain= server.servlet.session.cookie.name=sisisession #server.servlet.session.cookie.path=/sessionpath1 #server.servlet.session.cookie.http-only=true spring.redis.database=11 spring.redis.host=47.96.181.192 spring.redis.port=6379 spring.redis.password=123456 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.min-idle=0 spring.redis.timeout=5000 spring.session.store-type=redis |
||
redis session 配置
开启redis session
RedisSessionConfig.java
package com.bobo.code.config; import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @Configuration @EnableRedisHttpSession public class RedisSessionConfig { }
在使用了了 EnableRedisHttpSession 之后 session的getId()仍是一致的, 但是session.hashCode()已不一致, 是因为每次从redis中反序列化后重新生成了session对象,所以不一致,但是对getId()没有任何影响.
编写SessionController
SessionController.java
package com.bobo.code.web.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; //参考链接 https://blog.csdn.net/zhaokejin521/article/details/104243739 //进阶 spring-session-data-redis的实现原理剖析==>https://www.pianshen.com/article/6802714230/ @RestController public class SessionController { public static final String KEY = "key"; //http://localhost:9001/jpa/setsession //http://localhost:9002/jpa/setsession @RequestMapping(value = "/setsession") public Object setSession(@RequestParam(required = false) String value, HttpSession session) { session.setAttribute(KEY, value); return session.getId(); } //http://localhost:9001/jpa/getsession //http://localhost:9002/jpa/getsession @RequestMapping(value = "/getsession") public Object getSession(HttpServletRequest request, HttpSession session) { Map<String, Object> result = new HashMap<>(); System.err.println("-----------------------------------------"); String cookieInfoStr = request.getHeader("Cookie"); System.err.println("cookie info " + cookieInfoStr); System.err.println("session.isNew() " + session.isNew()); System.err.println("session.getId() " + session.getId()); System.err.println("session.hashCode() " + session.hashCode()); System.err.println("session.getLastAccessedTime() " + session.getLastAccessedTime()); System.err.println("session.getMaxInactiveInterval() " + session.getMaxInactiveInterval()); System.err.println("-----------------------------------------"); Object value = session.getAttribute(KEY); result.put("sessionId", session.getId()); result.put("value", value); return result; } }
启动9001,9002端口的两台服务
-
访问9001端口服务以设置session
http://localhost:9001/jpa/setsession?key=name&value=bobo
访问9001端口服务以获取session信息
http://localhost:9001/jpa/getsession?key=name
访问9002端口服务以获取session信息
http://localhost:9002/jpa/getsession?key=name
结论
最终会发现,不同端口的两台服务通过getsesion接口获取到的session完全一致