Create a Spring Boot Application for Hazelcast Viridian

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 Hazelcast Viridian console under Connect Your Application > Advanced Setup.

    • clusterName

    • discoveryToken

    • keyStorePassword

    • trustStorePassword (same as keyStorePassword)

  4. Download the keystore and truststore files from the Hazelcast Viridian console and add them to the src/main/resources/ directory. You can find these files in the Hazelcast Viridian console under Connect Your Application > 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>
HzViridianDemoApplication.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 HzViridianDemoApplication {

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

    @ConditionalOnProperty(
        name = "hazelcast.viridian.tlsEnabled",
        havingValue = "true"
    )
    @Bean
    ClientConfig hazelcastClientConfig(
        @Value("${hazelcast.viridian.discoveryToken}") String discoveryToken,
        @Value("${hazelcast.viridian.clusterName}") String clusterName,
        @Value("${hazelcast.viridian.keyStore}") Resource keyStore,
        @Value("${hazelcast.viridian.keyStorePassword}") String keyStorePassword,
        @Value("${hazelcast.viridian.trustStore}") Resource trustStore,
        @Value("${hazelcast.viridian.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.setClusterName(clusterName);
        config.setProperty("hazelcast.client.cloud.url", "https://api.viridian.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 Viridian Serverless 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 10 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 Viridian Serverless cluster.

Next Steps

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

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

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