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

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

5.10.1. Redis

5.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
5.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
5.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	}}

5.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

5.10.3. JDBC

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

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

你可能感兴趣的文章
KindEditor
查看>>
航拍去浆
查看>>
Linux-shell 练习题(一)
查看>>
12.01个人计划
查看>>
Dynamics CRM ISV文件夹禁用后的解决方案
查看>>
ASP.NET中IsPostBack详解
查看>>
Apple开启双重认证过程
查看>>
西安中科创达面试(java方向)
查看>>
es6+node
查看>>
常见的装包的三种宝,包 bao-devel bao-utils bao-agent ,包 开发包 工具包 客户端...
查看>>
Linux最大文件打开数
查看>>
java继承和多态
查看>>
django加载模板文件
查看>>
nohup—后端守护进程
查看>>
jsp,2016.11.28
查看>>
C# 将数字时间转化为特定格式字符串
查看>>
HDU_1285_拓扑排序(优先队列)
查看>>
leetcode_951. Flip Equivalent Binary Trees_二叉树遍历
查看>>
3.3绿盟面试
查看>>
Android jni中数组参数的传递方式
查看>>