기본 콘텐츠로 건너뛰기

스프링부트, Externalized Configuration

환경변수 설정

  • 설정파일을 어플리케이션 밖에서 관리함으로써 같은 어플리케이션으로 여러가지 설정을 변경하면서 운영할 수 있다.
  • Properties files, yaml files, environment variable, command-line arguments 사용
  • @Value 어노테이션을 통해 주입받을 수 있다.

우선순위

  • 스프링부트에서는 환경변수값을 읽는 우선순위가 있다. 만약 예사외의 값이 적용되었다면 아래 순위를 확인해보자
  • devtools -> test관련 -> command line -> servlet -> Jndi -> system property -> environments -> random -> properties file -> @Configuration classes
1.  [Devtools global settings properties](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-devtools-globalsettings) on your home directory (~/.spring-boot-devtools.properties when devtools is active).
2.  [@TestPropertySource](https://docs.spring.io/spring/docs/5.0.5.BUILD-SNAPSHOT/javadoc-api/org/springframework/test/context/TestPropertySource.html) annotations on your tests.
3.  [@SpringBootTest#properties](https://docs.spring.io/spring-boot/docs/2.0.1.BUILD-SNAPSHOT/api/org/springframework/boot/test/context/SpringBootTest.html) annotation attribute on your tests.
4.  Command line arguments.
5.  Properties from SPRING\_APPLICATION\_JSON (inline JSON embedded in an environment variable or system property).
6.  ServletConfig init parameters.
7.  ServletContext init parameters.
8.  JNDI attributes from java:comp/env.
9.  Java System properties (System.getProperties()).
10.  OS environment variables.
11.  A RandomValuePropertySource that has properties only in random.*.
12.  [Profile-specific application properties](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-profile-specific-properties) outside of your packaged jar (application-{profile}.properties and YAML variants).
13.  [Profile-specific application properties](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-profile-specific-properties) packaged inside your jar (application-{profile}.properties and YAML variants).
14.  Application properties outside of your packaged jar (application.properties and YAML variants).
15.  Application properties packaged inside your jar (application.properties and YAML variants).
16.  [@PropertySource](https://docs.spring.io/spring/docs/5.0.5.BUILD-SNAPSHOT/javadoc-api/org/springframework/context/annotation/PropertySource.html) annotations on your @Configuration classes.
17.  Default properties (specified by setting SpringApplication.setDefaultProperties).

Configuring Random Values

  • 프로퍼티에 랜던값을 넣어줄 수 있음
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int\[1024,65536\]}

yaml

  • json의 superset, properties의 대안,
  • SankeYAML 라이브러리 필요(Spring-boot-starter에 포함)

Loading YAML

  • yaml를 로딩하는 클래스는 2가지임
  • YamlPropertiesFactoryBean : Properties 형태
  • YamlMapFactoryBean : Map 형태
Yaml
environments:

dev:

url: http://dev.example.com

name: Developer Setup

prod:

url: http://another.example.com

name: My Cool App
Yaml->properties
environments.dev.url=http://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=http://another.example.com
environments.prod.name=My Cool App
Yaml
my:
servers:
- dev.example.com
- another.example.com
Yaml -> list
my.servers[0]=dev.example.com
my.servers[1]=another.example.com

@ConfigurationProperties

  • yaml 프로퍼티를 바로 mutable한 list나 set타입의 객체에 바인딩할 수도 있다.
@ConfigurationProperties(prefix="my")
public class Config {
  private List<String> servers = new ArrayList<String>();
  public List<String> getServers() {
  return this.servers;
  }
}

Multi-profile YAML Documents

  • 하나의 파일에서 복수개의 프로파일을 설정할 수 있다.
server: 
 address: 192.168.1.100
---
spring:
 profiles: development
server:
 address: 127.0.0.1
---
spring:
 profiles: production
server:
 address: 192.168.1.120

Third-party Configuration

@ConfigurationProperties 어노테이션을 사용하면 properties를 바로 클래스에 매핑해서 빈으로 등록해서 사용할 수 있었다. 그런데 이번에는 추가로 @Bean 어노테이션을 사용하면 클래스가 외부에 존재한다고 하더라도 빈으로 등록해서 사용할 수 있다.
@ConfigurationProperties(prefix = "another")
@Bean
public AnotherComponent anotherComponent() {
...
}

Properties Conversion

스프링부트에서 프로퍼티를 빈으로 매핑할때 적당한 타입을 지정할때 @ConfigurationProperties 빈이 사용된다. 만약 type 변환을 수정하려면 ConversionService 빈을 사용허거나 CustomEditorConfigurer, Coverters라는 빈을 활용하면 된다.

Converting durations

프로퍼티에 시간에 관련된 값이 있다면 java.util.Duration 클래스를 사용한다.
@DurationUnit 을 별도로 지정하지 않으면 ISO-8601 형식으로 출력된다.

@ConfigurationProperties("app.system")
public class AppSystemProperties {
  
  @DurationUnit(ChronoUnit.SECONDS)
  private Duration sessionTimeout = Duration.ofSeconds(30);
  private Duration readTimeout = Duration.ofMillis(1000);
  public Duration getSessionTimeout() {
   return this.sessionTimeout;
  }

  public void setSessionTimeout(Duration sessionTimeout) {
   this.sessionTimeout = sessionTimeout;
  }
  
  public Duration getReadTimeout() {
   return this.readTimeout;
  }

  public void setReadTimeout(Duration readTimeout) {
   this.readTimeout = readTimeout;
  }
}

Validation

@Validated 를 사용해 propertie값의 유효성체크를 할 수 있다.

@ConfigurationProperties(prefix="acme")
@Validated
public class AcmeProperties {

 @NotNull
 private InetAddress remoteAddress;

 //... getters and setters_
}
중첩된, 서브프로퍼티도 유효성체크를 진행할 수 있는데 명시적으로 @Valid 어노테이션을 사용하는걸 권장한다고 한다.
@ConfigurationProperties(prefix="acme")
@Validated
public class AcmeProperties {

@NotNull
private InetAddress remoteAddress;

@Valid
private final Security security = new Security();

// ... getters and setters

  public static class Security {

  @NotEmpty
  public String username;
  // ... getters and setters
  
  }
}
Validator를 수정할 수 도 있다. https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-property-validation/src/main/java/sample/propertyvalidation/SamplePropertyValidationApplication.java

@ConfigurationProperties vs. @Value

Feature @ConfigurationProperties @Value
Relaxed binding Yes No
Meta-data support Yes No
SpEL evaluation No Yes


Profile

어플리케이션 환경설정을 분리하고, 특정한 환경에서만 사용할 수 있도록 해준다.
@Component, @Configuration은 @Profile과 같이 사용할 수 있다.

@Configuration
@Profile("production")
public class ProductionConfiguration {

// ...


}

spring.profiles.active 특정 profile 명세해줌으로써 사용할 있다. 
// spring.profiles.active=dev,hsqldb

command-line에서도 지정 가능하다. 
// —spring.profiles.active=dev,hsqldb.




댓글

이 블로그의 인기 게시물

메일서버가 스팸으로 취급받을때

설치한 메일서버를 통해 발송되는 메일이 스팸으로 들어가는 경우가 더러 있다. 이게 한번 들어가기는 쉬운데, 빠져나오기는 드럽게 힘든것 같다... 본인의 경우에는 우선 국내서비스에는 별 무리 없이 들어간다. (naver,daum 등) 그런데 해외메일 그중 Gmail, Hotmail 에는 에누리없이 스팸으로 간주되고 있었다. Gmail같은 경우에는 그래도 스팸함으로 발송은 제대로 되는반면에 Hotmail같은경우에는 아예 수신자체가 안되는 경우도 더러있다.. ㅡ,.ㅡ; 제일 좋은 방법은 Gmail,Hotmail에 전화걸어서 우리 메일서버 IP white Ip로 등록해달라!!! 하면 좋지만, 얘네들은 걸어봤자 자동응답기고, 문의채널은 구글 그룹스 게시판이 전부다.. 본론으로 들어가서. 해외 메일이 차단될 경우 내 매일서버ip가 스팸ip로 등록되 버린 경우일 수 있다. (본인처럼. ㅎ) 이것부터 조회 해보고 싶으면 RBL(real-time blocking List) 체크를 해야 하는데, RBL체크 해주는 사이트는 꽤 많이 있고, 그중 좀 깔끔해 보이는곳 하나 소개. http://www.anti-abuse.org/ 메일서버ip 입력하고 조회해보면 쭈루룩 리스트가 나온다. 그 중 빨간불이 들어온 부분이 메일 서버가 스팸서버가 된 각종 이유들이다.ㅋ 본인의 경우 CBL 때문에 걸렸는데, 내용은 아래와 같다. This IP address is HELO'ing as  "localhost.localdomain"  which violates the relevant standards (specifically: RFC5321). 메일서버 도메인에 별다른 작업을 안해놓아서 "localhost.localdomain" 으로 설정되어있었다. 만약 CBL만 바로 테스트 해보고 싶으면 http://cbl.abusea

[javascript] 특정시간에만 함수 실행

특정시간에만 팝업을 띄우려면?? 특정시간에만 로그인을 막으려면?? 특정시간에만 할일은 의외로 참 많다. 방법? 딱히 없다. 현재시간 구해서 시작시간, 종료시간 사이에 있을때 시작하는 수밖엔. if ((현재시간 > 시작시간) && (현재시간 < 종료시간)){ .. 팝업노출(); 공사페이지 리다이렉트(); 기타등등(); .. } 자바스크립트로 작성하면 다음과 같다. var startdate = "2014012008" ; var enddate = "2014012418" ; var now = new Date (); //현재시간 year = now. getFullYear (); //현재시간 중 4자리 연도 month = now. getMonth () + 1 ; //현재시간 중 달. 달은 0부터 시작하기 때문에 +1 if ((month + "" ). length < 2 ){ month = "0" + month; //달의 숫자가 1자리면 앞에 0을 붙임. } date = now. getDate (); //현재 시간 중 날짜. if ((date + "" ). length < 2 ){ date = "0" + date; } hour = now. getHours (); //현재 시간 중 시간. if ((hour + "" ). length < 2 ){ hour = "0" + hour; } today = year + "" + month + "" + date + "" + hour; //오늘 날짜 완성. / / 시간비교 i

[linux] 모든 cron 확인

예전에 만들어놓은 cron이 요새 안돌고 있어서 찾아봤다. 물론 내가 만들었던 아니고ㅋ crontab -l no crontab for root 헐, 내가 찾던 cron이 안보인다. 분명히 예전 사수가 여기다 작업 해놓은것 같았는데... cron이 뭐하는 놈인지는 알아도, 어떻게 생겨먹은놈인지는 몰랐다ㅋ 이리저리 폴더 뒤져서 그때 스크립트 파일을 찾아보기 시작했다. 멍청한짓을 한 참 한후에 혹하는 마음에 로컬 사용자 뭐있나 찍어봄 cat /etc/passwd 찍어보믄 계정별로 결과가 쭈루루룩 나오는데, 뜻은 이거다. 계정:암호:UID:GID:정보:디렉토리:쉘 passwd 파일 설명은 ( http://www.linuweb.com/?p=248 ) 여기 님 블로그에 잘 되어있음. 암튼 결국 계정별로 cron 걸려있는거 죄다 찍어보니 for user in $(cut -f1 -d: /etc/passwd); do echo id $user; crontab -u $user -l ; done 한번에 다찍어주는 명령어는 따로 없나보다. 간단하게 스크립트로 찍어주는 수밖에. 결국 내가 찾던 cron은 처음보는 계정에 들어있었음. 그리고 cron이 안돌던 이유는 ftp가 막혀서였고 ㅋ