Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

otty375

July 30, 2020
Tweet

More Decks by otty375

Other Decks in Programming

Transcript

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

    ReadinessɿτϥϑΟοΫΛॲཧͰ͖Δ४උ͕׬͍ྃͯ͠Δ͔ • Spring Boot ActuatorͷϔϧενΣοΫͰঢ়ଶΛऔಘͰ͖Δ • Kubernetes؀ڥʹݶΒͣଞͷσϓϩΠ؀ڥͰ΋Livenessͱ Readinessͷ֓೦ࣗମ͸׆༻Ͱ͖Δ 
  2. ΞϓϦέʔγϣϯىಈϑΣʔζ ʹ͓͚ΔՄ༻ੑͷঢ়ଶ  ΞϓϦέʔγϣϯͷ ىಈϑΣʔζ 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
  3. ΞϓϦέʔγϣϯىಈ࣌ͷ ঢ়ଶ͕มΘΔλΠϛϯά 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ͷޙ 
  4. ϔϧενΣοΫ༗ޮԽઃఆํ๏ • 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
  5. ࢼͯ͠Έͨ  • ༻ҙͨ͠αϯϓϧΞϓϦέʔγϣϯ • Spring Boot 2.3.2 • Dependencies:

    Web, Actuator • σϑΥϧτͷϔϧενΣοΫର৅͸diskSpace, pingͷΈ • ApplicationEventͷϦεφʔΛ௥Ճ͠ϩάʹॻ͖ग़͢ • ΞϓϦέʔγϣϯىಈதʹϔϧενΣοΫΤϯυϙΠϯτΛୟ͍ͯ ͲͷλΠϛϯάͰεςʔλε͕มΘΔ͔Λ֬ೝ
  6. ࢼͯ͠Έͨʢઃఆͳ͠ͷ৔߹ʣ  ࣌ࠁ ग़དྷࣄ 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ʹͳΔ
  7. ࢼͯ͠Έͨʢઃఆ͋Γͷ৔߹ʣ  ࣌ࠁ ग़དྷࣄ 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ʹͳΔ
  8. ίʔυΛ௥ͬͯΈͨ ʢ2.3.2.RELEASEʣ  ΞϓϦέʔγϣϯىಈ࣌ͷॲཧΛ௥ͬͨ 1. αϯϓϧΞϓϦέʔγϣϯͷmainϝιου 2. SpringApplicationͷrunϝιου 3. SpringApplicationRunListenersͷstarted,

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

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

    { // লུʢΞϓϦέʔγϣϯىಈ࣌ͷॲཧ͕ॱʹߦΘΕ͍ͯΔʣ SpringApplicationRunListeners listeners = getRunListeners(args); // … try { // … listeners.started(context); // … } // … try { listeners.running(context); } // … return context; }
  11. 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); } }
  12. 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Λൃߦ
  13. 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; } // … } ΠϕϯτΛݕ஌͠ ಺෦Ͱঢ়ଶΛอ࣋
  14. ঢ়ଶͷऔಘ  • 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); }
  15. ঢ়ଶͷऔಘʢྫʣ • ApplicationAvailabilityΛDIͯ͠औಘ •  private final ApplicationAvailability applicationAvailability; public

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

    void sample() { try { // Կ͔͠Βͷॲཧ } catch (SampleException e) { AvailabilityChangeEvent.publish(publisher, e,ReadinessState.REFUSING_TRAFFIC); } }
  17. ࢀߟࢿྉ • 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