SpringBoot初始教程之Redis集中式Session管理

1.介紹

有關Session的管理方式這裏就不再進行討論,目前無非就是三種單機Session(基於單機內存,無法部署多臺機器)、基於Cookie(安全性差)、基於全局的統一Session管理(redis、mysql)等多種方式 
針對於像淘寶這種超大型網站來說Session如何管理的就無從得知了、但是可以通過yy的方式想象一下,這種大型架構都需要部署多臺認證Server,但是一般來說集中式Session無法存儲那麼多的Session 
那麼就可以通過UID分片的形式來存儲,不同UID分佈在不同的Server上認證即可(純屬猜測丷)

2.快速開始

這裏採用的是redis進行集中式Session管理,核心如下依賴

            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session</artifactId>
                <version>1.2.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
            </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.1.RELEASE</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>springboot-4</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session</artifactId>
                <version>1.2.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>1.4.1.RELEASE</version>
                </plugin>
            </plugins>
        </build>

    </project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

application.yaml配置

這塊主要是通過application.yaml去配置redis的鏈接信息


    server:
      port: 8080
    spring:
      redis:
        database: 1
        host: localhost
        pool:
          max-active: 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

開啓@EnableRedisHttpSession

通過加上@EnableRedisHttpSession註解,開啓redis集中式session管理,所有的session都存放到了redis中


    @SpringBootApplication
    @EnableRedisHttpSession
    public class AppApplication {
        public static void main(String[] args) throws Exception {
            SpringApplication.run(AppApplication.class, args);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

通過源碼可知、可以通過設置maxInactiveIntervalInSeconds來設定session的統一過期時間,


    @Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
    @Target({ java.lang.annotation.ElementType.TYPE })
    @Documented
    @Import(RedisHttpSessionConfiguration.class)
    @Configuration
    public @interface EnableRedisHttpSession {
        int maxInactiveIntervalInSeconds() default 1800;

        String redisNamespace() default "";


        RedisFlushMode redisFlushMode() default RedisFlushMode.ON_SAVE;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

通過redis集中式管理session這種方式在使用上面對客戶端是透明的,無需自己操作redis,在使用HttpSession對象的時候直接使用即可


    @RestController
    public class IndexController {
        @GetMapping("/index")
        public ResponseEntity index(HttpSession httpSession) {
            httpSession.setAttribute("user", "helloword");
            return ResponseEntity.ok("ok");
        }

        @GetMapping("/helloword")
        public ResponseEntity hello(HttpSession httpSession) {
            return ResponseEntity.ok(httpSession.getAttribute("user"));
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3. 其他擴展

SpringBoot Session集中式管理不僅僅限於redis這種方式,目前內部支持HttpSession with Pivotal GemFire、HttpSession with JDBC、HttpSession with Mongo 
等多種方式具體可以參考http://docs.spring.io/spring-session/docs/1.2.2.RELEASE/reference/html5/#httpsession-jdbc 
本文代碼

https://git.oschina.net/wangkang_daydayup/SpringBoot-Learn/tree/master

springboot-4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章