【Spring Session】和 Redis 结合实现 Session 共享
参考官方文档
HttpSession with Redis Guide
https://docs.spring.io/spring-session/docs/2.1.5.RELEASE/reference/html5/guides/boot-redis.html
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>redis-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.3</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
server.port=8002 spring.application.name=redis-service eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${random.uuid} eureka.client.service-url.default-zone=http://localhost:8761/eureka management.endpoints.web.exposure.include=routes spring.redis.host=192.168.226.128 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 spring.session.store-type=redis server.servlet.session.timeout= 7200 spring.session.redis.flush-mode=on_save spring.session.redis.namespace=spring:session
主类
@EnableRedisHttpSession(redisFlushMode = RedisFlushMode.IMMEDIATE) @EnableDiscoveryClient @Slf4j @RestController @SpringBootApplication public class RedisServiceApplication { public static void main(String[] args) { SpringApplication.run(RedisServiceApplication.class, args); } @Autowired RedisTemplate redisTemplate; @Autowired StringRedisTemplate stringRedisTemplate; @RequestMapping("/") public Map<String, Object> home(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); System.out.println(session.getId()); SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); String data = format.format(new Date()); session.setAttribute("user", data); Map<String, Object> res = new HashMap<>(); res.put("message", "redis"); res.put("data", data); return res; } @RequestMapping("/set") public void set(String key, String value) { log.info("key = " + key + ", value = " + value); stringRedisTemplate.opsForValue().set(key, value); } @RequestMapping("/get") public String show(String key) { return stringRedisTemplate.opsForValue().get(key); } }