Create a Spring Boot Application for Hazelcast Cloud

In this tutorial, you’ll learn how to create a basic Spring Boot application that connects to a cluster.

Context

The Spring Framework is a Java framework for creating standalone applications that run on the Java Virtual Machine (JVM).

Spring Boot is a tool that makes it easier and faster to develop applications with the Spring Framework by providing many features, including autoconfiguration.

If you’re unfamiliar with Spring or Spring Boot, see the Spring Boot documentation.

Before you Begin

You need the following:

Step 1. Clone the Sample Project

All the code for this project is available on GitHub. In this step, you’ll clone the project and learn how it works.

  1. Clone the GitHub repository.

    • HTTPS

    • SSH

    git clone https://github.com/hazelcast-guides/spring-boot-sample.git
    
    cd spring-boot-sample
    git clone git@github.com:hazelcast-guides/spring-boot-sample.git
    
    cd spring-boot-sample
  2. Open the src/main/resources/application.properties file.

  3. Add connection credentials for your cluster to the application.properties file.

    You can find these credentials in the Cloud dashboard for your cluster; clusterID is provided in the Cluster details, and the other credentials in the Advanced option under Connect Client.
    • clusterID

    • discoveryToken

    • keyStorePassword

    • trustStorePassword (same as keyStorePassword)

  4. Download the keystore and truststore files from the Cloud console and add them to the src/main/resources/ directory. You can find these files in the Cloud dashboard for your cluster under Connect Client > Advanced Setup.

pom.xml

If the Hazelcast client is on the classpath and a suitable configuration is found, Spring Boot automatically configures a HazelcastInstance that you can inject into your application.

<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-enterprise</artifactId>
    <version>5.1.2</version>
</dependency>
HzCloudDemoApplication.java

Spring Boot first attempts to create a Hazelcast client by checking the presence of a com.hazelcast.client.config.ClientConfig bean. This bean is configured using the information in the application.properties file.

@SpringBootApplication
public class HzCloudDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(HzCloudDemoApplication.class, args);
    }

    @ConditionalOnProperty(
        name = "hazelcast.cloud.tlsEnabled",
        havingValue = "true"
    )
    @Bean
    ClientConfig hazelcastClientConfig(
        @Value("${hazelcast.cloud.discoveryToken}") String discoveryToken,
        @Value("${hazelcast.cloud.clusterId}") String clusterId,
        @Value("${hazelcast.cloud.keyStore}") Resource keyStore,
        @Value("${hazelcast.cloud.keyStorePassword}") String keyStorePassword,
        @Value("${hazelcast.cloud.trustStore}") Resource trustStore,
        @Value("${hazelcast.cloud.trustStorePassword}") String trustStorePassword
    ) throws IOException {
        Properties props = new Properties();
        props.setProperty("javax.net.ssl.keyStore", keyStore.getURI().getPath());
        props.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
        props.setProperty("javax.net.ssl.trustStore", trustStore.getURI().getPath());
        props.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
        ClientConfig config = new ClientConfig();
        config.getNetworkConfig().setRedoOperation(true);
        config.getNetworkConfig().setSSLConfig(new SSLConfig().setEnabled(true).setProperties(props));
        config.getNetworkConfig()
            .getCloudConfig()
                .setDiscoveryToken(discoveryToken)
                .setEnabled(true);
        config.setClusterId(clusterId);
        config.setProperty("hazelcast.client.cloud.url", "https://api.cloud.hazelcast.com");

        return config;
    }

}
MapService.java

When the com.hazelcast.client.config.ClientConfig bean is present, a HazelcastInstance is injected into the application classes. This instance is a Hazelcast client with an established connection to the Cloud Standard cluster.

@Component
public class MapService {

    private static final Logger logger = LoggerFactory.getLogger(MapService.class);

    private final HazelcastInstance hazelcastInstance;

    public MapService(HazelcastInstance hazelcastInstance) {
        this.hazelcastInstance = hazelcastInstance;
    }

    @EventListener
    public void onApplicationIsReady(ContextRefreshedEvent contextRefreshedEvent) {
        var mapName = "MyMap";
        Map<String, String> myMap = hazelcastInstance.getMap(mapName);
        for (int i = 0; i < 10; i++) {
            myMap.put(UUID.randomUUID().toString(), "Value-" + i);
        }
        logger.info("Map prepopulated [mapName={},size={}]", mapName, myMap.size());
    }

}

Step 2. Connect the Client

In the spring-boot-sample/ directory, execute the following:

  • Linux and Mac

  • Windows

./mvnw spring-boot:run
mvnw.cmd spring-boot:run

The client connects to the cluster and adds ten random entries to a map called MyMap.

...
Authenticated with server
...
Map prepopulated [mapName=MyMap,size=10]
...
BUILD SUCCESS

Summary

In this tutorial, you learned how to set up a Spring Boot application to connect to a Cloud Standard cluster.

Next Steps

Use this boilerplate application as a foundation to start building your own application.

See the Hazelcast Cloud documentation to continue learning or try another tutorial.

See the Spring Boot documentation for more about using Hazelcast with Spring.