Hazelcast

如果类路径上有 Hazelcast,且找到了适当的配置,则 Spring Boot 会自动配置一个 HazelcastInstance,可将其注入你的应用程序。

If Hazelcast is on the classpath and a suitable configuration is found, Spring Boot auto-configures a HazelcastInstance that you can inject in your application.

Spring Boot 最初会尝试通过检查以下配置选项来创建客户端:

Spring Boot first attempts to create a client by checking the following configuration options:

  • The presence of a com.hazelcast.client.config.ClientConfig bean.

  • A configuration file defined by the configprop:spring.hazelcast.config[] property.

  • The presence of the hazelcast.client.config system property.

  • A hazelcast-client.xml in the working directory or at the root of the classpath.

  • A hazelcast-client.yaml (or hazelcast-client.yml) in the working directory or at the root of the classpath.

如果无法创建一个客户端,则 Spring Boot 会尝试配置一个内嵌服务器。如果你定义了一个 com.hazelcast.config.Config bean,则 Spring Boot 会使用它。如果你的配置定义了一个实例名称,则 Spring Boot 尝试查找一个现有的实例,而不是创建一个新实例。

If a client can not be created, Spring Boot attempts to configure an embedded server. If you define a com.hazelcast.config.Config bean, Spring Boot uses that. If your configuration defines an instance name, Spring Boot tries to locate an existing instance rather than creating a new one.

你还可以通过配置指定要使用的 Hazelcast 配置文件,如以下示例所示:

You could also specify the Hazelcast configuration file to use through configuration, as shown in the following example:

spring:
  hazelcast:
    config: "classpath:config/my-hazelcast.xml"

否则,Spring Boot 会尝试从默认位置查找 Hazelcast 配置:hazelcast.xml(位于工作目录或类路径根目录),或在相同位置的 YAML 副本。我们还会检查是否设置了 hazelcast.config 系统属性。有关更多详细信息,请参见 Hazelcast documentation

Otherwise, Spring Boot tries to find the Hazelcast configuration from the default locations: hazelcast.xml in the working directory or at the root of the classpath, or a YAML counterpart in the same locations. We also check if the hazelcast.config system property is set. See the Hazelcast documentation for more details.

默认情况下,支持 Hazelcast 组件上的 @SpringAwareManagementContext 可以通过声明一个 HazelcastConfigCustomizer bean 重写,该 bean 的 @Order 高于 0。

By default, @SpringAware on Hazelcast components is supported. The ManagementContext can be overridden by declaring a HazelcastConfigCustomizer bean with an @Order higher than zero.

Spring Boot 还具有 explicit caching support for Hazelcast。如果启用了缓存,则 HazelcastInstance 会自动包装在 CacheManager 实现中。

Spring Boot also has explicit caching support for Hazelcast. If caching is enabled, the HazelcastInstance is automatically wrapped in a CacheManager implementation.