本次开发环境:idea2016.3.4 +jdk1.8+maven3.3.9+redis+springboot+jedis
本文中的项目使用Maven来管理项目依赖,使用Spring Session和Redis的组合来代替原有的HttpSession实现Session在不同项目之间的共享
项目结构:
构建Spring Boot
pom文件如下
4.0.0
com.cky.sessionshare
spring-session-share
jar
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.3.1.RELEASE
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
Application.java
实现Spring Boot的启动main函数
@SpringBootApplication
public class Application {
// 自动配置Spring框架
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试
测试代码
创建一个类HelloWorldController用于测试
@RestController
public class HelloWorldController {
@RequestMapping("/index/{name}")
@ResponseBody
public String index(@PathVariable String name) {
if (null == name) {
name = "boy";
}
return "hello world " + name;
}
}
运行Application.java来启动Spring Boot,访问”http://localhost:8080/index/陈冠希”出现以下页面,说明Spring Boot部署成功
加入Spring Session框架
pom.xml
引入Spring Session和Redis需要的依赖
org.springframework.boot
spring-boot-starter-redis
org.springframework.session
spring-session-data-redis
Spring Session配置
创建一个Spring配置,用于创建一个支持Spring Session的Servlet Filter来代替原有的HttpSession的实现。
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800)
public class SessionConfig {
@Value("${redis.hostname:localhost}")
String HostName;
@Value("${redis.port:6379}")
int Port;
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connection = new JedisConnectionFactory();
return connection;
}
}
JedisConnectionFactory
默认连接端口:6379
默认连接地址:localhost
需要修改则可以使用JedisConnectionFactory的setPort()方法和setHostName()方法来修改默认的端口和连接地址
这里可以引入配置文件使Redis的配置更加灵活
创建配置文件application.properties
redis.homename = localhost
redis.port = 6379
在类SessionConfig中引入配置文件的值
修改后的SessionConfig类代码如下:
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800)
public class SessionConfig {
@Value("${redis.hostname:localhost}")
String HostName;
@Value("${redis.port:6379}")
int Port;
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connection = new JedisConnectionFactory();
connection.setPort(Port);
connection.setHostName(HostName);
return connection;
}
}
加载Spring Session配置
加载Spring Session配置,使得Servlet容器在每一次请求时都使用我们的springSessionRepositoryFilter过滤器。
public class SessionInitializer extends AbstractHttpSessionApplicationInitializer{
public SessionInitializer (){
super(SessionConfig.class);
}
}
测试
以上已经完成了Spring Boot + Spring Session +Redis的配置
测试是否可以在不同的容器(e.g. Tomcat),不同的项目中都访问到同一个Session
运行Redis
拷贝一个项目
项目结构与主项目相同
修改容器端口
修改Spring Boot中自带Tomcat的接口
在application.properties中加入server.port=8090
实现Session的读写
添加测试方法
主项目
在主项目中的类HelloWorldController中添加一个TestSession方法
用于保存和读取Session
修改后的代码如下
@RestController
public class HelloWorldController {
@RequestMapping("/index/{name}")
@ResponseBody
public String index(@PathVariable String name) {
if (null == name) {
name = "boy";
}
return "hello world " + name;
}
@RequestMapping("/tsession/{age}")
@ResponseBody
public String TestSession(HttpServletRequest req, HttpServletResponse resp, @PathVariable String age){
req.getSession().setAttribute("age", age);
String a = (String) req.getSession().getAttribute("age");
return a;
}
}
测试项目
为了测试是否可以读取Session,测试项目的Session不做保存操作
修改测试项目的HelloWorldController类中的TestSession方法
修改后的代码为
@RestController
public class WelcomeController {
@RequestMapping("/welcome/{name}")
@ResponseBody
public String index(@PathVariable String name) {
if (null == name) {
name = "edison";
}
return "welcome " + name;
}
@RequestMapping("/tsession/{age}")
@ResponseBody
public String TestSession(HttpServletRequest req, HttpServletResponse resp, @PathVariable String age){
String a = (String) req.getSession().getAttribute("age");
return a;
}
}
开始测试
分别运行主项目和测试项目的Application.java文件
验证
再登录”http://localhost:8090/tsession/88“,测试是否可以读取到之前的信息:若结果如下图,这表示测试成功:
这时用redis desktop manager查看redis发现数据
用谷歌查看刚才传递的给服务的cookie
优化
若每一次创建项目都要写一次的Session的配置,那么代码的复用性太差,那么不如把Session的配置单独打包出来,之后只需要在项目中加入一个依赖就可以实现Spring Session的功能。
项目结构
pom文件如下
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.springboot.sessionshare
springbootsessionshare
jar
0.0.1-SNAPSHOT
A Camel Route
org.springframework.boot
spring-boot-starter-parent
1.3.1.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-redis
org.springframework.session
spring-session-data-redis
配置类SessionConfig和加载类SessionInitializer的代码与之前没有变化
Maven install
由于项目依赖是由Maven来管理的,那么使用Maven install将项目安装到Maven的本地仓库当中
测试
创建测试项目
pom.xml
先获取去需要依赖的项目的Maven位置,位置在安装到本地仓库的项目的pom.xml文件中获取
将位置加入到测试项目中
设置端口
server.port=8050
其他测试文件
Application.java文件和HelloWorldController文件代码与之前的一致,以下就不贴出代码了
开始测试
运行Redis-运行测试项目中的Application.java文件-访问”http://localhost:8050/tsession/100”
若效果与下图效果一致,则创建成功