zhengyangyong closed pull request #693: [SCB-548] support gracefully shutdown URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/693
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java index 4952f35f3..0cd57f64f 100644 --- a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java +++ b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java @@ -33,6 +33,7 @@ import org.apache.servicecomb.foundation.common.event.EventManager; import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.foundation.common.utils.FortifyUtils; +import org.apache.servicecomb.foundation.vertx.VertxUtils; import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask; import org.slf4j.Logger; @@ -135,8 +136,6 @@ public void onApplicationEvent(ApplicationEvent event) { RegistryUtils.run(); // 当程序退出时,进行相关清理,注意:kill -9 {pid}下无效 - // 1. 去注册实例信息 - // TODO 服务优雅退出 if (applicationContext instanceof AbstractApplicationContext) { ((AbstractApplicationContext) applicationContext).registerShutdownHook(); } @@ -148,12 +147,18 @@ public void onApplicationEvent(ApplicationEvent event) { } else if (event instanceof ContextClosedEvent) { LOGGER.warn("cse is closing now..."); triggerEvent(EventType.BEFORE_CLOSE); + + //Unregister microservice instance from Service Center and close vertx + //We need unregister from service center immediately RegistryUtils.destroy(); + VertxUtils.closeVertxByName("registry"); + triggerEvent(EventType.AFTER_CLOSE); isInit = false; } } + /** * <p>As the process of instance registry is asynchronous, the {@code AFTER_REGISTRY} * event should not be sent immediately after {@link RegistryUtils#run()} is invoked. diff --git a/core/src/main/java/org/apache/servicecomb/core/handler/ShutdownHookHandler.java b/core/src/main/java/org/apache/servicecomb/core/handler/ShutdownHookHandler.java index 8d1318aa3..019aab266 100644 --- a/core/src/main/java/org/apache/servicecomb/core/handler/ShutdownHookHandler.java +++ b/core/src/main/java/org/apache/servicecomb/core/handler/ShutdownHookHandler.java @@ -22,6 +22,7 @@ import org.apache.servicecomb.core.Handler; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.foundation.vertx.VertxUtils; import org.apache.servicecomb.swagger.invocation.AsyncResponse; import org.apache.servicecomb.swagger.invocation.Response; import org.slf4j.Logger; @@ -92,6 +93,11 @@ public void run() { time = time + period; LOG.warn("waiting invocation to finish in seconds " + time); } + + //Stop vertx to prevent blocking exit, this work need do after invocation waiting timeout + VertxUtils.closeVertxByName("config-center"); + VertxUtils.closeVertxByName("transport"); + LOG.warn("handler chain is shut down"); } } diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java index 4aba612f2..d5e55f463 100644 --- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java +++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java @@ -157,6 +157,13 @@ public static Vertx getVertxByName(String name) { return vertxMap.get(name); } + public static void closeVertxByName(String name) { + Vertx vertx = vertxMap.remove(name); + if (vertx != null) { + vertx.close(); + } + } + public static <T> void runInContext(Context context, AsyncResultCallback<T> callback, T result, Throwable e) { if (context == Vertx.currentContext()) { complete(callback, result, e); diff --git a/integration-tests/spring-zuul-tracing-tests/src/test/java/org/apache/servicecomb/spring/cloud/zuul/tracing/SpringCloudZuulTracingTest.java b/integration-tests/spring-zuul-tracing-tests/src/test/java/org/apache/servicecomb/spring/cloud/zuul/tracing/SpringCloudZuulTracingTest.java index 35a82b1de..1402cc237 100644 --- a/integration-tests/spring-zuul-tracing-tests/src/test/java/org/apache/servicecomb/spring/cloud/zuul/tracing/SpringCloudZuulTracingTest.java +++ b/integration-tests/spring-zuul-tracing-tests/src/test/java/org/apache/servicecomb/spring/cloud/zuul/tracing/SpringCloudZuulTracingTest.java @@ -17,7 +17,6 @@ package org.apache.servicecomb.spring.cloud.zuul.tracing; -import static com.seanyinx.github.unit.scaffolding.AssertUtils.expectFailing; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertEquals; diff --git a/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java b/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java index dd11460e4..714975512 100644 --- a/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java +++ b/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcIntegrationTest.java @@ -33,7 +33,7 @@ public static void init() throws Exception { } @AfterClass - public static void shutdown() throws Exception { + public static void shutdown() { CseApplicationListener cal = BeanUtils.getBean("org.apache.servicecomb.core.CseApplicationListener"); ContextClosedEvent event = new ContextClosedEvent(BeanUtils.getContext()); cal.onApplicationEvent(event); diff --git a/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java b/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java index a4cd05038..f25f08019 100644 --- a/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java +++ b/integration-tests/springmvc-tests/src/test/java/org/apache/servicecomb/demo/springmvc/tests/RawSpringMvcSimplifiedMappingAnnotationIntegrationTest.java @@ -34,7 +34,7 @@ public static void init() throws Exception { } @AfterClass - public static void shutdown() throws Exception { + public static void shutdown() { CseApplicationListener cal = BeanUtils.getBean("org.apache.servicecomb.core.CseApplicationListener"); ContextClosedEvent event = new ContextClosedEvent(BeanUtils.getContext()); cal.onApplicationEvent(event); ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services