同一个服务器配置,多个tomcat:
package org.xdemo.example.SpringActivemq.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * @作者hailong * @日期 2017-2-2上午10:54:11 * @描述 测试 */ @Controller @RequestMapping("/sessonShare") public class SessionShareController { /** * 设定session值 * localhost:8082/SpringExample/sessonShare/set?message=3 * * @param message * @return String */ @RequestMapping("set") @ResponseBody public String set(@RequestParam("message")String message, HttpSession session, HttpServletRequest request, HttpServletResponse response){ HttpSession session2 = request.getSession(); session2.setAttribute("username", message); return "username=" + message; } /** * 取得session值 * @param message * @return String */ @RequestMapping("get") @ResponseBody public String get(HttpSession session, HttpServletRequest request, HttpServletResponse response){ HttpSession session2 = request.getSession(); return session2.getAttribute("username") +""; } }
此时是不能共享session的。
解决方案一:
在nginx-config配置文件加入ip_hash
就可以共享了
解决方案二:
把session放jvm里,因为nginxB是对jvm直接交流的。所以在nginxB可以配置。
还要配置tomcat下setting.xml文件。
<Engine name="Catalina" defaultHost="localhost">
解决方案三:使用redis
http://blog.csdn.net/patrickyoung6625/article/details/45694157
把session数据保存到内存以外的一个统一的地方,例如Memcached/Redis等数据库中
关于redis在centos服务器上安装:http://572327713.iteye.com/admin/blogs/2346196
下载maven_jar去aliyun速度快:
http://maven.aliyun.com/nexus/#nexus-search;quick~spring session
redis desktop manager管理工具:
http://download.csdn.net/download/li295214001/9398955
spring配置参考:
http://www.cnblogs.com/qlong8807/p/5557271.html
spring.xml文件配置(非spring_mvc的xml文件):
<!-- 20170222 --> <!-- redis spring-session --> <!-- 使用spring session托管session,将session放入redis中 --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" > <property name="maxInactiveIntervalInSeconds" value="600" /> </bean> <!-- spring-data-redis --> <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- redis 配置 --> <property name="hostName" value="192.168.92.129"/> <property name="port" value="6379" /> </bean>
web.xml配置:
<!-- 分布式Session共享Filter --> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
pom.xml配置:
<!-- 集群中,session共享 ,可以完全脱离spring去用--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.3.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.4.1.RELEASE</version> </dependency> <!--redis客户端--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency>
注意:jedis用2.8以上版本会出其他问题。
以上配置好后,放在不同tomcat下运行,放在同一个服务器tomcat端口不同,配置到nginx软负载,他们的session是共享的,可以下载redis桌面管理工具进行查看。
关于nginx配置:
/** * 设定与取得session值 * * @param message * @return String * @throws UnknownHostException */ @RequestMapping("gsSession") @ResponseBody public String gsSession(@RequestParam("message")String message, @RequestParam("action")String action, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws UnknownHostException{ HttpSession session2 = request.getSession(); String addr = InetAddress.getLocalHost().getHostAddress(); String hostName = InetAddress.getLocalHost().getHostName(); String message2 = "addr:"+ addr +" hostName: " + hostName; if("set".equals(action)){ //session的name是username session2.setAttribute("username", message); System.out.println("set:" + session.getId()); } else if("get".equals(action)){ message2 =(String)session2.getAttribute("username"); System.out.println("get:" + session.getId()); } return message2; }