Spring Boot + Redis 处理 Session 共享
参考网址:
https://mp.weixin.qq.com/s?__biz=MzAwMjk5Mjk3Mw==&mid=2247493417&idx=3&sn=3d5966aa32b1947f5287e2a611aec665&chksm=9ac3590badb4d01d1a7693ae52bfd752a4f9a72ff927424a5cd5a9247e87d857f0a46f7d6040&mpshare=1&scene=23&srcid=1118zEKSu62KU0knXWvrgEMO&sharer_sharetime=1605751851594&sharer_shareid=9d1e76e919cc0b2f3ca23ed1f5ef67a8#rd
1.springboot环境
- 新建一个springboot项目
- 引入web依赖
- 添加测试方法
具体代码如下
@RestController
public class TestController {
@GetMapping("/set-session")
public Object writeSession(String sessionVal, HttpSession httpSession) {
System.out.println("Param 'sessionVal' = " + sessionVal);
httpSession.setAttribute("sessionVal", sessionVal);
return sessionVal;
}
@GetMapping("/get-session")
public Object readSession(HttpSession httpSession) {
Object obj = httpSession.getAttribute("sessionVal");
System.out.println("'sessionVal' in Session = " + obj);
return obj;
}
}
- 启动项目
- 访问地址
localhost:8080/set-session?sessionVal=mysessionValue
localhost:8080/get-session
页面输出 mysessionValue
2.springboot整合redis
- pom.xml内容如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-demo-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <packaging>war</packaging>-->
<name>springboot-demo-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入springboot整合redis的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- springboot使用redis实现session共享依赖 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>-->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- yml或者properties配置文件如下
application.properties,这里为了演示清晰,只做了最简配置,正式使用请调整相关参数
server.port=9001
#配置redis
#指定redis的库
spring.redis.database=0
# 指定redis服务的端口号
spring.redis.port=6379
# 指定redis服务的主机ip
spring.redis.host=localhost
# 指定redis服务的密码(测试期间,密码可以设置为空)
spring.redis.password=
4.session共享配置类
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
public class SessionConfig {
}
说明:
重点是两个注解
@Configuration
@EnableRedisHttpSession
5.打jar包部署
java -jar redis-session.jar --server.port=9001
java -jar redis-session.jar --server.port=9002
测试成功,可以拿到各自的session
6.重点提炼
6.1引入两个依赖
<!-- 引入springboot整合redis的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- springboot使用redis实现session共享依赖 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
6.2配置session共享的配置类
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
public class SessionConfig {
//重要的是类上的两个注解
}
7.配置nginx
说明:
为了方便测试,我在windows上部署的jar包
nginx使用的也是windows版本
nginx.conf内容如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream sessionshare{
server 127.0.0.1:9001 weight=5;
server 127.0.0.1:9002 weight=5;
}
server {
listen 9000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
#root C:\Users\Admin\Desktop\dist;
proxy_pass http://sessionshare;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
重点配置提炼
upstream sessionshare{
server 127.0.0.1:9001 weight=5;
server 127.0.0.1:9002 weight=5;
}
server {
listen 9000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
#root C:\Users\Admin\Desktop\dist;
proxy_pass http://sessionshare;
}
8.启动nginx进行访问测试
localhost:9000/set-session?sessionVal=mysessionVal
localhost:9000/get-seesion
页面返回
mysessionVal