Spring-boot Batch with MySQL

์žญ์žญ์ดยท2021๋…„ 4์›” 26์ผ
1

Spring-boot

๋ชฉ๋ก ๋ณด๊ธฐ
6/11
post-thumbnail

Spring-boot Batch with MySQL

๐ŸŽ ๋ชฉ์ฐจ

0. ๊ฐœ์š”

์ด์ „ ํฌ์ŠคํŠธ์™€ ์ด์–ด์ง€๋Š” ๋‚ด์šฉ์ด๋‹ค.

Spring batch์— h2์™€ ๊ฐ™์€ ์ธ๋ฉ”๋ชจ๋ฆฌ db๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด spring-boot์ด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”๋“ค์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค.
ํ•˜์ง€๋งŒ mysql๊ณผ ๊ฐ™์€ db๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง์ ‘ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.

1. MySQL ๋„์šฐ๊ธฐ

ํŽธ์˜๋ฅผ ์œ„ํ•ด mysql์„ ubuntu์—์„œ docker container๋กœ ๋„์šด๋‹ค.

mkdir ~/db-source
touch ~/db-source/docker-compose.yaml
# db-source
version: '3.1'

networks:
  etl-example:

services:
  db-source:
    image: mariadb:latest
    container_name: db-source
    restart: always
    environment:
      TZ: Asia/Seoul
      # mysql์„ ์ฒ˜์Œ ์„œ๋น„์Šคํ• ๋•Œ์— root password๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
      # ์•„๋ž˜์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•˜์—ฌ ์˜ฌ๋ฆฐ ํ›„ ์ง€์›Œ์ค€๋‹ค.
      # MYSQL_ROOT_PASSWORD: ${PASSWORD}
    networks:
      - etl-example
    ports:
      - 3306:3306
    volumes:
      - /data/db/data:/var/lib/mysql
      - /data/db/config:/etc/mysql/conf.d
# container run
docker-compose -f ~/db-source/docker-compose.yaml up -d

2. DB ์„ค์ •

mysql์— database๋ฅผ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

# db ์ƒ์„ฑ
docker exec -it db-source bash
mysql -u root -p
## ํŒจ์Šค์›Œ๋“œ ์ž…๋ ฅ

CREATE DATABASE spring_batch default CHARACTER SET UTF8;
SHOW DATABASES;

# spring์—์„œ ์ ‘์†ํ•  ๊ฒƒ ์ด๋ฏ€๋กœ localhost๊ฐ€ ์•„๋‹Œ %๋ฅผ ์จ์ค€๋‹ค.
GRANT ALL PRIVILEGES ON spring_batch.* TO $ID@'%' IDENTIFIED BY "$PASSWORD";

exit

mysql -u $ID -p
## $PASSWORD ์ž…๋ ฅ

SHOW DATABASES;
## spring_batch๊ฐ€ ๋ณด์ด๋Š”์ง€ ํ™•์ธ

3. Dependency ์ถ”๊ฐ€

build.gradle์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

dependencies {  
	// jdbc
	implementation 'org.springframework.boot:spring-boot-starter-jdbc'
  // mysql
	implementation 'mysql:mysql-connector-java'
}

4. application.yml ์ž‘์„ฑ

mysql์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ resources/application.propertiesํŒŒ์ผ์„ ๋‹ค์‹œ ์ž‘์„ฑํ•œ๋‹ค.
์„ค์ •์ด ๋ณต์žกํ•ด์ง€๋ฏ€๋กœ *.properties๋Œ€์‹  *.yml๋กœ ์ „ํ™˜ํ•œ๋‹ค.

spring:
  profiles: mysql
  datasource:
    hikari:
      # MYSQL_ADDR:PORT/DATABASE
      jdbc-url: jdbc:mysql://192.168.56.103:3306/spring_batch
      # mysql databases๋ฅผ accessํ•˜๊ธฐ์œ„ํ•œ ๊ณ„์ •
      username: rivernine
      password: rivernine
      # mysql์šฉ jdbc๋“œ๋ผ์ด๋ฒ„
      driver-class-name: com.mysql.jdbc.Driver

5. ํ…Œ์ด๋ธ” ์ƒ์„ฑ

์—ฌ๊ธฐ๊นŒ์ง€ ์ง„ํ–‰ํ•œ ํ›„ ์‹คํ–‰์„ ์‹œ์ผœ๋ณด๋ฉด ํ•ด๋‹น ํ…Œ์ด๋ธ”์ด ์—†๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์ถ”๊ฐ€๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
์Šคํ‚ค๋งˆ๋“ค์€ ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

๋ณธ ๋ฌธ์„œ์—์„œ๋Š” schema-mysql.sql์„ ์‚ฌ์šฉํ•œ๋‹ค.
shcema-mysql.sql์˜ ๋ชจ๋“  ์Šคํ‚ค๋งˆ๋ฅผ mysql์—์„œ ์‹คํ–‰ํ•œ๋‹ค.

mysql -u $USER -p

use spring_batch;
# shcema-mysql.sql ๋ถ™์—ฌ๋„ฃ๊ธฐ
show tables;

๋‹ค์Œ ํ…Œ์ด๋ธ”๋“ค์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธํ•œ๋‹ค.

6. ์‹คํ–‰

# ๋นŒ๋“œ
./gradlew build
# Spring-boot ์‹คํ–‰
java -jar ./build/lib/*.jar


๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ์‹คํ–‰์„ ํ•˜๋ฉด ์˜ค๋ฅ˜์—†์ด ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.


๋ชจ๋“  ์†Œ์Šค๋Š” ๊นƒํ—ˆ๋ธŒ์— ์˜ฌ๋ ค๋†“์•˜๋‹ค.
์ฐธ๊ณ ๋งํฌ: jojoldu ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€