多个tomcat服务,使用nginx负载均衡
同一个服务器配置,多个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;
}
- 大小: 37.7 KB
- 大小: 19.9 KB
- 大小: 19.3 KB
- 大小: 22.6 KB
- 大小: 52.3 KB
- 大小: 35.2 KB
- 大小: 23.8 KB
- 大小: 25 KB
- 大小: 9.4 KB
- 大小: 14.1 KB
- 大小: 139 KB
- 大小: 56.2 KB
- 大小: 26.4 KB
- 大小: 67.9 KB
分享到:
相关推荐
session共享
参考的官网上的sessions共享,调整对高版本的framework的适应,采用vs2015 。可直接采用生成的cmodel
解决springboot实现跨域session共享问题,防止sql注入。可以更有效的解决token问题,欢迎下载,有问题可以再评论下方留言,及时解答!!加群:687942640
更改配置文件,实现session的跨域共享,记住登录的状态
Spring Boot 使用 Redis 进行配置 Session 共享(Ajax 跨域) 源码下载。
iframe 跨域访问session问题解决方法
主要介绍了ThinkPHP框架实现session跨域问题的解决方法,需要的朋友可以参考下
NULL 博文链接:https://justcoding.iteye.com/blog/747398
该文件可以通过代码实例,让你清楚的理解session和cookie的意思,当你明白了这点,你就可以设计出来单点登录功能,同一账号在同一时间只能登录一次功能。同时你可以通过ie、firefox去测试你对...在此共享,你值的拥有
在前后端集成测试的时候,就遇到了一些web安全相关的问题,cors跨域资源共享就是其中之一。 cors问题介绍 跨域资源共享(CORS) 是一种机制,它使用额外的 HTTP 头来告诉浏览器 让运行在一个 origin (domain) 上的Web...
主要介绍了PHP实现cookie跨域session共享的方法,结合实例形式分析了php操作cookie的有效期、跨域、session存储等相关操作技巧,需要的朋友可以参考下
通过spring-session 和redis 达到跨域同应用时 session 统一存储。在应用中实现单点登录操作
高并学习路线,maven、dubbo框架、nosql专题、消息中间件activeMQ、session跨域共享、自动化部署(png)
遇到了HTTP、HTTPS协议下session共享解决cookie失效的问题,这里提供一个临时解决办法。 实现原理:把session id设置到本地的cookie。 如下: 复制代码 代码如下: $currentSessionID = session_id(); session_id($...
session跨域共享 JAVANIO原理详解 高并发数据库(Mysql数据库性能优化) 软件质量管控 企业常用框架springMVC基于注解+xml配置方式实现链接 WEB服务器优化之Tomcat7性能调优 JVM概述 Java开发技术之(项目工程的日志...
主要介绍了PHP简单实现HTTP和HTTPS跨域共享session解决办法,本文讲解的方法相对简单,需要的朋友可以参考下
NULL 博文链接:https://pengfeifei26.iteye.com/blog/2173052
session跨域共享及企业级单点登录解决方案实战 分布式事务解决方案实战 高并发下的服务降级、限流实战 基于分布式架构下分布式锁的解决方案实战 分布式架构实现分布式定时调度 分布式架构-中间件 分布式消息...
解决跨子域网站之间的SESSION取值问题,简单实现,只需要改改配置文件就OK.