Spring Boot 2.3 の Liveness & Readiness Probes 対応 について調べてみた / Spring Boot 2.3 Liveness & Readiness Probes

Spring Boot 2.3 の Liveness & Readiness Probes 対応 について調べてみた / Spring Boot 2.3 Liveness & Readiness Probes

2020/07/30「JSUG勉強会 2020年その6 LT大会!」#jsug
@otty_375

58c7dad4dc33a4aeec9f4e63e9355528?s=128

otty375

July 30, 2020
Tweet

Transcript

  1. Spring Boot 2.3ͷ Liveness & Readiness Probes ରԠ ʹ͍ͭͯௐ΂ͯΈͨ 2020/07/30ʮJSUGษڧձ

    2020೥ͦͷ6 LTେձʂʯ#jsug @otty_375 
  2. ࣗݾ঺հ • ͍͚(@otty_375) • WebΤϯδχΞ • ۀ຿ͰSpringΛ࢖ͬͯ·͢ 

  3. Liveness & Readiness Probes ରԠͷ֓ཁ • ΞϓϦέʔγϣϯͷՄ༻ੑͷঢ়ଶΛ؅ཧ͢Δ࢓૊Έ͕ಋೖ͞Εͨ • LivenessɿΞϓϦέʔγϣϯͷ಺෦ঢ়ଶ͕ਖ਼ৗ͔ •

    ReadinessɿτϥϑΟοΫΛॲཧͰ͖Δ४උ͕׬͍ྃͯ͠Δ͔ • Spring Boot ActuatorͷϔϧενΣοΫͰঢ়ଶΛऔಘͰ͖Δ • Kubernetes؀ڥʹݶΒͣଞͷσϓϩΠ؀ڥͰ΋Livenessͱ Readinessͷ֓೦ࣗମ͸׆༻Ͱ͖Δ 
  4. ΞϓϦέʔγϣϯىಈϑΣʔζ ʹ͓͚ΔՄ༻ੑͷঢ়ଶ  ΞϓϦέʔγϣϯͷ ىಈϑΣʔζ LivenessState ReadinessState ϔϧενΣοΫ݁Ռ starting BROKEN

    REFUSING_TRAFFIC DOWN started CORRECT REFUSING_TRAFFIC OUT_OF_SERVICE ready CORRECT ACCEPTING_TRAFFIC UP • LivenessState: CORRECT / BROKEN • ReadinessState: ACCEPTING_TRAFFIC / REFUSING_TRAFFIC
  5. ΞϓϦέʔγϣϯىಈ࣌ͷ ঢ়ଶ͕มΘΔλΠϛϯά 1. ApplicationStartingEventɿΞϓϦέʔγϣϯىಈ࣌ͷॲཧͷ։࢝લ 2. ApplicationEnvironmentPreparedEventɿར༻͢ΔEnvironment͕ಛఆ͞Εͨޙ 3. ApplicationContextInitializedEventɿApplicationContext͕༻ҙ͞Εͨޙ 4. ApplicationPreparedEventɿBeanఆ͕ٛϩʔυ͞Εͨޙ

    5. ApplicationStartedEventɿApplicationContext͕ϦϑϨογϡ͞Εͨޙ 6. AvailabilityChangeEvent (LivenessState.CORRECT)ɿApplicationStartedEventͷޙ 7. ApplicationReadyEventɿApplicationRunnerͱCommandLineRunner͕ݺ͹Εͨޙ 8. AvailabilityChangeEvent (ReadinessState.ACCEPTING_TRAFFIC)ɿ ApplicationReadyEventͷޙ 
  6. ϔϧενΣοΫ༗ޮԽઃఆํ๏ • Kubernetes؀ڥɿࣗಈͰ༗ޮʹͳΔ • ͦΕҎ֎ͷ؀ڥɿҎԼͷ͍ͣΕ͔ͷઃఆΛ௥Ճ  # NEW! Spring Boot

    2.3.2 ͔Β͸ίϨ management.endpoint.health.probes.enabled=true # Spring Boot 2.3.1·Ͱ management.health.probes.enabled=true # ໌ࣔతʹKubernetes؀ڥͩͱࢦఆ͢Δύλʔϯ spring.main.cloud-platform=kubernetes
  7. ࢼͯ͠Έͨ  • ༻ҙͨ͠αϯϓϧΞϓϦέʔγϣϯ • Spring Boot 2.3.2 • Dependencies:

    Web, Actuator • σϑΥϧτͷϔϧενΣοΫର৅͸diskSpace, pingͷΈ • ApplicationEventͷϦεφʔΛ௥Ճ͠ϩάʹॻ͖ग़͢ • ΞϓϦέʔγϣϯىಈதʹϔϧενΣοΫΤϯυϙΠϯτΛୟ͍ͯ ͲͷλΠϛϯάͰεςʔλε͕มΘΔ͔Λ֬ೝ
  8. ࢼͯ͠Έͨʢઃఆͳ͠ͷ৔߹ʣ  ࣌ࠁ ग़དྷࣄ 23:42:42.381 SpringApplicationىಈॲཧ։࢝ 23:42:48.308 ϔϧενΣοΫεςʔλε͕UPͰฦͬͯ͘ΔΑ͏ʹͳΔ ʢνΣοΫର৅ͷdiskSpace, ping͕UPεςʔλεͰฦͬͯདྷΔͨΊʣ

    23:42:48.395 ApplicationStartedEventൃੜ 23:42:48.498 AvailabilityChangeEventൃੜ livenessState͕CORRECTʹมΘΔ 23:42:48.502 ApplicationReadyEventൃੜ 23:42:48.503 AvailabilityChangeEventൃੜ readinessState͕ACCEPTING_TRAFFICʹมΘΔ ΞϓϦέʔγϣϯ͸·ͩ SFBEZͳঢ়ଶͰ͸ͳ͍͕ ϔϧενΣοΫ͸61ʹͳΔ
  9. ࢼͯ͠Έͨʢઃఆ͋Γͷ৔߹ʣ  ࣌ࠁ ग़དྷࣄ 23:21:58.839 SpringApplicationىಈॲཧ։࢝ 23:22:05.789 ϔϧενΣοΫεςʔλε͕DOWNͰฦͬͯ͘ΔΑ͏ʹͳΔ ʢlivenessState͕DOWN, readinessState͕OUT_OF_SERVICEʣ

    23:22:05.877 ApplicationStartedEventൃੜ 23:22:06.030 AvailabilityChangeEventൃੜ livenessState͕CORRECTʹมΘΔ 23:22:06.034 ApplicationReadyEventൃੜ 23:22:06.039 AvailabilityChangeEventൃੜ readinessState͕ACCEPTING_TRAFFICʹมΘΔ 23:22:06.110 ϔϧενΣοΫεςʔλε͕UPͰฦͬͯ͘ΔΑ͏ʹͳΔ ʢlivenessState͕UP, readinessState͕UPʣ ΞϓϦέʔγϣϯ͕SFBEZͳ ঢ়ଶʹͳ͔ͬͯΒ ϔϧενΣοΫ͕61ʹͳΔ
  10. ίʔυΛ௥ͬͯΈͨ ʢ2.3.2.RELEASEʣ  ΞϓϦέʔγϣϯىಈ࣌ͷॲཧΛ௥ͬͨ 1. αϯϓϧΞϓϦέʔγϣϯͷmainϝιου 2. SpringApplicationͷrunϝιου 3. SpringApplicationRunListenersͷstarted,

    runningϝιου 4. EventPublishingRunListenerͷstarted, runningϝιουͰ AvailabilityChangeEventΠϕϯτൃߦ 5. ApplicationAvailabilityBeanͷonApplicationEventϝιουͰΠϕϯτݕ஌
  11. 1. αϯϓϧΞϓϦέʔγϣϯͷ mainϝιου @SpringBootApplication public class Application { public static

    void main(String[] args) { SpringApplication.run(Application.class, args); } }  • Application.java
  12. 2. SpringApplicationͷ runϝιου  • SpringApplication.javaʢҰ෦ൈਮʣ public ConfigurableApplicationContext run(String... args)

    { // লུʢΞϓϦέʔγϣϯىಈ࣌ͷॲཧ͕ॱʹߦΘΕ͍ͯΔʣ SpringApplicationRunListeners listeners = getRunListeners(args); // … try { // … listeners.started(context); // … } // … try { listeners.running(context); } // … return context; }
  13. 3. SpringApplicationRunListenersͷ started, runningϝιου  • SpringApplicationRunListeners.javaʢҰ෦ൈਮʣ private final List<SpringApplicationRunListener>

    listeners; void started(ConfigurableApplicationContext context) { for (SpringApplicationRunListener listener : this.listeners) { listener.started(context); } } void running(ConfigurableApplicationContext context) { for (SpringApplicationRunListener listener : this.listeners) { listener.running(context); } }
  14. 4. EventPublishingRunListenerͷ started, runningϝιου  • EventPublishingRunListener.javaʢҰ෦ൈਮʣ public class EventPublishingRunListener

    implements SpringApplicationRunListener, Ordered { @Override public void started(ConfigurableApplicationContext context) { context.publishEvent(new ApplicationStartedEvent(this.application, this.args, context)); AvailabilityChangeEvent.publish(context, LivenessState.CORRECT); } @Override public void running(ConfigurableApplicationContext context) { context.publishEvent(new ApplicationReadyEvent(this.application, this.args, context)); AvailabilityChangeEvent.publish(context, ReadinessState.ACCEPTING_TRAFFIC); } } "WBJMBCJMJUZ$IBOHF&WFOUΛൃߦ
  15. 5. ApplicationAvailabilityBeanͷ onApplicationEventϝιου  • ApplicationAvailabilityBean.javaʢҰ෦ൈਮʣ public class ApplicationAvailabilityBean implements

    ApplicationAvailability, ApplicationListener<AvailabilityChangeEvent<?>> { private final Map<Class<? extends AvailabilityState>, AvailabilityChangeEvent<?>> events = new HashMap<>(); @Override public void onApplicationEvent(AvailabilityChangeEvent<?> event) { Class<? extends AvailabilityState> stateType = getStateType(event.getState()); this.events.put(stateType, event); } @Override public <S extends AvailabilityState> S getState(Class<S> stateType, S defaultState) { Assert.notNull(stateType, "StateType must not be null"); Assert.notNull(defaultState, "DefaultState must not be null"); S state = getState(stateType); return (state != null) ? state : defaultState; } // … } ΠϕϯτΛݕ஌͠ ಺෦Ͱঢ়ଶΛอ࣋
  16. ঢ়ଶͷऔಘ  • ApplicationAvailability.javaʢҰ෦ൈਮʣ public interface ApplicationAvailability { default LivenessState

    getLivenessState() { return getState(LivenessState.class, LivenessState.BROKEN); } default ReadinessState getReadinessState() { return getState(ReadinessState.class, ReadinessState.REFUSING_TRAFFIC); } <S extends AvailabilityState> S getState(Class<S> stateType, S defaultState); <S extends AvailabilityState> S getState(Class<S> stateType); <S extends AvailabilityState> AvailabilityChangeEvent<S> getLastChangeEvent(Class<S> stateType); }
  17. ঢ়ଶͷऔಘʢྫʣ • ApplicationAvailabilityΛDIͯ͠औಘ •  private final ApplicationAvailability applicationAvailability; public

    void sample() { applicationAvailability.getLivenessState(); applicationAvailability.getReadinessState(); }
  18. ঢ়ଶͷมߋʢྫʣ • AvailabilityChangeEventΛൃߦ •  private final ApplicationEventPublisher publisher; public

    void sample() { try { // Կ͔͠Βͷॲཧ } catch (SampleException e) { AvailabilityChangeEvent.publish(publisher, e,ReadinessState.REFUSING_TRAFFIC); } }
  19. ·ͱΊˍײ૝ • ΞϓϦέʔγϣϯͷՄ༻ੑͷঢ়ଶΛ؅ཧ͢Δ࢓૊Έ͕ಋೖ͞Εͨ • LivenessStateͱReadinessStateΛϔϧενΣοΫʹؚΊΔʹ͸ɹ ࣗಈ΋͘͠͸1ߦઃఆΛ௥Ճ͢Δ͚ͩͰOK • ΞϓϦέʔγϣϯىಈ࣌ͷঢ়ଶมߋͷλΠϛϯάΛ஌Δʹ͸ɹɹ ॲཧͷྲྀΕΛཧղ͢Δ͜ͱ͕େ੾ •

    ΄ͱΜͲͷ৘ใ͸ެࣜυΩϡϝϯτʹॻ͍ͯ͋Δ͕ɺɹɹɹɹɹ ϩάΛॻ͖ग़ͨ͠ΓίʔυΛ௥͏͜ͱͰཧղ͕ਂ·ͬͨ 
  20. ࢀߟࢿྉ • Liveness and Readiness Probes with Spring Boot •

    https://spring.io/blog/2020/03/25/liveness-and-readiness-probes-with-spring-boot • Spring Boot 2.3.2 available now • https://spring.io/blog/2020/07/24/spring-boot-2-3-2-available-now • Application Availability • https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot- features.html#boot-features-application-availability • Kubernetes Probes • https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready- features.html#production-ready-kubernetes-probes 
  21. ऴΘΓ