spring boot

spring boot app을 로컬, 개발 서버, 실행(배포) 서버로 구분해서 실행하기 - I

hoazzinews 2024. 11. 22. 10:36

이번 글에서는 spring boot app을 로컬, 개발서버, 실행(배포) 서버로 구분해서 실행하는 방법에 대해서 살펴보겠습니다.
 
일반적으로 spring boot 프로젝트를 진행하게 되면 나만 사용하는 로컬(내 PC)과 팀에서 사용하는 개발 서버, 그리고 실제 사용자들이 접속해서 우리 서비스를 이용하는 실행(배포)서버로 구분합니다. 이때 DB와 관련한 주소, 계정, 비밀번호 등의 정보가 각각의 서버 환경에 따라서 변경되는데요, 이를 위해서 효율적으로 spring boot app에 애플리케이션 설정 값을 어떻게 해야하는지 살펴봅니다.
 
다음은 각각의 환경에 따른 DB정보및 사용자 임의 애플리케이션 설정 값(server.name) 등의 정보입니다.
 

서버 환경에 따라 DB정보 또는 사용자 임의 변수값이 변경됩니다.

 
spring boot에서는 각각의 환경에 따른  애플리케이션 설정 값을 application.properties에 정의합니다. 이를 이용해서 각각의 환경에 맞게 애플리케이션 설정 값을 적용하기 위해서는 application.properties 파일을 분리 할 수 있습니다.
 

application-properties파일을 분리 합니다.

 
위의 properties 파일의 구조를 기억하고 다음의 과정에 따라 실습합니다.
 
1. application.properties을 다음과 같이 작성합니다. application.properties에는 모든 환경에서 공통으로 사용되는 애플리케이션 설정 값을 명시 합니다.

spring.application.name=samplepjt

# Tomcat
server.port=8090

# Dev Tools
spring.devtools.restart.enabled=true

# Thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true

 
2. application.properties와 동일한 경로에 application-local.properties를 만들고 다음과 같이 작성합니다. application-local.properties에는 로컬 서버에서만 사용되는 애플리케이션 설정 값을 명시 합니다.

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/DB_NAME
spring.datasource.username=root
spring.datasource.password=1234

# Server config
server.name=local

 
4. application.properties와 동일한 경로에 application-dev.properties를 만들고 다음과 같이 작성합니다. application- dev.properties에는 개발 서버에서만 사용되는 애플리케이션 설정 값을 명시 합니다.

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.56.101:3306/DB_NAME
spring.datasource.username=root
spring.datasource.password=1234

# Server config
server.name=dev

 
5. application.properties와 동일한 경로에 application-prod.properties를 만들고 다음과 같이 작성합니다. application- prod.properties에는 실행 서버에서만 사용되는 애플리케이션 설정 값을 명시 합니다.

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.56.102:3306/DB_NAME
spring.datasource.username=root
spring.datasource.password=1234

# Server config
server.name=prod

 
이렇게 application.properties를 분리하게 되면 프로젝트 구조는 다음과 같습니다.

각각의 환경에 맞게 application-properties를 분리 했습니다.

 
6. 각각의 환경에 맞게 application-properties를 만들었으니, 이제 환경에 맞는 파일을 적용해야 합니다. application-properties파일을 열어서 다음 내용을 추가 합니다.

# profiles active
spring.profiles.active=local

 
spring.profiles.active 설정 값은 애플리케이션이 실행될 때 application.properties에 어떤 properties파일을 추가할것인지 결정하는 설정 값입니다. 따라서 'spring.profiles.active=local'이라고 하면 application.properties에  application-local.properties 설정 값이 더해지게 되고 application-dev.properties 와 application-prod.properties는 무시됩니다. 다음은 실제 애플리케이션이 실행될 때의 설정값입니다.

spring.application.name=samplepjt

# Tomcat
server.port=8090

# Dev Tools
spring.devtools.restart.enabled=true

# Thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true

# profiles active
spring.profiles.active=local

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/DB_NAME
spring.datasource.username=root
spring.datasource.password=1234

# Server config
server.name=local

 
만약 application.properties의 spring.profiles.active 값을 dev라고 하면 application.properties에 application-dev.properties 설정 값이 더해져서 다음과 같이 설정됩니다.

spring.application.name=samplepjt

# Tomcat
server.port=8090

# Dev Tools
spring.devtools.restart.enabled=true

# Thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true

# profiles active
spring.profiles.active=local

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.56.101:3306/DB_NAME
spring.datasource.username=root
spring.datasource.password=1234

# Server config
server.name=dev

 
결과적으로 개발자는 애플리케이션을 실행할 때는 로컬에서 실행한다면 application.properties의 spring.profiles.active 값을 local로 변경하고, 개발 서버에서 실행할 때는 spring.profiles.active 값을 dev라고 변경하면 됩니다. 당연히 실행 서버에서 실행하려면  spring.profiles.active 값을 prod로 변경합니다.
 
다음 그림은 spring.profiles.active 값에 따라 설정 파일이 적용되는 과정입니다.

active 값을 이용해서 해당 설정 파일을 찾습니다.

 
7. 추가로 애플리케이션 설정 값을 사용하기 위해서는 @Value를 이용합니다.
 
application.properties

# profiles active
spring.profiles.active=dev

 
application-dev.properties

# Server config
server.name=dev

HomeController.java

package com.office.samplepjt;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Controller
public class HomeController {

	@Value("${server.name}")
	private String serverName;
	
	@GetMapping({"", "/"})
	public String home() {
		log.info("serverName: {}", serverName);
		
		String nextPage = "home";
		
		return nextPage;
		
	}
	
}

 
애플리케이션 실행 후 log 확인

serverName: dev

 
다음 시간에는 보다 편리하게 애플리케이션 설정 값을 구분하는 방법에 대해서 살펴보겠습니다.
https://hoazzinews.tistory.com/5

 

spring boot app를 로컬, 개발 서버, 실행(배포) 서버로 구분해서 실행하기 - II

지난 글에서는 application.properties 파일에서 spring.profiles.active 값을 변경해서 로컬, 개발 서버, 실행(배포) 서버를 구분하는 방법에 대해서 살펴봤습니다.https://hoazzinews.tistory.com/4 spring boot app를 로

hoazzinews.tistory.com