개발일지

[개발일지_safeHome] 11. redis 세팅 추가

woopii 2025. 12. 29. 00:48

1. 의존성 추가

  • build.gradle
val embeddedRedisVersion = "0.7.3"

dependencies {
	...

    // Redis, embedded redis
    implementation("org.springframework.boot:spring-boot-starter-data-redis")
    implementation("it.ozimov:embedded-redis:${embeddedRedisVersion}")

	...
}

 

 

2. 프로퍼티 설정 추가

  • application-redis.yml 
---
spring:
  config:
    activate:
      on-profile: local

  redis:
    host: localhost
    port: 6379

---
spring:
  config:
    activate:
      on-profile: dev

  redis:
    host: localhost
    port: 6379

---
spring:
  config:
    activate:
      on-profile: prd

  redis:
    host: localhost
    port: 6379

 

 

 

3. config 추가

로컬은 서버 실행 시 embedded redis가 따로 실행 되게끔 의존성과 설정을 추가,

(단독실행이 가능한 환경을 만들기 위해)

 

  • RedisConfig
package com.woopi.safehome.global.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.connection.RedisConnectionFactory
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer
import org.springframework.data.redis.serializer.StringRedisSerializer

@Configuration
class RedisConfig {

    @Bean
    fun redisTemplate(
        connectionFactory: RedisConnectionFactory
    ): RedisTemplate<String, Any> {
        return RedisTemplate<String, Any>().apply {
            this.connectionFactory = connectionFactory

            // Key는 String으로
            keySerializer = StringRedisSerializer()
            hashKeySerializer = StringRedisSerializer()

            // Value는 JSON으로 (객체 저장 가능)
            valueSerializer = GenericJackson2JsonRedisSerializer()
            hashValueSerializer = GenericJackson2JsonRedisSerializer()
        }
    }
}

 

 

  • EmbeddedRedisConfig
package com.woopi.safehome.global.config


import jakarta.annotation.PostConstruct
import jakarta.annotation.PreDestroy
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import redis.embedded.RedisServer
import java.io.IOException
import java.net.ServerSocket

@Profile("local", "test") 
@Configuration
class EmbeddedRedisConfig(
    @Value("\${spring.data.redis.port:6379}")
    private val redisPort: Int
) {
    private var redisServer: RedisServer? = null

    @PostConstruct
    fun startRedis() {
        val availablePort = if (isPortInUse(redisPort)) {
            findAvailablePort()
        } else {
            redisPort
        }

        redisServer = RedisServer(availablePort)
        redisServer?.start()
        println("Embedded Redis started on port $availablePort")
    }

    @PreDestroy
    fun stopRedis() {
        redisServer?.stop()
        println("Embedded Redis stopped")
    }

    private fun isPortInUse(port: Int): Boolean {
        return try {
            ServerSocket(port).use { false }
        } catch (e: IOException) {
            true
        }
    }

    private fun findAvailablePort(): Int {
        return try {
            ServerSocket(0).use { socket ->
                socket.localPort
            }
        } catch (e: IOException) {
            throw IllegalStateException("사용 가능한 포트를 찾을 수 없습니다", e)
        }
    }
}