spring-session-data-redis라는 라이브러리가 있어서 Redis로 세션 스토리지를 쉽게 구축할 수 있다. 이 라이브러리를 활용해서 세션 스토리지를 구축해보자. ... dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-webmvc' implementation 'org.springframework.boot:spring-boot-starter-data-redis' implementation 'org.springframework.session:spring-session-data-redis' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-data-jpa-test' testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } ...

spring: datasource: url: jdbc:mysql://localhost:3306/mydb?rewriteBatchedInserts=true username: root password: password driver-class-name: com.mysql.cj.jdbc.Driver data: redis: host: localhost port: 6379 jpa: hibernate: ddl-auto: create show-sql: true session: store-type: redis
@Configuration @EnableRedisHttpSession public class RedisSessionConfig { }
기존에 실행시켜놨던 Spring Boot 서버가 있다면 종료하고 아래 명령어를 입력하자.
$ ./gradlew bootRun --args='--server.port=8080' $ ./gradlew bootRun --args='--server.port=8081'



$ keys * # key 값 확인하기 $ type [세션이 저장된 key] # type 확인하기 -> hash # (값이 깨져서 나올 것이다. 왜냐면 Spring Session이 세션 값을 Binary 형태로 Redis에 저장하기 때문이다.) $ HGETALL [세션이 저장된 key] # 해당 hash에 저장된 모든 field, value 불러오기 $ HGET [세션이 저장된 key] sessionAttr:userId $ HGET [세션이 저장된 key] sessionAttr:name