63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/hazelcast/hazelcast-go-client"
|
|
"github.com/relex/aini"
|
|
)
|
|
|
|
func main() {
|
|
var hazelcastHosts []string
|
|
|
|
id, err := aini.ParseFile("./hazelcast_stack.toml")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
for _, h := range id.Groups["all"].Hosts {
|
|
fmt.Printf("scan ansible host : %s => %s:%d\n", h.Name, h.Vars["ansible_host"], h.Port)
|
|
hazelcastHosts = append(hazelcastHosts, h.Vars["ansible_host"])
|
|
}
|
|
//fmt.Printf("%+v\n", id.Groups["all"].Vars)
|
|
|
|
clientConfig := hazelcast.NewConfig()
|
|
fmt.Println("Cluster name: ", clientConfig.GroupConfig().Name())
|
|
|
|
clientConfig.NetworkConfig().AddAddress(hazelcastHosts...)
|
|
fmt.Println("Cluster discovery: ", clientConfig.NetworkConfig().Addresses())
|
|
|
|
hz, err := hazelcast.NewClientWithConfig(clientConfig)
|
|
if err != nil {
|
|
log.Fatalf("fail to start hazelcast client %+v", err)
|
|
}
|
|
|
|
// Shutdown this hazelcast client
|
|
defer hz.Shutdown()
|
|
|
|
// Get the Distributed Map from Cluster.
|
|
mp, _ := hz.GetMap("myDistributedMap")
|
|
//Standard Put and Get.
|
|
mp.Put("key", "value")
|
|
|
|
v, err := mp.Get("key")
|
|
if err != nil {
|
|
log.Fatalf("error reading key into map myDistributedMap", err)
|
|
} else {
|
|
fmt.Printf("myDistributedMap['key'] = %+v\n", v)
|
|
}
|
|
|
|
//Concurrent Map methods, optimistic updating
|
|
mp.PutIfAbsent("somekey", "somevalue")
|
|
mp.ReplaceIfSame("key", "value", "newvalue")
|
|
|
|
v, err = mp.Get("key")
|
|
if err != nil {
|
|
log.Fatalf("error reading key into map myDistributedMap", err)
|
|
} else {
|
|
fmt.Printf("myDistributedMap['key'] = %+v\n", v)
|
|
}
|
|
|
|
}
|