Redis共享session的作用
- 微服务自身可以保持无状态,应用实例数量的多少不会影响用户登录状态;
- 可实现单点登录的踢出功能,如可以让上次异地登录的用户下线;
- session在多个服务或服务器间共享,实现多站点单点登录(参考SSO服务)
Redis缓存session原理简述
其工作原理,可简单用图描述(假设服务A运行有有个多个实例):
Springboot-session结合Redis示例
- 添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
- 添加redis配置(application.properties)
spring.redis.database=1
spring.redis.host=localhost
spring.redis.password=xxxxx
spring.redis.port=6379
# server.port=8080
server.port=8081
- 添加配置类
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}
- 添加controller
package com.example.demo.controller;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
@RequestMapping(value = "/")
public class Hello {
@RequestMapping(value = "/set", method = RequestMethod.GET)
public Map<String, Object> firstResp (HttpServletRequest request){
request.getSession().setAttribute("testKey", "testValue");
Map<String, Object> map = new HashMap<>();
map.put("testKey", "testValue");
return map;
}
@RequestMapping(value = "/query", method = RequestMethod.GET)
public Object sessions (HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
map.put("sessionId", request.getSession().getId());
map.put("testKey", request.getSession().getAttribute("testKey"));
return map;
}
}
package com.example.demo.controller;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
@RequestMapping(value = "/")
public class Hello {
@RequestMapping(value = "/set", method = RequestMethod.GET)
public Map<String, Object> firstResp (HttpServletRequest request){
request.getSession().setAttribute("testKey", "testValue");
Map<String, Object> map = new HashMap<>();
map.put("testKey", "testValue");
return map;
}
@RequestMapping(value = "/query", method = RequestMethod.GET)
public Object sessions (HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
map.put("sessionId", request.getSession().getId());
map.put("testKey", request.getSession().getAttribute("testKey"));
return map;
}
}
- 浏览器访问测试
- 启动两个springboot程序,分别监听8080,8081端口
- 浏览器分别访问
http://127.0.0.1:8080/set
,127.0.0.1:8081/query
接口,效果如下:
- 查看redis中的key
可以看出两个独立的应用已经共享了同一个session。