java 编码 session共享_使用idea,springboot,springsession,redis实现分布式微服务的session 共享...

本次开发环境:idea2016.3.4 +jdk1.8+maven3.3.9+redis+springboot+jedis

本文中的项目使用Maven来管理项目依赖,使用Spring Session和Redis的组合来代替原有的HttpSession实现Session在不同项目之间的共享

项目结构:

7c196ec607d5650f62864b3c246d4763.png

构建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部署成功

7f37088e451f3d304e75de13a79932a2.png

加入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

拷贝一个项目

项目结构与主项目相同

cc294c4335dcab6a3e8f806b3b281a5d.png

修改容器端口

修改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文件

验证

abb5278e886904a1f2175302ec70bdad.png

再登录”http://localhost:8090/tsession/88“,测试是否可以读取到之前的信息:若结果如下图,这表示测试成功:

30de51071f849c10f933f606b818d965.png

这时用redis desktop manager查看redis发现数据

221a13ce7f6c008e93c0f8af10aeb119.png

用谷歌查看刚才传递的给服务的cookie

a4206ea55c60c7763c863bf4f0593f49.png

优化

若每一次创建项目都要写一次的Session的配置,那么代码的复用性太差,那么不如把Session的配置单独打包出来,之后只需要在项目中加入一个依赖就可以实现Spring Session的功能。

项目结构

6e46ced0d52fe5ad09eb1dcd0fdc56bf.png

2e5b0117e586d080ed8e27f5bf0a88e0.png

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的本地仓库当中

037ae17d7ea7dfa07e616c0bf3981bbc.png

测试

创建测试项目

pom.xml

先获取去需要依赖的项目的Maven位置,位置在安装到本地仓库的项目的pom.xml文件中获取

6c1b383eb12a9a24c9e7698530589732.png

将位置加入到测试项目中

a0eea6067729f884ad9237daa790ef0b.png

设置端口

server.port=8050

其他测试文件

Application.java文件和HelloWorldController文件代码与之前的一致,以下就不贴出代码了

开始测试

运行Redis-运行测试项目中的Application.java文件-访问”http://localhost:8050/tsession/100”

若效果与下图效果一致,则创建成功

f1e908a810a808425a951b48cbcb8f3a.png

62149534c84a603d8f69ab4321948aff.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值