【问题标题】:Hazelcast not shutting down gracefully in Spring Boot?Hazelcast 没有在 Spring Boot 中正常关闭?
【发布时间】:2021-11-14 05:59:24
【问题描述】:

我试图了解 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

它似乎正在尝试运行关闭挂钩,但它所做的最后报告仍然是“ACTIVE”,并且它永远不会像 artice 中提到的那样进入“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 正确关闭,以便其他实例识别它正在关闭而不是“消失”?

【问题讨论】:

  • 如何创建 Hazelcast 实例?如果一个是 Spring "@Bean",那么 Spring Boot 将调用它的 shutdown() 方法。但这只会关闭那个,从而让另一个运行。还要检查hazelcast.com/blog/spring-boot-hazelcast-session-replication,尤其是关于实例名称属性,可能你不需要两个 Hazelcast 实例。
  • @NeilStevenson,我不是自己创建的。我让 Spring 处理其余的事情。我拥有的唯一配置实际上就是我在帖子中列出的内容。例如,我的 App.java 中也有 @EnableCaching

标签: spring-boot hazelcast spring-cache


【解决方案1】:

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

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

你可以通过设置这个属性来完全禁用关机钩子:

-Dhazelcast.shutdownhook.enabled=false

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

-Dhazelcast.shutdownhook.policy=GRACEFUL

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

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

-Dhazelcast.logging.type=slf4j

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

更改日志级别
-Dlogging.level.com.hazelcast=TRACE

有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多