๐ SpringBoot & Docker + Reids ์ฐ๋
๊ฐ ๊ธฐ๊ธฐ์ ๋ง๋ docker ๋ฒ์ ์ ๋ค์ด๋ก๋ ๋ฐ๋๋ค.
๋ค์ด๋ก๋ ๋ฐ์ ํ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์ฌ docker์ ํ์ฌ ๋ฒ์ ์ ๋ณผ ์ ์๋ค.
โป ํฐ๋ฏธ๋์ 'docker' ๋ฅผ ์
๋ ฅํ๊ฒ ๋๋ฉด ๋ค์ํ ๋ช
๋ น์ด๋ค์ ํ์ธํ ์ ์๋ค.
์ ๋๋ก ๋ค์ด๋ก๋ ๋ฐ์๋ค๋ฉด docker์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ง๋ค์ด์ ธ์๋ ์ด๋ฏธ์ง๋ค์ ํ์ธ ํ ์ ์๋ค. ๋ฐ์ ์ฌ์ง์ ๋์ปค ๋ฐ์คํธํฑ ์ด๋ผ๋ ์ฑ์ด๋ค.
ํฐ๋ฏธ๋์์๋ ํ์ธํ ์ ์๋ค.
์ด์ ๋์ปค ํ์ผ ์ ์ด์ฉํ์ฌ ๋์ปค ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ํ, ์ด๋ฏธ์ง๋ฅผ ๋น๋ํ์ฌ ์ปจํ ์ด๋๋ฅผ ์์ฑํด ์ค๊ฒ์ด๋ค.
์ฌ๊ธฐ์ ์ฌ๋ฌ ์ฉ์ด๋ค์ด ๋์ค๋๋ฐ...
์ฉ์ด๋ค์ ๋ํ ์ค๋ช ์ด๋ค
Dockerfile์ ์์ฑํ๊ธฐ์ ์์, .jar ํ์ผ์ ์์ฑํด์ผํ๋ค.
gradle ์ด๋ maven ์ ์ฌ์ฉํ์ฌ ํ๋ก์ ํธ๋ฅผ ๋น๋ ํ ๊ฒ์ด๋ค.
์ฌ๊ธฐ์๋ Maven ์ ์ฌ์ฉํ๋ค.
ํํ์ด์ง์์ ์ง์ ๋ค์ด๋ฐ์๋ ๋๊ณ , brew๋ฅผ ์ฌ์ฉํด ๊ฐ์ ๋ค์ด๋ฐ์ผ๋ฉด ๋๋ค.
์ดํ์, ํ๋ก์ ํธ์ ๋ฃจํธ ๋๋ ํ ๋ฆฌ์์ 'mvn install' ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ก์ ํธ๋ฅผ ๋น๋ ํ ์ ์๋ค.
๋น๋๋ฅผ ๋ง์น๋ฉด, ํ๋ก์ ํธ ๋ด์ target์ด ์์ฑ ๋จ์ ๋ณผ ์ ์๋ค.
๊ทธ ํ, ๋ฃจํธ ๋๋ ํ ๋ฆฌ์ 'Dockerfile' ์ ์์ฑํ์ฌ ์ค๋ค.
Dockerfile ์์ฑ์ ์ฌ์ฉ๋๋ ๋ช ๋ น์ด๋ค์ ๋ค์๊ณผ ๊ฐ๋ค.
๋๋ ๊ธฐ๋ณธ์ ์ธ ์ต์ ๋ง ๋ฃ์ด์ test ํ์๋ค.
FROM ํ๊ทธ์ ๋์ java(jdk)๋ฒ์ ์ ํ์ธํ์ฌ baseimage๋ฅผ ์ง์ ํ๊ณ ( dockerRpository์์ ๋ค์ํ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ๋ค ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก ์ฐพ์๋ณด์ธ์)
ADD ํ๊ทธ์ maven์ ํตํด ์์ฑ๋ .jarํ์ผ๊ณผ ๊ฒฝ๋ก๋ฅผ ์ ๋ ฅํ๋ค
์ด์ธ์ ํ๊ทธ๋ ์์ ๋กญ๊ฒ ์์ฑํด ์ค ์ ์๋ค.
ํ๋กฌํํธ์
docker build -t ${"๋์ปคํ์ผ ๋ช
"} ${"๋๋ ํ ๋ฆฌ"}
ํ์์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
ํด๋น ๋จ๊ณ๋ฅผ ๋ง์น๋ฉด ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
ํ๋กฌํํธ์
docker run -p ${์ฌ์ฉํ ํฌํธ๋ฒํธ} ${์ด๋ฏธ์ง ๋ช
}
์ ์ฌ์ฉํ๋ค.
๋ณธ์ธ์ 8000๋ฒ ํฌํธ๋ฅผ ์ง์ ์ฌ์ฉํ์ฌ 8080ํฌํธ์ธ ๋์ปค ์ปจํ ์ด๋์ ์ ๊ทผํ ๊ฒ์ด๋ค.
์ง์! ์ ์์ ์ผ๋ก ๋์ํ๋ค.
docker๋ ๋ค์ด๋ฐ์์ ธ ์๋๊ฑธ๋ก ์๊ณ ..
'docker pull redis:latest' ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ต์ ๋ฒ์ ์ redis๋ฅผ ๋ฐ์์จ๋ค.
๊ทธ ๋ค์,
docker network create redis-network --driver bridge
๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ๋คํธ์ํฌ๋ฅผ ์์ฑํด ์ค๋ค.
โป docker network : ์ปจํ ์ด๋๊ฐ์ ํต์ ๋ฐ ๋ฐ์ดํฐ ๊ณต์ ๋ฅผ ์ํ ๊ฐ์ ๋คํธ์ํฌ
์ด์ , 'sudo vim redis.conf' ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ redis.conf ํ์ผ์ ์์ ํ์ฌ ์ค์ ์ ์ก์์ค๋ค.
docker run \
-d \
--name redis \
-p 6379:6379 \
--network redis-network \
-v ~/${๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ํ์ผ ๊ฒฝ๋ก} /redis.conf:/etc/redis/redis.conf \
-v redis_data:/data \
redis:latest redis-server /etc/redis/redis.conf
ํฌํธ๋ฒํธ์, ๋ฐฉ๊ธ ์ค์ ํ ๋คํธ์ํฌ, ๋ฐ์ดํฐ ์ ์ฅ ๊ฒฝ๋ก๋ฅผ ์ก์์ค๋ค.
์ด๋ ๊ฒ ์ค์ ์ ๋ง์ณค์ผ๋ฉด redis ์ด๋ฏธ์ง๊ฐ ์์ฑ๋์์์ ํ์ธํ ์ ์๋ค.
์ต์ข ์ ์ผ๋ก ์์ฑ๋ ์ด๋ฏธ์ง๋ก ์ปจํ ์ด๋๋ฅผ ๋ง๋ค์ด ์ค๋ค.
docker run --name myredis \
-p 6379:6379 \
--network redis-network \
-v /Users/lts/Desktop/docker/redis:/data \
-d redis:latest redis-server \
--appendonly yes
์ปจํ ์ด๋๊น์ง ์์ฑ์๋ฃ!
docker exec -it myredis redis-cli --raw
๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ปจํ ์ด๋ ์คํ
ํ๋กฌํํธ๋ฅผ ๋ณด๋ฉด ์ ์์ ์ผ๋ก ์ ์๋จ์ ํ์ธํ ์ ์๋ค.
โป ํด๋น ํ๋ก์ ํธ๋ Maven ์ ์ฌ์ฉํ ํ๋ก์ ํธ์ด๋ค.
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
@Bean
public StringRedisTemplate stringRedisTemplate() {
final StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
stringRedisTemplate.setConnectionFactory(redisConnectionFactory());
return stringRedisTemplate;
}
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
import java.time.LocalDateTime;
@Getter
@RedisHash(value = "resultHistory", timeToLive = 3600) // Redis Repository ์ฌ์ฉ์ ์ํ
@AllArgsConstructor
@NoArgsConstructor
public class ResultHistory {
@Id
private String id;
@Indexed // ํ๋ ๊ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ ์ ์๋๋ก ์ค์ (findByAccessToken)
private String ip;
private String originalText;
private String translatedText;
@Indexed
private LocalDateTime createDateTime;
@Builder
public ResultHistory(String ip, String originalText, String translatedText, LocalDateTime createDateTime) {
this.ip = ip;
this.originalText = originalText;
this.translatedText = translatedText;
this.createDateTime = createDateTime;
}
}
import com.teamTS.funfun.model.ResultHistory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface ResultRedisRepository extends JpaRepository<ResultHistory, String> {
Optional<List<ResultHistory>> findByIpOrderByCreateDateTimeAsc(String ip);
}
@GetMapping("/hello")
public String HelloWorld(Model model) {
redisConnectionTest();
List<TestModel> tm = testRepository.getTestData();
model.addAttribute("data", tm.get(0).getTitle());
return "home/homeView";
}
void redisConnectionTest() {
final String key = "a";
final String data = "1";
final ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
valueOperations.set(key, data);
final String s = valueOperations.get(key);
}
}
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
์์ธ์ ๊ธฐ์กด MapperRepository์ Redis์ฉ JpaRepository์์
@Repository
ํด๋น ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ์ถฉ๋ํ๋ฏ ํ๋ค. ์ด๋ด๋๋
spring.main.allow-bean-definition-overriding=true
ํ ์ค์ ์ถ๊ฐํ์ฌ ์ค๋ค.
์ฑ๊ณต์ ์ธ ์คํ.
์ดํ ' keys * ' ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๊ฐ ์ ์์ ์ผ๋ก ์ถ๊ฐ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.