Consul
Distributed Storebackend/consul
Consul is a service discovery and configuration management tool. It provides a distributed key-value store that's perfect for configuration management and service coordination in microservices architectures.
📦 Installation
go get github.com/azrod/kivigo/backend/consul
Requires a running Consul server or cluster. Consul provides strong consistency guarantees through the Raft consensus algorithm.
Dependencies
- Consul server (version 1.8+)
- Network connectivity to Consul cluster
✨ Features
✅Basic Operations
✅Batch Operations
✅Health Checks
✅Persistence
✅Multi-DC Support
✅ACLs
❌Watches
❌Service Discovery
🚀 Usage
- Configuration
- Basic Usage
- Health Check
package main
import (
"github.com/azrod/kivigo"
"github.com/azrod/kivigo/backend/consul"
)
func main() {
// Basic configuration
opt := consul.DefaultOptions()
opt.Address = "localhost:8500"
// Custom configuration with auth
customOpt := &consul.Config{
Address: "consul.example.com:8500",
Token: "your-acl-token",
Scheme: "https",
Datacenter: "dc1",
}
// Create backend
kvStore, err := consul.New(customOpt)
if err != nil {
panic(err)
}
defer kvStore.Close()
// Create client
client, err := kivigo.New(kvStore)
if err != nil {
panic(err)
}
}
package main
import (
"context"
"fmt"
"log"
"github.com/azrod/kivigo"
"github.com/azrod/kivigo/backend/consul"
)
type ServiceConfig struct {
Name string `json:"name"`
Port int `json:"port"`
Replicas int `json:"replicas"`
Enabled bool `json:"enabled"`
}
func main() {
// Setup
opt := consul.DefaultOptions()
opt.Address = "localhost:8500"
kvStore, err := consul.New(opt)
if err != nil {
log.Fatal(err)
}
defer kvStore.Close()
client, err := kivigo.New(kvStore)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// Store service configuration
config := ServiceConfig{
Name: "auth-service",
Port: 8080,
Replicas: 3,
Enabled: true,
}
err = client.Set(ctx, "services/auth-service/config", config)
if err != nil {
log.Fatal(err)
}
// Retrieve configuration
var retrievedConfig ServiceConfig
err = client.Get(ctx, "services/auth-service/config", &retrievedConfig)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Service Config: %+v\n", retrievedConfig)
// List all service configurations
keys, err := client.List(ctx, "services/")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Service keys: %v\n", keys)
}
// Consul health check example
err := kvStore.Health(ctx)
if err != nil {
log.Printf("Consul unhealthy: %v", err)
} else {
log.Println("Consul is healthy")
}
📝 Notes
- Consul provides strong consistency through Raft consensus
- All writes go through the leader, reads can be eventually consistent
- Use ACL tokens for production security
- Consul supports multi-datacenter deployments
- Consider using Consul Connect for service mesh capabilities