javaspringspring-boothazelcast

Configuring Hazelcast via Spring Boot application.yml


I was able to get Hazelcast caching working in my Spring Boot application with a hazelcast.xml file. However, the application has multiple profiles and I'm trying to avoid managing multiple hazelcast.xml files. I want to configure all of the options within the application.yml. If I understand the documentation correctly, I should be able to do the following in my application.yml, correct?

hazelcast:
  client:
    config:
      network:
        join:
          tcp-ip:
            member:
              - 1.2.3.4
              - 9.8.7.6
      map:
        name: user-details
        max-size: 1000
        eviction-policy: LRU
        eviction-percentage: 10
        time-to-live-seconds: 900

Except it's not working. I've tried finding other references to configuring Hazelcast through the application.yml but have found none. Is what I'm trying to do impossible?


Solution

  • The application.yml file is a way to pass configuration properties to Spring Boot. At the moment, there isn't an automatic way to pass these from one to the other.

    So what you're trying to do won't work as is. Perhaps this is worth you logging an issue on https://github.com/hazelcast/hazelcast or https://github.com/spring-projects/spring-boot for it to be addressed.

    In the meantime, if you have an application.yml with a property such as

    hazelcast:
     group:
      name: "stack.overflow"
    

    then this will be put into Spring's environment as hazelcast.group.name property.

    And you had a hazelcast.xml containing

    <group>
        <name>${hazelcast.group.name}</name>
    </group>
    

    then "all" you'd need then to do is have your code create a Config @Bean like this

    @Bean
    public Config config(Environment environment) {
        Properties props = new Properties();
        props.put("hazelcast.group.name", environment.getProperty("hazelcast.group.name"));
        return new ClasspathXmlConfig("hazelcast.xml", props);
    }
    

    to pass the property from the YML file into the XML file.