SpringBoot之使用Spring Session集群-redis

      用nginx做负载的时候,会出现每一次http请求都会分配到不同的tomcat上,这样用session存储用户数据就会有问题。因为tomcat集群之间没有实现session共享。

spring-session跟redis很好的帮我们解决了这个问题。

一、引入maven依赖

01 <?xml version="1.0" encoding="UTF-8"?>
04     <modelVersion>4.0.0</modelVersion>
05  
06     <groupId>com.cicoding.druid</groupId>
07     <artifactId>springboot_session_redis_colony</artifactId>
08     <version>0.0.1-SNAPSHOT</version>
09     <packaging>war</packaging>
10  
11     <name>springboot_session_redis_colony</name>
12     <description>Demo project for Spring Boot</description>
13  
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.9.RELEASE</version>
18         <relativePath /> <!-- lookup parent from repository -->
19     </parent>
20  
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26  
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-data-redis</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.session</groupId>
34             <artifactId>spring-session-data-redis</artifactId>
35         </dependency>
36          
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-starter-web</artifactId>
40         </dependency>
41     </dependencies>
42  
43     <build>
44         <plugins>
45             <plugin>
46                 <groupId>org.springframework.boot</groupId>
47                 <artifactId>spring-boot-maven-plugin</artifactId>
48             </plugin>
49         </plugins>
50     </build>
51 </project>

二、配置application.properties

server.port=8082
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=redis password

# spring session使用存储类型
spring.session.store-type=redis
  • spirngboot默认就是使用redis方式,如果不想用可以填none。

三、在启动类中加入@EnableRedisHttpSession  注解

package com.cicoding;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableCaching
@EnableRedisHttpSession  
@SpringBootApplication
public class SessionApplication {

	public static void main(String[] args) {
		SpringApplication.run(SessionApplication.class, args);
	}
}

四、编写控制器

package com.cicoding.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/")
public class UserController {

	@ResponseBody
    @RequestMapping(value = "/session")
    public Map<String, Object> getSession(HttpServletRequest request) {
        request.getSession().setAttribute("username", "admin");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sessionId", request.getSession().getId());
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/get")
    public String get(HttpServletRequest request) {
        String userName = (String) request.getSession().getAttribute("username");

        return userName;
    }

}

五、测试

posted on 2019-08-03 16:42  小学员G  阅读(275)  评论(0编辑  收藏  举报