๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ˜˜๏ธ ๋ฐฑ์—”๋“œ: Backend

[Spring] MySQL๊ณผ mongoDB ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค 2๊ฐœ ์—ฐ๋™ํ•˜๊ธฐ

by ๐Ÿค ์ค€์ฝฉ์ด 2023. 5. 10.

๐Ÿง ์ƒํ™ฉ

 

ํ˜„์žฌ ์šด์˜์ค‘์ธ Hous- (ํ•˜์šฐ์Šค) ๊ณต๋™์ƒํ™œ ๋„์šฐ๋ฏธ ์„œ๋น„์Šค์—์„œ ๊ธฐ์กด์—๋Š” MySQL๋งŒ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์Œ“์ด๋Š” ์˜ค๋ž˜๋œ ์•Œ๋ฆผ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์šฐ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•ด์•ผํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ฐ€๋‚œํ•œ ๋Œ€ํ•™์ƒ์ด๊ธฐ ๋•Œ๋ฌธ์—.. ์‚ฌ์šฉ์ž๊ฐ€ ์•ฝ 200๋ช… ์ •๋„์— ๋ถˆ๊ณผํ•œ๋ฐ ์ž˜ ๋ณด์ง€๋„ ์•Š๋Š” ์•Œ๋ฆผ ๋ฐ์ดํ„ฐ๊ฐ€ 10,000๊ฐœ ์ด์ƒ ์Œ“์ด๋‹ˆ..

์ด๋ ‡๊ฒŒ ๊ณ„์† ํ•„์š”์—†๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์Œ“์ด๋‹ค ๋ณด๋ฉด AWS ํ”„๋ฆฌํ‹ฐ์–ด ๊ธฐ์ค€์„ ์ดˆ๊ณผํ• ๊นŒ๋ด ๊ฒ์ด๋‚ฌ๊ฑฐ๋“ ์š” ๐Ÿฅฒ

 

์„œ๋ฒ„ ํŒŒํŠธ ํŒ€์›๊ณผ ๋…ผ์˜๋ฅผ ํ†ตํ•ด

  1. Spring Batch๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
  1. ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ด ์˜ค๋ž˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์šฐ๋Š” ๋ฐฉ๋ฒ•
  1. ๊ทธ๋ฆฌ๊ณ  mongoDB๋ฅผ ๋„์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ–ˆ์Šต๋‹ˆ๋‹ค.

 

mongoDB์—์„œ๋Š” TTL index๋ฅผ ์ง€์›ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์œ ํšจ๊ธฐ๊ฐ„์„ ์„ค์ •ํ•ด์„œ ์˜ค๋ž˜๋œ ๋ฐ์ดํ„ฐ๋Š” ๋ณ„๋„์˜ ์ฒ˜๋ฆฌ ์—†์ด ์ž๋™์œผ๋กœ ์ง€์›Œ์ง€๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ฑฐ๋“ ์š”!

 

Spring Batch๋ฅผ ํ™œ์šฉํ•˜๊ธฐ์—๋Š” ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋„ˆ๋ฌด ์ ์–ด์„œ ์˜ค๋ฒ„์ŠคํŽ™์ด๋ผ๊ณ  ํŒ๋‹จํ–ˆ๊ณ ,

์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์•ˆ์€ ์„œ๋ฒ„์— ๋ถˆํ•„์š”ํ•œ ๋ถ€ํ•˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๋˜ํ•œ mongoDB๋ฅผ ์ด๋ฒˆ ๊ธฐํšŒ์— ๋„์ž…ํ•œ๋‹ค๋ฉด, NoSQL์ด ๋” ์ ํ•ฉํ•œ ๋„๋ฉ”์ธ๋“ค์€ ์ดํ›„์— ์ „ํ™˜์„ ํ•ด์„œ ์„ฑ๋Šฅ์„ ๋” ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค!

๊ทธ๋ž˜์„œ ์ €ํฌ๋Š” mongoDB๋ฅผ ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ MySQL๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ›  ์ฝ”๋“œ

 

โญ๏ธ 1. build.gradle ์ˆ˜์ •

 

mongoDB ๋„์ž…์„ ์œ„ํ•ด์„œ๋Š” ์ฒซ๋ฒˆ์งธ๋กœ build.gradle ์— mongoDB ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

