spring boot + redis 实现session共享
2016-05-28 15:01
1216 查看
这次带来的是spring boot + redis 实现session共享的教程。
在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring session支持,配置如下:
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}
而@EnableRedisHttpSession这个注解是由spring-session-data-redis提供的,所以在pom.xml文件中添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
接下来,则需要在application.properties中配置redis服务器的位置了,在这里,我们就用本机:
spring.redis.host=localhost
spring.redis.port=6379
这样以来,最简单的spring boot + redis实现session共享就完成了,下面进行下测试。
首先我们开启两个tomcat服务,端口分别为8080和9090,在application.properties中进行设置【下载地址】
:
server.port=8080
接下来定义一个Controller:
@RestController
@RequestMapping(value = "/admin/v1")
public class QuickRun {
@RequestMapping(value = "/first", method = RequestMethod.GET)
public Map<String, Object> firstResp (HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
request.getSession().setAttribute("request Url", request.getRequestURL());
map.put("request Url", request.getRequestURL());
return map;
}
@RequestMapping(value = "/sessions", method = RequestMethod.GET)
public Object sessions (HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
map.put("sessionId", request.getSession().getId());
map.put("message", request.getSession().getAttribute("map"));
return map;
}
}
启动之后进行访问测试,首先访问8080端口的tomcat,返回 获取【下载地址】
:
{"request Url":"http://localhost:8080/admin/v1/first"}
接着,我们访问8080端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}
最后,再访问9090端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}
可见,8080与9090两个服务器返回结果一样,实现了session的共享
如果此时再访问9090端口的first的话,首先返回:
{"request Url":"http://localhost:9090/admin/v1/first"}
而两个服务器的sessions都是返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/first"}
通过spring boot + redis来实现session的共享非常简单,而且用处也极大,配合nginx进行负载均衡,便能实现分布式的应用了。
本次的redis并没有进行主从、读写分离等等配置(_(:з」∠)_其实是博主懒,还没尝试过.......)
而且,nginx的单点故障也是我们应用的障碍......以后可能会有对此次博客的改进版本,比如使用zookeeper进行负载均衡,敬请期待。
在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring session支持,配置如下:
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}
而@EnableRedisHttpSession这个注解是由spring-session-data-redis提供的,所以在pom.xml文件中添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
接下来,则需要在application.properties中配置redis服务器的位置了,在这里,我们就用本机:
spring.redis.host=localhost
spring.redis.port=6379
这样以来,最简单的spring boot + redis实现session共享就完成了,下面进行下测试。
首先我们开启两个tomcat服务,端口分别为8080和9090,在application.properties中进行设置【下载地址】
:
server.port=8080
接下来定义一个Controller:
@RestController
@RequestMapping(value = "/admin/v1")
public class QuickRun {
@RequestMapping(value = "/first", method = RequestMethod.GET)
public Map<String, Object> firstResp (HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
request.getSession().setAttribute("request Url", request.getRequestURL());
map.put("request Url", request.getRequestURL());
return map;
}
@RequestMapping(value = "/sessions", method = RequestMethod.GET)
public Object sessions (HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
map.put("sessionId", request.getSession().getId());
map.put("message", request.getSession().getAttribute("map"));
return map;
}
}
启动之后进行访问测试,首先访问8080端口的tomcat,返回 获取【下载地址】
:
{"request Url":"http://localhost:8080/admin/v1/first"}
接着,我们访问8080端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}
最后,再访问9090端口的sessions,返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/first"}
可见,8080与9090两个服务器返回结果一样,实现了session的共享
如果此时再访问9090端口的first的话,首先返回:
{"request Url":"http://localhost:9090/admin/v1/first"}
而两个服务器的sessions都是返回:
{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/first"}
通过spring boot + redis来实现session的共享非常简单,而且用处也极大,配合nginx进行负载均衡,便能实现分布式的应用了。
本次的redis并没有进行主从、读写分离等等配置(_(:з」∠)_其实是博主懒,还没尝试过.......)
而且,nginx的单点故障也是我们应用的障碍......以后可能会有对此次博客的改进版本,比如使用zookeeper进行负载均衡,敬请期待。
相关文章推荐
- redis安装问题小结
- kindeditor 批量上传 上传失败 thinkphp swfupload session
- 使用 Redis 和 Python 构建一个共享单车的应用程序
- Redis偶发连接失败案例实战记录
- 杰奇登录后的东西都是在session里面的
- Redis中实现查找某个值的范围
- win 7 安装redis服务【笔记】
- redis的hGetAll函数的性能问题(记Redis那坑人的HGETALL)
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- Redis实现信息已读未读状态提示
- ASP中SESSION无法保存问题的解决办法
- redis中修改配置文件中的端口号 密码方法
- 在Ruby on Rails上使用Redis Store的方法
- Redis和Memcache的区别总结
- Oracle中的Connect/session和process的区别及关系介绍