1.Spring Session MongoDB
1.1 什么是 Spring Session MongoDB
Spring Session MongoDB 是 Spring Session 的二级项目。其功能与 Spring Session 是相同的。Spring Session MongoDB 提供了一个 API 和实现,用于通过利用 Spring Data MongoDB 来管理存储在 MongoDB 中的用户会话信息。
1.2.他与 Spring Session 的区别
Spring Session 与 Spring Session MongoDB 的作用是相同的。都是来解决 Session 共享问题。不同的是 Spring Session 默认的是依赖于 Redis 作为数据缓存平台,而 Spring Session MongoDB 是依赖于 MongoDB 来作为数据缓存平台的。
1.3 安装 MongoDB
1.下载 MongoDB
mongodb-linux-x86_64-4.0.9.tgz
2.解压 tgz 文件
[root@localhost temp]# tar -zxf mongodb-linux-x86_64-4.0.9.tgz
3.创建数据库目录
[root@localhost etc]# mkdir -p data/db
4.创建日志文件
[root@localhost etc]# touch mongodb.log
5.创建配置文件
[root@localhost etc]# vim mongodb.conf
6.启动 MongoDB
[root@localhost bin]# ./mongod --cofnig /usr/local/mongodb/etc/mongodb.conf
7.创建 dqcgm库
> use dqcgm
2.搭建案例环境
2.1 技术版本
JDK:1.8
Spring Boot:2.1.6RELEASE
Spring Session MongoDB:Bean-SR3
2.2 创建项目
2.3 修改 POM 文件添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dqcgm</groupId> <artifactId>session_mongodb</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>session_mongo1</module> <module>session_mongo2</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-bom</artifactId> <version>Bean-SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-mongodb</artifactId> </dependency> </dependencies> </project>
2.4 添加配置文件
Session_monog1
#配置服务的端口 server: port: 8080 #配置 MongoDB 的链接信息 spring: data: mongodb: host: 192.168.70.156 port: 27017 database: dqcgm session: store-type: mongodb
Session_mongo2
#配置服务的端口 server: port: 8081 #配置 MongoDB 的链接信息 spring: data: mongodb: host: 192.168.70.156 port: 27017 database: dqcgm session: store-type: mongodb
2.5 创建启动类
@SpringBootApplication @EnableMongoHttpSession public class Mongo1Application { public static void main(String[] args){ SpringApplication.run(Mongo1Application.class,args); } }
Session_mongo2
@SpringBootApplication @EnableMongoHttpSession public class Mongo2Application { public static void main(String[] args){ SpringApplication.run(Mongo2Application.class,args); } }
3.编写测试代码
3.1 Session_mongo1
@RestController @RequestMapping("/service1") public class WebController { //将数据存放到 HttpSession 中 @RequestMapping("/setMsg") public String getMsg(HttpSession session,String msg){ session.setAttribute("msg",msg); return "ok"; } }
3.2 Session_mongo2
@RestController @RequestMapping("/service2") public class WebController { //获取 HttpSession 中的数据 @RequestMapping("/getMsg") public String getMsg(HttpSession session){ String msg = (String) session.getAttribute("msg"); return msg; } }
3.3 修改 MongoDB 的配置文件添加 bind_ip
dbpath=/usr/local/mongodb/data/db logpath=/usr/local/mongodb/log/mongodb.log port=27017 fork=true bind_ip=0.0.0.0
3.4 测试
4.共享自定义对象
4.1 创建 Users 类
public class Users implements Serializable { private String username; private String userpwd; public String getUsername() { return username; } public String getUserpwd() { return userpwd; } public void setUsername(String username) { this.username = username; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } }
4.2 session_mongo1
@RestController @RequestMapping("/service1") public class WebController { //将数据存放到 HttpSession 中 @RequestMapping("/setMsg") public String getMsg(HttpSession session,String msg){ session.setAttribute("msg",msg); return "ok"; } //获取数据封装 Users @RequestMapping("/setUsers") public String setUsers(HttpSession session, Users users){ session.setAttribute("users",users); return "ok"; } }
4.3 session_mongo2
@RestController @RequestMapping("/service2") public class WebController { //获取 HttpSession 中的数据 @RequestMapping("/getMsg") public String getMsg(HttpSession session){ String msg = (String) session.getAttribute("msg"); return msg; } //从 HttpSession 中获取 Users @RequestMapping("/getUsers") public Users getUsers(HttpSession session){ Users users = (Users) session.getAttribute("users"); return users; } }
4.4 测试
5.Spring Session MongoDB 的存结构
{ "_id" : "fe3f6cea-c3e2-426b-a47a-cf21c5b848cb", "created" : ISODate("2019-07-12T12:46:48.916Z"), "accessed" : ISODate("2019-07-12T12:46:48.919Z"), "interval" : "PT30M", "principal" : null, "expireAt" : ISODate("2019-07-12T13:16:48.919Z"), "attr" : BinData(0,"rO0ABXNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9h ZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAABdAAFdXNlcnN zcgAcY29tLmJqc3h0Lm1vbmdvLmRvbWFpbi5Vc2Vyc5EM02IkljhzAgACTAAIdXNlcm5hbWV0ABJMamF2YS9sYW5nL1N0cmluZztMAAd1c2VycHdkcQB+AAR4cHQABWFkbWludAAF YWRtaW54") }
6.设置 Session 失效时间
@SpringBootApplication @EnableMongoHttpSession(maxInactiveIntervalInSeconds=10) public class Mongo1Application { public static void main(String[] args){ SpringApplication.run(Mongo1Application.class,args); } }
7.@EnableMongoHttpSession 注解讲解
maxInactiveIntervalInSeconds:设置 Session 失效时间
collectionName:设置 MongoDB 的 Collections 的名称
8.更换 Spring Session MongoDB 的序列化器
Spring Session MongoDB 默认使用的是 JDK 序列化器
8.1 创建配置类添加 Jackson 序列化器
//更换 Spring Session MongoDB 的序列化器 @Configuration public class SessionMongoConfig { @Bean JacksonMongoSessionConverter mongoSessionConverter() { return new JacksonMongoSessionConverter(); } }
8.2 修改实体类,添加@JsonAutoDetect 注解
@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY) public class Users implements Serializable { private String username; private String userpwd; public String getUsername() { return username; } public String getUserpwd() { return userpwd; } public void setUsername(String username) { this.username = username; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } }
8.3 测试