Spring

Spring 외부설정 2 (Profile 사용 예)

쭈녁 2024. 5. 6. 00:17

 

외부 설정 Profile 사용

교육 과정 마지막 프로젝트에서 Profile을 적용시키고 실행 시점에 값을 주어 실행시킬만한 좋은 케이스가 생겨서 적용해 보았다.

 

현재 프로젝트에 프로메테우스 그라파나를 설정하여 애플리케이션에 대한 모니터링을 실행하고 있다.

하지만 이 모니터링은 로컬 환경에서 실행시킬 때 불필요하고 배포 서버에서 실행될 때 필요한 상황이다. 따라서 배포 시에 VM옵션에 profile=deploy를 주어 배포 시에만 실행되도록 지정하였다.

 

모니터링 외부 설정 profile 지정

#그라파나 설정
---
spring:
  config:
    activate:
      on-profile: deploy

management:
  info:
    java:
      enabled: true
    os:
      enabled: true
    env:
      enabled: true
  server:
#    port: 8080
    port: 9292
  endpoint:
#    shutdown:
#      enabled: true
    health:
      #      show-details: always
      show-components: always
  endpoints:
    web:
      exposure:
        include: "*"
#/info? ?????? ??
info:
  app:
    name: iyjy
    company: elice

server:
  tomcat:
    mbeanregistry:
      enabled: true

 

해당 프로필에 작동할 클래스

bean을 등록할 config에서도 특정 프로필에서만 작동하도록 하였다.

@Configuration
@Profile("deploy")
public class MeterAspectConfig {


    @Bean
    public CountedAspect countedAspect(MeterRegistry registry) {
        return new CountedAspect(registry);
    }

    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
    }
}

 

그리고 배포 서버에서 실행시점에 아래와 같이 VM 옵션으로 실행시킬 프로필을 명시하여 특정 상황에만 모니터링 로그를 수집하도록 하였다

 

실행 명령어

nohup java -jar -Dspring.profiles.active=deploy demo-0.0.1-SNAPSHOT.jar &

 

이를 통해 로컬에서 default 프로필로 실행했을 때엔 수집 세팅이 되지 않고 배포시 프로필 적용하였을 때는 수집 세팅이 되도록 수정하였다.