首页 > 解决方案 > Hazelcast 没有在 Spring Boot 中正常关闭?

问题描述

我试图了解 Spring Boot 如何关闭分布式 Hazelcast 缓存。当我连接然后关闭第二个实例时,我得到以下日志:

一审(仍在运行)

2021-09-20 15:34:47.994  INFO 11492 --- [.IO.thread-in-0] c.h.internal.nio.tcp.TcpIpConnection     : [localhost]:8084 [dev] [4.0.2] Initialized new cluster connection between /127.0.0.1:8084 and /127.0.0.1:60552
2021-09-20 15:34:54.048  INFO 11492 --- [ration.thread-0] c.h.internal.cluster.ClusterService      : [localhost]:8084 [dev] [4.0.2] 

Members {size:2, ver:2} [
    Member [localhost]:8084 - 4c874ad9-04d1-4857-8279-f3a47be3070b this
    Member [localhost]:8085 - 2282b4e7-2b6d-4e5b-9ac8-dfac988ce39f
]

2021-09-20 15:35:11.087  INFO 11492 --- [.IO.thread-in-0] c.h.internal.nio.tcp.TcpIpConnection     : [localhost]:8084 [dev] [4.0.2] Connection[id=1, /127.0.0.1:8084->/127.0.0.1:60552, qualifier=null, endpoint=[localhost]:8085, alive=false, connectionType=MEMBER] closed. Reason: Connection closed by the other side
2021-09-20 15:35:11.092  INFO 11492 --- [ached.thread-13] c.h.internal.nio.tcp.TcpIpConnector      : [localhost]:8084 [dev] [4.0.2] Connecting to localhost/127.0.0.1:8085, timeout: 10000, bind-any: true
2021-09-20 15:35:13.126  INFO 11492 --- [ached.thread-13] c.h.internal.nio.tcp.TcpIpConnector      : [localhost]:8084 [dev] [4.0.2] Could not connect to: localhost/127.0.0.1:8085. Reason: SocketException[Connection refused: no further information to address localhost/127.0.0.1:8085]
2021-09-20 15:35:15.285  INFO 11492 --- [ached.thread-13] c.h.internal.nio.tcp.TcpIpConnector      : [localhost]:8084 [dev] [4.0.2] Connecting to localhost/127.0.0.1:8085, timeout: 10000, bind-any: true
2021-09-20 15:35:17.338  INFO 11492 --- [ached.thread-13] c.h.internal.nio.tcp.TcpIpConnector      : [localhost]:8084 [dev] [4.0.2] Could not connect to: localhost/127.0.0.1:8085. Reason: SocketException[Connection refused: no further information to address localhost/127.0.0.1:8085]
2021-09-20 15:35:17.450  INFO 11492 --- [cached.thread-3] c.h.internal.nio.tcp.TcpIpConnector      : [localhost]:8084 [dev] [4.0.2] Connecting to localhost/127.0.0.1:8085, timeout: 10000, bind-any: true
2021-09-20 15:35:19.474  INFO 11492 --- [cached.thread-3] c.h.internal.nio.tcp.TcpIpConnector      : [localhost]:8084 [dev] [4.0.2] Could not connect to: localhost/127.0.0.1:8085. Reason: SocketException[Connection refused: no further information to address localhost/127.0.0.1:8085]
2021-09-20 15:35:19.474  WARN 11492 --- [cached.thread-3] c.h.i.n.tcp.TcpIpConnectionErrorHandler  : [localhost]:8084 [dev] [4.0.2] Removing connection to endpoint [localhost]:8085 Cause => java.net.SocketException {Connection refused: no further information to address localhost/127.0.0.1:8085}, Error-Count: 5
2021-09-20 15:35:19.475  INFO 11492 --- [cached.thread-3] c.h.i.cluster.impl.MembershipManager     : [localhost]:8084 [dev] [4.0.2] Removing Member [localhost]:8085 - 2282b4e7-2b6d-4e5b-9ac8-dfac988ce39f
2021-09-20 15:35:19.477  INFO 11492 --- [cached.thread-3] c.h.internal.cluster.ClusterService      : [localhost]:8084 [dev] [4.0.2] 

Members {size:1, ver:3} [
    Member [localhost]:8084 - 4c874ad9-04d1-4857-8279-f3a47be3070b this
]

2021-09-20 15:35:19.478  INFO 11492 --- [cached.thread-7] c.h.t.TransactionManagerService          : [localhost]:8084 [dev] [4.0.2] Committing/rolling-back live transactions of [localhost]:8085, UUID: 2282b4e7-2b6d-4e5b-9ac8-dfac988ce39f

似乎当我关闭它时,第二个实例没有报告它正在正确关闭第一个实例。在它无法连接几秒钟并因此从集群中删除后,我们会收到警告。

第二个实例(被关闭的那个)

2021-09-20 15:42:03.516  INFO 4900 --- [.ShutdownThread] com.hazelcast.instance.impl.Node         : [localhost]:8085 [dev] [4.0.2] Running shutdown hook... Current state: ACTIVE
2021-09-20 15:42:03.520  INFO 4900 --- [ionShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown        : Commencing graceful shutdown. Waiting for active requests to complete
2021-09-20 15:42:03.901  INFO 4900 --- [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown        : Graceful shutdown complete

它似乎正在尝试运行关闭挂钩,但它所做的最后报告仍然是“活动”,并且它永远不会进入本文中提到的“SHUTTING_DOWN”或“SHUT_DOWN

配置

pom.xml

...
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
...
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-all</artifactId>
            <version>4.0.2</version>
        </dependency>
    </dependencies>
...

只是为了添加一些上下文。我有以下 application.yml

---
server:
  shutdown: graceful

以及以下 hazelcast.yaml

---
hazelcast:
  shutdown:
    policy: GRACEFUL
    shutdown.max.wait: 8
  network:
    port:
      auto-increment: true
      port-count: 20
      port: 8084
    join:
      multicast:
        enabled: false
      tcp-ip:
        enabled: true
        member-list:
          - localhost:8084

问题

所以我的理论是 Spring Boot 通过终止 hazelcast 来关闭它,而不是让它优雅地关闭。

如何使 Spring Boot 和 Hazelcast 正确关闭,以便其他实例识别它正在关闭而不是“消失”?

标签: spring-boothazelcastspring-cache

解决方案


这里有两件事在起作用。首先是终止实例而不是正常关闭的真正问题。另一个在日志中正确地看到它。

Hazelcast 默认注册一个关闭钩子,在 JVM 退出时终止实例。

您可以通过设置此属性来完全禁用关闭挂钩:

-Dhazelcast.shutdownhook.enabled=false

或者,您可以将策略更改为正常关闭

-Dhazelcast.shutdownhook.policy=GRACEFUL

但这会导致 spring boot 正常关闭=完成服务请求和 Hazelcast 实例同时关闭,从而导致问题。

要正确查看日志,请将日志记录类型设置为 slf4j:

-Dhazelcast.logging.type=slf4j

然后您将info正确看到来自 Hazelcast 的所有日志,并通过更改日志级别

-Dlogging.level.com.hazelcast=TRACE

作品。


推荐阅读