dependencies {
		// ...
    runtimeOnly "mysql:mysql-connector-java"
    implementation "org.springframework.boot:spring-boot-starter-data-jpa"
    implementation "org.springframework.boot:spring-boot-starter-data-mongodb"
}

 

๊ฐ€์žฅ ์•„๋žซ์ค„์ด mongoDB๋ฅผ ์ถ”๊ฐ€ํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

 

 

โญ๏ธ 2. Document ์ถ”๊ฐ€

 

MySQL์„ ์‚ฌ์šฉํ•  ๋•Œ @Entity ์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ด @Document ์ž…๋‹ˆ๋‹ค.

@Column ๊ณผ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์€ @Field ์ž…๋‹ˆ๋‹ค.

 

@Document(collection = "notification")
public class Notification {

	@Id
	private String id;

	@Field(name = "type")
	private String type;

	@Field(name = "content")
	private String content;

	@Field(name = "is_read")
	private boolean isRead;
}

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด Document ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์˜ํ•  ์ ์œผ๋กœ๋Š” mongoDB ์˜ ๊ฒฝ์šฐ, id ๋ฅผ String, ObjectId, BigInteger ์ค‘ ํ•˜๋‚˜๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค!

 

 

โญ๏ธ 3. Repository ์ถ”๊ฐ€

 

mongoDB์˜ ๊ฒฝ์šฐ, JpaRepository ๋Œ€์‹  MongoRepository ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

public interface NotificationRepository extends MongoRepository<Notification, String> {

}

 

 

โญ๏ธ 4. Config ํŒŒ์ผ ์ถ”๊ฐ€

 

Jpa ์„ค์ • ํŒŒ์ผ

@Configuration
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = "hous.core.domain.*.mysql")
public class JpaConfig {
}

 

mongoDB ์„ค์ • ํŒŒ์ผ

@Configuration
@RequiredArgsConstructor
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = "hous.core.domain.*.mongo")
public class MongoDbConfig {

	private final MongoMappingContext mongoMappingContext;

	@Bean
	public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory mongoDatabaseFactory,
		MongoMappingContext mongoMappingContext) {
		DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDatabaseFactory);
		MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
		converter.setTypeMapper(new DefaultMongoTypeMapper(null));
		return converter;
	}
}

 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ ๊นŠ๊ฒŒ ๋ด์•ผํ•  ๋ถ€๋ถ„์€

 

  • @EnableJpaRepositories(basePackages = "hous.core.domain.*.mysql")
  • @EnableMongoRepositories(basePackages = "hous.core.domain.*.mongo")

 

๋‘ ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.

 

basePackages ๋ฅผ ์ž์„ธํžˆ ๋ณด๋ฉด

JpaRepository ์˜ ๊ฒฝ์šฐ mysql ํŒจํ‚ค์ง€๋กœ ์„ค์ •๋˜์–ด์žˆ๊ณ ,

MongoRepository ์˜ ๊ฒฝ์šฐ mongo ํŒจํ‚ค์ง€๋กœ ์„ค์ •๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•ด๋‹น ์„ค์ •์„ ํ†ตํ•ด Spring ์€ ๊ฐ๊ฐ์˜ repository ๋“ค์„

Jpa ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ Bean ์„ ๋“ฑ๋กํ• ์ง€, mongoDB ์„ค์ • ๊ธฐ์ค€์œผ๋กœ Bean ์„ ๋“ฑ๋กํ• ์ง€ ๊ตฌ๋ถ„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

 

ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

MappingMongoConverter ๋Š” ๋ถ€๊ฐ€์ ์ธ ์„ค์ •์ž…๋‹ˆ๋‹ค.

ํ•ด๋‹น ์„ค์ • ์—†์ด ์‹คํ–‰ํ•˜๋ฉด _class ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๊ฑธ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์ž…๋‹ˆ๋‹ค.

(๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์„ค์ •์„ ์ง€์šฐ๊ณ  Document ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด๋ณด์„ธ์š”!)

 

 

โญ๏ธ 5. yml ํŒŒ์ผ ์ˆ˜์ •

 

๋งˆ์ง€๋ง‰์œผ๋กœ mongoDB ์— ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

application.yml

spring:

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: MySQL url
    username: ์œ ์ €๋„ค์ž„
    password: ํŒจ์Šค์›Œ๋“œ

  data:
    mongodb:
      uri: mongoDB uri

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•˜๋ฉด ๋์ž…๋‹ˆ๋‹ค! ๐Ÿคฉ