The Hazelcast Cloud Developer Hub

Welcome to the Hazelcast Cloud developer hub. You'll find comprehensive guides and documentation to help you start working with Hazelcast Cloud as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Go Client

Quick Setup

Follow the below instructions to connect a Go client to your cluster.

1. Create a cluster as explained in the Create a Cluster section. After you create the cluster, the cluster-specific page will load and cluster's state will be "Pending" for some seconds, as shown below.

2. When the cluster's state becomes "Running", the previously inactive button Configure Client on this page becomes active. Click on it and the following dialog box appears:

3. Click on the Go tab (initially, the Java tab opens by default) and download the ZIP file as instructed in Step 1 of the dialog box.
4. Extract the ZIP file and run the command, in the extracted folder, shown in Step 2 of the above dialog box.

You should see the log output similar to the following:

Connection Successful! in your logs means that your client application has successfully connected to your cluster in Hazelcast Cloud. The sample code inserts random entries to your cluster as you can see in the logs. In your Hazelcast Cloud console (your cluster's page where you can reach from the "Clusters" top menu), you should see the charts are being populated with metrics of your map, as shown below:

Also, you can see your client listed under the "Client Statistics" at the bottom of your cluster's page:

Client Code

Now, as we have successfully connected and put data into the cluster, let's review and explain the client code you downloaded.

Go to the directory (extracted from the ZIP file you downloaded in the Step 3 above) and locate the client sample code. If you had enabled encryption in your cluster, open the client_with_ssl.go file. Otherwise, open the client.go file. We will explain both files line by line in the following sections.

client.go

This is the downloaded sample client code when your cluster does not have encryption.

package main

import (
    "fmt"
    "log"
    "math/rand"
    "time"

    hazelcast "github.com/hazelcast/hazelcast-go-client"
    "github.com/hazelcast/hazelcast-go-client/config"
    "github.com/hazelcast/hazelcast-go-client/config/property"
)
func main() {

    cfg := hazelcast.NewConfig()
    cfg.GroupConfig().SetName("YOUR_CLUSTER_NAME")
    cfg.GroupConfig().SetPassword("YOUR_CLUSTER_PASSWORD")
    cfg.NetworkConfig().SSLConfig().SetEnabled(false)
    discoveryCfg := config.NewCloudConfig()
    discoveryCfg.SetEnabled(true)
    discoveryCfg.SetDiscoveryToken("YOUR_CLUSTER_DISCOVERY_TOKEN")
    cfg.NetworkConfig().SetCloudConfig(discoveryCfg)
    cfg.SetProperty(property.StatisticsEnabled.Name(), "true")
    cfg.SetProperty(property.StatisticsPeriodSeconds.Name(), "1")

    client, _ := hazelcast.NewClientWithConfig(cfg)

    mp, _ := client.GetMap("map")
    mp.Put("key", "value")
    val, _ := mp.Get("key")
    if val == "value" {
        log.Println("Connection Successful!")
        log.Println("Now, `map` will be filled with random entries.")
        rand.Seed(time.Now().UTC().UnixNano())
        for true {
            randKey := rand.Intn(100000)
            mp.Put("key"+string(randKey), "value"+string(randKey))
            if randKey%10 == 0 {
                size, _ := mp.Size()
                log.Println(fmt.Sprintf("Map size: %d", size))
            }
            time.Sleep(100 * time.Millisecond)
        }
    } else {
        panic("Connection failed, check your configuration.")
    }

    client.Shutdown()
}

First, we create an empty client configuration using the following line:

cfg := hazelcast.NewConfig()

Then, we set the cluster name and password via GroupConfig using the following lines:

cfg.GroupConfig().SetName("YOUR_CLUSTER_NAME")
cfg.GroupConfig().SetPassword("YOUR_CLUSTER_PASSWORD")

The cluster name and password are unique to your cluster. The client is authenticated using this password.

Then, we set the discovery token using the following lines:

discoveryCfg := config.NewCloudConfig()
discoveryCfg.SetEnabled(true)
discoveryCfg.SetDiscoveryToken("YOUR_CLUSTER_DISCOVERY_TOKEN")
cfg.NetworkConfig().SetCloudConfig(discoveryCfg)

This token is also unique to your cluster and used to discover Hazelcast cluster members.

To enable Hazelcast to send the client statistics to see them in the cluster's page, we use the following lines:

cfg.SetProperty(property.StatisticsEnabled.Name(), "true")
cfg.SetProperty(property.StatisticsPeriodSeconds.Name(), "1")

Now, we create the client with config using the following line:

client, _ := hazelcast.NewClientWithConfig(cfg)

This step creates the connection between your application and the cluster.

We get/create a map and put a simple entry ("key", "value") using the following lines, respectively:

mp, _ := client.GetMap("map")
mp.Put("key", "value")
val, _ := mp.Get("key")

Then, we check if the entry has been put successfully and let the code throw an exception if the value is not correct using the following lines. Finally, we put random entries with 100-millisecond intervals:

if val == "value" {
  log.Println("Connection Successful!")
  log.Println("Now, `map` will be filled with random entries.")
  rand.Seed(time.Now().UTC().UnixNano())
  for true {
    randKey := rand.Intn(100000)
    mp.Put("key"+string(randKey), "value"+string(randKey))
    if randKey%10 == 0 {
      size, _ := mp.Size()
      log.Println(fmt.Sprintf("Map size: %d", size))
    }
    time.Sleep(100 * time.Millisecond)
  }
} else {
  panic("Connection failed, check your configuration.")
}

client_with_ssl.go

This is the downloaded sample client code when your cluster has encryption enabled.

package main

import (
    "fmt"
    "log"
    "math/rand"
    "path/filepath"
    "time"

    hazelcast "github.com/hazelcast/hazelcast-go-client"
    "github.com/hazelcast/hazelcast-go-client/config"
    "github.com/hazelcast/hazelcast-go-client/config/property"
)

func main() {
    cfg := hazelcast.NewConfig()
    sslConfig := cfg.NetworkConfig().SSLConfig()
  sslConfig.SetEnabled(true)
  caFile,_ := filepath.Abs("./ca.pem")
  certFile,_ := filepath.Abs("./cert.pem")
  keyFile,_ := filepath.Abs("./key.pem")
  sslConfig.SetCaPath(caFile)
  sslConfig.AddClientCertAndEncryptedKeyPath(certFile, keyFile, "YOUR_SSL_PASSWORD")
    sslConfig.ServerName = "hazelcast.cloud"
    cfg.GroupConfig().SetName("YOUR_CLUSTER_NAME")
    cfg.GroupConfig().SetPassword("YOUR_CLUSTER_PASSWORD")
    discoveryCfg := config.NewCloudConfig()
    discoveryCfg.SetEnabled(true)
    discoveryCfg.SetDiscoveryToken("YOUR_CLUSTER_DISCOVERY_TOKEN")
    cfg.NetworkConfig().SetCloudConfig(discoveryCfg)
    cfg.SetProperty(property.StatisticsEnabled.Name(), "true")
    cfg.SetProperty(property.StatisticsPeriodSeconds.Name(), "1")

    client, _ := hazelcast.NewClientWithConfig(cfg)

        mp, _ := client.GetMap("map")
        mp.Put("key", "value")
        val, _ := mp.Get("key")
        if val == "value" {
            log.Println("Connection Successful!")
            log.Println("Now, `map` will be filled with random entries.")
            rand.Seed(time.Now().UTC().UnixNano())
            for true {
                randKey := rand.Intn(100000)
                mp.Put("key"+string(randKey), "value"+string(randKey))
                if randKey%10 == 0 {
                    size, _ := mp.Size()
                    log.Println(fmt.Sprintf("Map size: %d", size))
                }
                time.Sleep(100 * time.Millisecond)
            }
        } else {
            panic("Connection failed, check your configuration.")
        }

        client.Shutdown()
    }

The only difference between this one and the client.go is the lines that enable and configure TLS encryption on the client side:

caFile,_ := filepath.Abs("./ca.pem")
certFile,_ := filepath.Abs("./cert.pem")
keyFile,_ := filepath.Abs("./key.pem")
sslConfig.SetCaPath(caFile)
sslConfig.AddClientCertAndEncryptedKeyPath(certFile, keyFile, "YOUR_SSL_PASSWORD")
sslConfig.ServerName = "hazelcast.cloud"

You may want to move the 'pem' files to another directory. Then, you need to set 'ca', 'cert' and 'key' file directories accordingly.

More Configuration Options

Please refer to the Hazelcast Go Client Documentation for more information on the configuration options of the client.

Updated about a month ago

Go Client


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.