当前位置: 首页 > 知识库问答 >
问题:

Hazelcast在Spring开机时没有优雅地关机?

彭鸿文
2023-03-14

我正在尝试了解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

它表示它正在尝试运行一个关闭挂钩,但它所做的最后一个报告仍然是“活动的”,并且它从来没有像本文中提到的那样转到“关机”或“关机”。

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>
...

只是为了添加一些上下文。我有以下申请。yml公司

---
server:
  shutdown: graceful

以及下面的hazelcast。亚马尔

---
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正确关闭,以便其他实例识别它正在关闭,而不是“消失”?

共有1个答案

索嘉胜
2023-03-14

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

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

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

-Dhazelcast.shutdownhook.enabled=false

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

-Dhazelcast.shutdownhook.policy=GRACEFUL

但这将导致Spring boot优雅地关闭=完成服务请求和Hazelcast实例同时关闭,从而导致问题。

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

-Dhazelcast.logging.type=slf4j

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

-Dlogging.level.com.hazelcast=TRACE

作品

 类似资料:
  • 我正在开发一个由嵌入式Tomcat支持的Spring Boot应用程序,我需要通过以下步骤开发一个优雅的关机: 停止处理新的HTTP请求(停止web容器) 处理所有已接受的请求 关闭Spring ApplicationContext 附注。Spring Boot 1.5.20版本,Java 8

  • 在spring boot中有没有一种方法可以控制App的优雅关机。 我知道在bean中可以使用@predestroy方法,但如何控制调用这些@predestroy方法的顺序。 您可以有多个相互依赖的bean,上下文的关闭是否已经寻找这种依赖关系并以正确的顺序调用@predestroy方法? 2.)阻止rabbit消息侦听器接受新消息 3.)等待关机前已经开始但尚未完成的所有处理。

  • 我用的是spring kafka 1.2.2。释放目前,我已经为没有BackOffPolicy和AlwaysRetryPolicy的容器配置了重试模板。确认模式为手动或立即。 当一个SIGTERM出现时,我会让当前消息被处理,当@KafkaListener再次被调用时,我会在容器上抛出RuntimeException,该容器会无限期重试并持续抛出异常。一段时间后,SIGKILL被发出,容器被停止(

  • null 我正在尝试做一个优雅的关闭Rest应用程序和SCS(Kafka消费者和生产者)应用程序

  • 作为自动缩放presto集群工作的一部分,我们喜欢在EC2终止presto worker之前优雅地关闭它。以下命令后 curl-v-xput--data'“shutting_down”'-h“content-type:application/json”http://250.0.46.167:8081/v1/info/state worker日志立即指示“com.facebook.presto.se

  • 我正在将FlinkKafkaConsumer010与Flink 1.2.0一起使用,我面临的问题是:如果出现某种情况,是否有办法通过编程关闭整个管道? 一种可能的解决方案是,我可以通过调用FlinkKafkaConsumer010中定义的close()方法关闭kafka消费源,然后调用带有shutdown的管道。对于这种方法,我创建了一个列表,其中包含对所有FlinkKafkaConsumer01