Spring Boot DevTools
, Spring Web
, Spring Data JPA
, MySQL Driver
μ νpackage com.example.demo;
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
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@GetMapping("/")
public String home() {
redisTemplate.opsForValue().set("abc", "def");
return "Hello World!";
}
}
FROM openjdk:17-jdk
COPY build/libs/*SNAPSHOT.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
services:
my-sever:
build: .
ports:
- 8080:8080
depends_on:
my-db:
condition: service_healthy
my-cache-server:
condition: service_healthy
my-db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: pwd1234
MYSQL_DATABASE: mydb
volumes:
- ./mysql_data:/var/lib/mysql
ports:
- 3306:3306
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
interval: 5s
retries: 10
my-cache-server:
image: redis
ports:
- 6379:6379
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
retries: 10
depends_on
μ μμ±ν΄μ€λ€.MySQL
μλ²μ Redis
μλ²μ healthcheck
λ₯Ό μ§ννκ³ , μ μμ μΌλ‘ μ¬λΌκ° λ€μ Spring Boot
μλ²κ° μ¬λΌκ°κ² λλ€.spring:
datasource:
url: jdbc:mysql://my-db:3306/mydb
username: root
password: pwd1234
driver-class-name: com.mysql.cj.jdbc.Driver
data:
redis:
host: my-cache-server
port: 6379
datasource
μ url
κ³Ό redis
μhost
λ₯Ό localhostλ‘ μ€μ νλ©΄ κ°κ° 컨ν
μ΄λ μ
μ₯μμμ ν΄λΉ ν¬νΈλ₯Ό κ°λ¦¬ν€κΈ° λλ¬Έμ μ°κ²°μ΄ λμ§ μλλ€.compose.yml
νμΌμμ μμ±ν μ΄λ¦μ μ μ΄μ£Όμ΄μΌ νλ€../gradlew clean build
docker compose up -d --build
docker compose ps
docker ps
docker logs [Container ID]