batata-consul-client
A Rust client library for HashiCorp Consul, supporting service discovery, health checking, key-value store, sessions, ACL, and more.
Features
- KV Store: Get, put, delete keys with CAS support and distributed locking
- Agent: Register/deregister services, manage health checks, TTL updates
- Catalog: Query nodes and services across the cluster
- Health: Health check queries with status filtering
- Session: Distributed locking and leader election
- ACL: Token, policy, and role management
Installation
Add to your Cargo.toml:
[]
= "0.0.1"
Quick Start
use ;
async
Configuration
Default Configuration
let client = new?;
From Environment Variables
let client = from_env?;
Supported environment variables:
| Variable | Description |
|---|---|
CONSUL_HTTP_ADDR |
Consul server address (e.g., http://127.0.0.1:8500) |
CONSUL_HTTP_TOKEN |
ACL token |
CONSUL_HTTP_AUTH |
HTTP basic auth (username:password) |
CONSUL_HTTP_SSL |
Enable HTTPS (true or 1) |
CONSUL_HTTP_SSL_VERIFY |
Verify TLS certificates (false or 0 to skip) |
CONSUL_CACERT |
CA certificate path |
CONSUL_CLIENT_CERT |
Client certificate path |
CONSUL_CLIENT_KEY |
Client key path |
CONSUL_NAMESPACE |
Namespace (Enterprise only) |
CONSUL_PARTITION |
Partition (Enterprise only) |
Builder Pattern
use Duration;
let client = builder
.address
.scheme
.token
.datacenter
.timeout
.build?;
API Examples
Service Registration
use ;
let registration = new
.with_id
.with_address
.with_port
.with_tags
.with_check;
client.agent.service_register.await?;
Service Discovery
// Get healthy instances of a service
let = client.health.service_healthy.await?;
for service in services
KV Store with CAS
let kv = client.kv;
// Get current value
let = kv.get.await?;
if let Some = pair
Blocking Queries
use Duration;
use QueryOptions;
let opts = new
.with_wait;
let = client.health.service.await?;
// meta.last_index can be used for the next blocking query
Session and Locking
use ;
// Create a session
let session_req = new
.with_name
.with_ttl
.with_behavior;
let = client.session.create.await?;
// Acquire a lock
let = client.kv.acquire.await?;
if acquired
// Destroy the session
client.session.destroy.await?;
Running Examples
Make sure you have a Consul server running locally:
# Start Consul in dev mode
# Run examples
License
Apache-2.0