30 Ağustos 2021 Pazartesi

SpringBoot Actuator shutdown Endpoint

Giriş
Açıklaması şöyle.. Uygulamayı sonlandırır. Bu endpoint normalde etkin değildir, etkinleştirmek gerekir.
Shutdown is an endpoint that allows the application to be gracefully shutdown. This feature is not enabled by default. You can enable this by using management.endpoint.shutdown.enabled=true in your application.properties file. But be careful about this if you are using this.
shutdown için 2 tane yöntem var. Açıklaması şöyle.
There are two supported values for this property
- immediate: this is the default value and will cause the server to shut down immediately.
- graceful: this will enable the graceful shutdown and start respecting the timeout given in next property.

spring.lifecycle.timeout-per-shutdown-phase : This takes the values in java.time.Duration format. which will be allowed before the application is shutdown.
1.immediate Yöntem
Hemen kapatılır

2. graceful Yöntem
Açıklaması şöyle
Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and reactive and Servlet-based web applications.
Şöyle yaparız. Burada varsayılan bekleme süresi 30 saniye.
management.endpoint.shutdown.enabled=true
server.shutdown=graceful
Açıklaması şöyle. Mevcut istek yoksa yeni istek kabul edilmez ve sunucu kapanır, ancak mevcut istek varsa belirli bir süre bu isteğin işlenip bitmesini bekler.
Now as we have graceful shutdown configured, there could be two possibilities:

- There are no inflight requests. In this case the application will proceed to shutdown without waiting for grace period to complete.
- If there are any requests in flight, then application will wait for the grace period and shutdown. If there are still requests pending even after grace period, the application will throw an exception and proceed to force shutdown with below logs.
Not : graceful yöntem özellikle verinin bozulmaması (data corruption) için önemlidir.

2.1 Bekleme Süresi Tanımlamak
Varsayılan bekleme süresi 30 saniye. Ancak istenirse değiştirilebilir. Açıklaması şöyle.
During a graceful shutdown Spring Boot allows some grace period to the application to finish all the current requests or processes. Once, the grace period is over the unfinished processes or requests are just killed. By default, Spring Boot allows a 30 seconds graceful shutdown timeout. However, we can configure it by using application properties or yaml file.
Örnek
Şöyle yaparız
server:
  shutdown: "graceful"

spring:
  lifecycle:
    timeout-per-shutdown-phase: "45s" # Default is 30s
Örnek
20 saniye vermek için şöyle yaparız
# Enable graceful shutdown
server.shutdown=graceful
# Allow grace timeout period for 20 seconds
spring.lifecycle.timeout-per-shutdown-phase=20s


# Force enable health probes. Would be enabled on kubernetes platform by default
management.health.probes.enabled=true
Graceful shutdown ile loglar şuna benzer. Burada shutdown isteği alındıktan sonra timeout-per-shutdown-phase süresi kadar daha mevcut isteklerin bitirilmeye çalışıldığı görülebilir, çünkü JMSListener çalışmaya devam ediyor.
14:27:41|INFO | o.s.b.w.e.t.GracefulShutdown:53 - Commencing graceful shutdown. Waiting for active requests to complete
14:27:41|INFO | o.s.b.w.e.t.GracefulShutdown:78 - Graceful shutdown complete
14:27:49|INFO | c.a.s.t.s.w.Listener:19 - Finished processing JMS Message
14:27:49|INFO | o.s.s.c.ThreadPoolTaskExecutor:218 - Shutting down ExecutorService 'applicationTaskExecutor'
14:27:49|INFO | c.z.h.HikariDataSource:350 - HikariPool-1 - Shutdown initiated...
14:27:49|INFO | c.z.h.HikariDataSource:352 - HikariPool-1 - Shutdown completed.
3. Dışarıdan Tetiklemek
Örnek

shutdown endpoint'i tetiklemek için şöyle yaparız. Rest noktası olduğu için JSON post ediliyor.
String url = "http://localhost:8080/actuator/shutdown";

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON),

RestTemplate restTemplate = new RestTemplate();

HttpEntity<String> request = new HttpEntity("",httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url,request,String.class);

Hiç yorum yok:

Yorum Gönder