博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.10. Spring boot with Session share
阅读量:5910 次
发布时间:2019-06-19

本文共 7591 字,大约阅读时间需要 25 分钟。

2.10.1. Redis

2.10.1.1. Maven

增加下面代码到pom.xml

org.springframework.boot
spring-boot-starter-data-redis
org.springframework.session
spring-session-data-redis

pom.xml 文件

4.0.0
cn.netkiller
deploy
0.0.1-SNAPSHOT
jar
deploy.netkiller.cn
Deploy project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.session
spring-session-data-redis
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-websocket
org.webjars
webjars-locator
org.webjars
sockjs-client
1.0.2
org.webjars
stomp-websocket
2.3.3
org.webjars
bootstrap
3.3.7
org.webjars
jquery
3.1.0
org.springframework.boot
spring-boot-starter-test
test
org.apache.tomcat.embed
tomcat-embed-jasper
provided
javax.servlet
jstl
mysql
mysql-connector-java
junit
junit
test
org.springframework.boot
spring-boot-maven-plugin
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2.10.1.2. application.properties

spring.session.store-type=redis 将Session 存储在Redis中

spring.redis.database=0spring.redis.host=192.168.4.1spring.redis.port=6379#spring.redis.password=spring.redis.pool.max-active=8spring.redis.pool.max-wait=30spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.timeout=10spring.session.store-type=redis
2.10.1.3. Application
package cn.netkiller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@SpringBootApplication@EnableAutoConfiguration@ComponentScan@EnableMongoRepositories@EnableJpaRepositories@EnableSchedulingpublic class Application {	public static void main(String[] args) {		SpringApplication.run(Application.class, args);	}}

RedisHttpSessionConfig.java

package cn.netkiller.config;import org.springframework.context.annotation.Configuration;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@Configuration@EnableRedisHttpSessionpublic class RedisHttpSessionConfig {	public RedisHttpSessionConfig() {		// TODO Auto-generated constructor stub	}}

2.10.2. 测试 Session

package cn.netkiller.web;import java.util.Date;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class TestController {	public TestController() {		// TODO Auto-generated constructor stub	}	@RequestMapping("/session/set")	@ResponseBody	public String set(HttpSession session) {		String key = "test";		session.setAttribute(key, new Date());		return key;	}	@RequestMapping("/session/get")	@ResponseBody	public String get(HttpSession session) {		String value = (String) session.getAttribute("test").toString();		return value;	}}

keys spring:session:* 查看 Session Key

$ telnet 192.168.4.1 6379				Connecting to 192.168.4.1:6379...				Connection established.				To escape to local shell, press 'Ctrl+Alt+]'.				keys spring:session:*				*7				$68				spring:session:sessions:expires:a510f46f-0a2f-4649-af05-34bd750562c1				$40				spring:session:expirations:1476100200000				$40				spring:session:expirations:1476098400000				$60				spring:session:sessions:f6494a2f-591e-42ba-b381-ce2596f4046d				$60				spring:session:sessions:a510f46f-0a2f-4649-af05-34bd750562c1				$112				spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:user				$60				spring:session:sessions:627018c8-243e-43ac-87b9-fc07f130c899

2.10.3. JDBC

spring.session.store-type=jdbcspring.session.jdbc.table-name=SESSIONS

原文出处:Netkiller 系列 手札

本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
阿里云提示ecshop Discuz uc.key泄露导致代码注入漏洞修复
查看>>
关于STUN和NAT
查看>>
常见JVM内存异常分析
查看>>
针对不同浏览器内核css写法
查看>>
ubuntu下添加gimp的ppa
查看>>
使用Java8实现自己的个性化搜索引擎
查看>>
龙家贰少的MarkDown学习笔记
查看>>
查看端口占用命令
查看>>
arm9时钟及定时器
查看>>
vi 常用命令
查看>>
抓包工具Charles
查看>>
码云项目100,水一发
查看>>
CLRS 4.2 Exercises
查看>>
redis启动警告处理
查看>>
【python初级】010-构造方法,属性和迭代器
查看>>
Textillate.js – 实现动感的 CSS3 文本动画的简单插件(用法详情&只支持现代浏览)...
查看>>
项目 调dubbo接口 异常总结
查看>>
通过Gearman实现MySQL到Redis的数据复制
查看>>
基于swoole扩展的异步redis客户端
查看>>
仿webqq桌面–jquery desktop 2.0
查看>>