文章目录
文章参考
Spring全家桶之SpringSession
Spring-Session实现session共享原理及解析
spring-session集成redis
如下图,创建2个项目,session-service1 和 session-service2
在这里插入图片描述
1. 父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>
<packaging>pom</packaging>
<modules>
<module>session-service1</module>
<module>session-service2</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<groupId>com.zzhua</groupId>
<artifactId>demo-spring-session</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Spring session data redis-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<!--<version>2.1.4.RELEASE</version>-->
</dependency>
<!--Lettuce是 一 个 基 于 Netty的 NIO方 式 处 理 Redis的 技 术 -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. session-service1
SpringSessionConfig
package com.zzhua.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* 1.需要两端都添加这个配置类
* 2.同时允许关联对象的添加
*
*/
@Configuration
@EnableRedisHttpSession
public class SpringSessionConfig {
/**
* SpringSession 中默认的序列化器为 jdk 序列化器,该序列化器效率低下,内存再用大。
* 我们可以根据自己的需要更换其他序列化器,如 GenericJackson2JsonRedisSerializer 序列化器。
* @return
*/
@Bean("springSessionDefaultRedisSerializer")
public RedisSerializer setSerializer(){
return new GenericJackson2JsonRedisSerializer();
}
}
WebController
package com.zzhua.controller;
import com.zzhua.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
@RequestMapping("service1")
public class WebController {
@RequestMapping("setMsg")
public String setMsg(HttpSession session) {
session.setAttribute("msg", "Hello SpringSession!");
return "ok";
}
@RequestMapping("addUser")
public String addUser(HttpSession session, User user) {
session.setAttribute("user", user);
return "addUser Success!!!";
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String username;
private String userpwd;
@Override
public String toString() {
return "Users [username=" + username + ", userpwd=" + userpwd + "]";
}
}
application.yml
spring:
application:
name: session-service1
redis:
#redis单机配置
host: 127.0.0.1
port: 6379
# 选择redis的数据库的分库
database: 5
#redis连接池配置
jedis:
pool:
max-idle: 10
min-idle: 5
max-active: 100
max-wait: 3000
timeout: 6005
server:
port: 8081
SessionService1App
@SpringBootApplication
public class SessionService1App {
public static void main(String[] args) {
SpringApplication.run(SessionService1App.class, args);
}
}
3. session-service2
SpringSessionConfig
package com.zzhua.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* 1.需要两端都添加这个配置类
* 2.同时允许关联对象的添加
*
*/
@Configuration
@EnableRedisHttpSession
public class SpringSessionConfig {
/**
* SpringSession 中默认的序列化器为 jdk 序列化器,该序列化器效率低下,内存再用大。
* 我们可以根据自己的需要更换其他序列化器,如 GenericJackson2JsonRedisSerializer 序列化器。
* @return
*/
@Bean("springSessionDefaultRedisSerializer")
public RedisSerializer setSerializer(){
return new GenericJackson2JsonRedisSerializer();
}
}
WebController
package com.zzhua.controller;
import com.zzhua.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
@RequestMapping("service2")
public class WebController {
@RequestMapping("getMsg")
public String getMsg(HttpSession session) {
String msg=(String) session.getAttribute("msg");
return msg;
}
@RequestMapping("getUser")
public User getUser(HttpSession session) {
User user=(User) session.getAttribute("user");
return user;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String username;
private String userpwd;
@Override
public String toString() {
return "Users [username=" + username + ", userpwd=" + userpwd + "]";
}
}
application.yml
spring:
application:
name: session-service2
redis:
#redis单机配置
host: 127.0.0.1
port: 6379
# 选择redis的数据库的分库
database: 5
#redis连接池配置
jedis:
pool:
max-idle: 10
min-idle: 5
max-active: 100
max-wait: 3000
timeout: 6005
server:
port: 8082
SessionService2App
@SpringBootApplication
public class SessionService2App {
public static void main(String[] args) {
SpringApplication.run(SessionService1App.class, args);
}
}