Spring Session Redis Standalone

Redis Developer Course Redis Technical Support Redis Enterprise Server

Java Program Project Configuration

구성 소프트웨어 버전

Spring Boot: 3.1.6
Spring: 6.0.14
Srping-session-data-redis:3.1.3
Lettuce: 6.2.7
Redis: 7.0.10
Java: 17

Project 생성: start.spring.io

build.gradle

dependencies에 spring-session-data-redis 가 있는지 확인한다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.session:spring-session-data-redis'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

application.properties


Java Program

Main Class: SpringSessionRedisStandaloneApplication

Class SessionController

  • hello1(): session.setAttribute("hello1","Charlie");
  • hello2(): session.setAttribute(); 2회 실행
  • hello3(): ① 속성명 지정해서 값 가져오기. ② 모든 속성명, 값 가져오기. ③ Session ID, 생성일시, 마지막 액세스(접근) 일시 가져오기
  • hello4(): Redis HGETALL 명령으로 세션 데이터 가져오기. 메서드 명은 redisHash.entries()


Redis Server에서 실행되는 명령과 클라이언트 로그

Redis Server에서 실행되는 명령 확인은 redis-cli의 monitor 기능을 사용했습니다.
방법: redis-cli -h ip -p port -a password monitor

http://localhost:8080/hello1

hello1() source

레디스 서버에서 실행되는 명령

처음 실행

  • 새 브라우저에서 처음 실행할 때 - 13 개 명령 실행
  • HMSET 명령은 4개 필드 lastAccessedTime, maxInactiveInterval, creationTime, sessionAttr를 저장한다.
1701408755.729478 "HMSET" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
    "lastAccessedTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;"
    "maxInactiveInterval" "\xac\xed\x00\x05sr\x00\x11java.lang.Integer"
    "creationTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;"
    "sessionAttr:hello1" "\xac\xed\x00\x05t\x00\aCharlie"
1701408755.738963 "PEXPIREAT" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178" "1701410555621"
1701408755.750703 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408755.925471 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408755.942429 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408755.958528 "HMSET" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
    "lastAccessedTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;"
1701408755.965737 "PEXPIREAT" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178" "1701410555831"
1701408755.972962 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408755.988381 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408756.003783 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408756.012902 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408756.027778 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408756.049819 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"

두 번째 실행

  • 두 번째 실행할 때는 6개 명령이 실행된다. 이것이 일반적인 레디스 명령 실행 개수이다.
  • HGETALL 명령이 실행되면 HMSET 명령으로 lastAccessedTime을 수정한다. 그리고 PEXPIREAT 명령으로 만료(삭제)일시를 수정한다.
1701408831.590145 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408831.606970 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408831.613901 "HMSET" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
    "lastAccessedTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;"
    "sessionAttr:hello1" "\xac\xed\x00\x05t\x00\aCharlie"
1701408831.625532 "PEXPIREAT" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178" "1701410631498"
1701408831.636370 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701408831.647818 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"

http://localhost:8080/hello2

hello2() source

레디스 서버에서 실행되는 명령

  • 6개 명령이 실행되었다.
  • setAttribute()로 인해서 HMSET 명령에 sessionAttr:hello2-2, sessionAttr:hello2-1 필드와 값이 추가되었다.
1701409172.906381 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409172.921199 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409172.930663 "HMSET" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
    "lastAccessedTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;"
    "sessionAttr:hello2-2" "\xac\xed\x00\x05t\x00\x06Kwon-2"
    "sessionAttr:hello2-1" "\xac\xed\x00\x05t\x00\x06Kwon-1"
1701409172.939577 "PEXPIREAT" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178" "1701410972807"
1701409172.950508 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409172.967013 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"

http://localhost:8080/hello3

hello3() source

레디스 서버에서 실행되는 명령

  • 6개 명령이 실행되었다.
  • 조회(HGETALL)만 있었으므로 HMSET이 lastAccessedTime 만 업데이트 했다.
1701409202.838687 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409202.867229 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409202.875582 "HMSET" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
    "lastAccessedTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;"
1701409202.884368 "PEXPIREAT" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178" "1701411002751"
1701409202.894681 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409202.910857 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"

클라이언트 로그 - 해당 java source를 같이 표시했다.

    // 속성명 지정해서 값 가져오기
    String hello1Value = (String) session.getAttribute("hello1");
    log.info("hello1 Value: {}",hello1Value);
T14:40:02.751 c.r.s.SessionController : hello1 Value: Charlie

    // 모든 속성명, 값 가져오기
    Enumeration<?> attrName = session.getAttributeNames();
    while (attrName.hasMoreElements()) {
        String attr = (String) attrName.nextElement();
        log.info("{}:{}",attr,session.getAttribute(attr));
    }
T14:40:02.752 c.r.s.SessionController : hello2-1:Kwon-1
T14:40:02.752 c.r.s.SessionController : hello1:Charlie
T14:40:02.752 c.r.s.SessionController : hello2-2:Kwon-2


    // Session ID, 생성일시, 마지막 액세스(접근) 일시 가져오기
    String sessionId = session.getId();
    log.info("sessionId: {}",sessionId);
T14:40:02.752 c.r.s.SessionController : sessionId: 05e334af-daf0-4041-a762-f549779e8178
    Date ctime = new Date(session.getCreationTime());
    log.info("CreationTime: {}",ctime);
T14:40:02.752 c.r.s.SessionController : CreationTime: Fri Dec 01 14:32:35 KST 2023
    Date atime = new Date(session.getLastAccessedTime());
    log.info("LastAccessedTime: {}",atime);
T14:40:02.752 c.r.s.SessionController : LastAccessedTime: Fri Dec 01 14:40:02 KST 2023

http://localhost:8080/hello4

hello4() source

레디스 서버에서 실행되는 명령

소스 "Map entries = redisHash.entries(springSessionId);" 의 결과로 두 번째 HGETALL이 실행되었다.

1701409226.979379 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409226.991984 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409227.014409 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409227.022081 "HMSET" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
    "lastAccessedTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;"
1701409227.035565 "PEXPIREAT" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178" "1701411026878"
1701409227.051148 "HGETALL" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"
1701409227.078058 "EXISTS" "spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178"

클라이언트 로그

소스에서는 세 가지 방법으로 출력(로그)했으나 결과는 모두 같으므로 여기서는 "방법1: Lambda 사용"만 표시했다.

T14:40:26.879 c.r.s.SessionController : springSessionId: spring:session:sessions:05e334af-daf0-4041-a762-f549779e8178
T14:40:26.894 c.r.s.SessionController : 방법1: Lambda 사용
T14:40:26.894 c.r.s.SessionController : key:maxInactiveInterval, value:java.lang.Integer
T14:40:26.894 c.r.s.SessionController : key:sessionAttr:hello2-1, value:Kwon-1
T14:40:26.894 c.r.s.SessionController : key:creationTime, value:java.lang.Long;
T14:40:26.895 c.r.s.SessionController : key:sessionAttr:hello1, value:Charlie
T14:40:26.895 c.r.s.SessionController : key:lastAccessedTime, value:java.lang.Long;
T14:40:26.895 c.r.s.SessionController : key:sessionAttr:hello2-2, value:Kwon-2

<< Spring Project IntelliJ Session Standalone Session Master-Replica >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.