스프링 개발환경 분리

woniwon·2024년 3월 18일

Spring

목록 보기
8/8

개발 환경 분리

💡 프로젝트 개발시에는 `h2` 를 사용했고, 최종 배포에서는 `MySQL`을 사용하기로 해서 개발 환경 분리가 필요해졌다. 환경 분리 방법은 yml 파일을 나누는 방법과 하나의 yml 파일 안에서 나누는 방법이 있는데, test를 이전에 이미 나눴고, 분리하는 것은 pord, 메인 두개 뿐이기 때문에 코드의 가독성이 좋다고 생각되는 **yml 파일을 나누는 방법**을 사용할 것이다.

기존의 yml 파일

application.yml

spring:
  datasource:
    url: jdbc:h2:mem:test;MODE=MySQL
    username: sa
    password:
    driver-class-name: org.h2.Driver
  h2:
    console:
      enabled: true
      path: /h2-console
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        format_sql: true
      default_batch_fetch_size: 100
    open-in-view: false
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

jwt:
  secretKey: ENC(FFqIhuVHP55GbmEItS8qE6yuBnqu6uy8pH7iGmebJW9NB0AEtYPesSJndj1Mqb2H2CasgMESlggdCQDnY98j8US2kEbSVEiqip3xIbBrRlPNVbzzIb4wLrgfHxZks9gdmGCaqttRLw8=)

jasypt:
  encryptor:
    enabled: true

kakao:
  token-url: https://kauth.kakao.com/oauth/token
  user-api-url: https://kapi.kakao.com/v2/user/me
  rest-api-key: 0efff2c2f5f7de603b91cb6230b131ab
  redirect-url:  http://localhost:3000

google:
  token-url: https://oauth2.googleapis.com/token
  user-api-url: https://www.googleapis.com/oauth2/v3/userinfo
  redirect-url: http://localhost:3000/login/google
  client-id: ${GOOGLE_CLIENT_ID}
  client-secret: ${GOOGLE_CLIENT_SECRET}

cloud:
  aws:
    s3:
      bucket: kakaotechcampust-step3-nemobucket
    stack.auto: false
    region.static: ap-northeast-2
    credentials:
      accessKey: AKIAVBQDJ37FAVI2YLP2
      secretKey: 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

application-test.yml

spring:
  datasource:
    url: jdbc:h2:mem:test;MODE=MySQL
    username: sa
    password:
    driver-class-name: org.h2.Driver
  h2:
    console:
      enabled: true
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        format_sql: true
      default_batch_fetch_size: 100
    open-in-view: false
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

kakao:
  tokenUrl: https://kauth.kakao.com/oauth/token
  userApiUrl: https://kapi.kakao.com/v2/user/me
  restapiKey: 0efff2c2f5f7de603b91cb6230b131ab
  redirectUrl:  http://localhost:8080/callback
  authUrl: https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=
  • 우리는 기본 port 번호를 지정하지 않았는데.. 포트 번호를 별도로 지정하지 않으면 스프링 부트에서 기본적으로 8080 포트를 사용한다고 한다!

변경 사항

설계

  • application-test.yml : test 설정 파일
  • application-pord.yml : 배포용 설정 파일
  • application.yml : local 개발 환경 및 환경 변수 설정

알게 된 사실

  • 스프링은 application.yml 파일을 기본 설정 파일로 사용하며, 프로파일 별로 추가 설정을 application-{profile}.yml 파일에 작성할 수 있다
  • application.yml 파일에 작성된 설정은 모든 프로파일에서 공통으로 사용되며, 각 프로파일 설정 파일에 작성된 설정은 해당 프로파일이 활성화될 때만 사용 ⇒ application.yml에 기본적인 설정들을 작성하고, 프로파일 별로 필요한 설정을 application-{profile}.yml 파일에 작성하면, 해당 프로파일이 활성화될 때 프로파일 설정 파일의 값으로 application.yml의 값을 덮어씀

개발 코드

https://github.com/Step3-kakao-tech-campus/Team2_BE/pull/64

  • test 프로파일의 경우 기본 설정과 동일해서 따로 들어있는 내용이 없다. 하지만 추후 설정이 추가될 수 있다는 확장성의 측면과, 혼동을 방지하기 위해 무슨 파일인지 주석을 달아두었다.
  • DATABASE_URL , DATABASE_USER , DATABASE_PASSWORD 는 보안상의 문제를 방지하기 위해 정보를 외부에 노출하지 않도록 하였고 환경변수로 설정해 사용하게 되었다.

참고

https://ttl-blog.tistory.com/1344#📙 실제 데이터베이스의 정보 감추기-1

profile
단순 기록용 Velog 입니다.

0개의 댓글