Home Spring boot 핵심 원리
Post
Cancel

Spring boot 핵심 원리

이 게시물은 김영한 - 스프링 부트 핵심 원리 요약임
모든 코드는 OsoriAndOmori/playground-spring 에 올려둠.
멋대로 만든 프로젝트 구조 이기 떄문에, 강좌 내용은 따라가나 구조는 절대 따라가지 않음..

웹 어플리케이션 컨테이너 (톰캣 직접 사용 & servlet 개발)

  • spring boot 빼고 이거 한번 구현해봄.
    • Servlet 컨테이너 초기화 -> 어플리케이션 초기화(Servlet 등록)
    • 여기서 어플리케이션 초기화 과정에! spring container 만들면 되는거임.
  • DispatcherServlet 은 controller 를 호출해주는 servlet
  • 하나의 서버에 DispatcherServlet 도 두개만들고, spring container 도 여러개 띄울 수 있음. 근데 보통 이렇게 안하지 ㅋㅋ
  • spring 에서 WebApplicationInitializer 가 어떻게 어플리케이션 초기화 할때 하는지 이해해야함.
    • 결국 위에서 META-INF 등록하고, @HandleType 써서 쫙 class 가져와가지고 init 하는건 똑같음.
    • 여기까진 서블릿 컨테이너 위에서 모든걸 개발한 것임

빌드 배포

  • jar 는 다른 jar 를 품지 못함.
    • 하지만 클래스는 다 가질 수 있다.
    • 그래서 library 에 있는 class 를 꺼내서 집어넣음. -> fat jar 로 만든다.
    • fat jar 는 이름 겹치는 문제가 발생해서 실행가능 Jar 를 만듬
    • 실행가능 Jar = bootJar 명령어를 통해서 만들고
    • bootJar 는 플러그인이 만듦
  • WebApplicationInitializer 인터페이스 이용해서 spring container 만들고, tomcat 만들고, dispatcher servlet 등록하고 띄움
    • TomcatWebServerFactory 브레이크 걸고 봐.

Spring boot

  • 내가 주시해야할 패키지
    • spring-boot
    • autoconfigure : 코드
    • spring-boot-starter : https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot
      • build.gradle 만 있고 parent pom 같은거라고 생각하면 됨.
  • 실행 가능 jar 란?
    • JarLauncher.class main 을 보면됨
  • 스프링 컨테이너 만드는 곳 : org.springframework.boot.web.servlet.context.ServletWebServerApplicationContextFactory
  • 내장 톰캣 만드는 곳 : org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory

  • Autoconfiguration 기능
    • Condition 인터페이스를 구현해서
    • META-INF/spring/*AutoConfiguration.imports 에다가 넣음.
    • autoconfigure 잘 뒤져서 보면 됨. @Import(AutoConfigurationImportSelector.class) ImportSelector 로 동적으로 등록할 수 있음을 인지.
    • @Conditional - @ConditionalOnProperty, @ConditionalOnClass, @ConditionalOnBean, @ConditionalOnXXXXX

모니터링

  • micrometer 라는 거로 cpu, mem 측정 추상화를 다 시켜버림
  • 이후 그 데이터로, 프로메테우스보내던가 JMX 로 보내던가 구현체만 따로따로 만들면 됨.
  • 다양한 메트릭은 /actuator/metrics리스트보고 /actuator/metrics/{metricname} 하면 하나하나 볼 수 있음.
  • /request 보면 max time, 이런거 다 기록되어있음 -> 이걸 그대로 prometeous 로 날리는거임.
  • tag=level:error 이런 기능도 가능.
  • 사용자 정의 메트릭도 가능 - 주문 숫자 뭐 이런거로 서버 숨졋나 확인 가능.
  • 마이크로미터 <-주기적떙겨감– 프로메테우스 <-PromQL- 그라파나
    • 프로메테우스만으로도 그냥 데이터 볼 수 있지만, 공유 dashboard 를 적극 활용하자
  • MeterRegistry micrometer 활용해서 커스텀한 수치를 prometheus 를 추가할 수 있음 : AOP (@Counted, @Timer, @Timed)
This post is licensed under CC BY 4.0 by the author.

모든 개발자를 위한 HTTP 웹 기본 지식

내 맘대로 팀 입사 교육과정