Rust Installation
Complete guide to install and configure Easy RMQ for Rust.
Requirements
Before you begin, ensure you have:
- Rust 1.70 or higher
- RabbitMQ server (or Docker)
- Tokio runtime (for async support)
Installation
Add to Cargo.toml
Add Easy RMQ to your Cargo.toml:
[dependencies]
easy-rmq-rs = { git = "https://github.com/skyapps-id/easy-rmq-rs" }
tokio = { version = "1", features = ["full"] }
Using Cargo Edit
cargo add easy-rmq-rs --git https://github.com/skyapps-id/easy-rmq-rs
Configuration
Basic Setup
Create a client with connection string and pool size:
use easy_rmq_rs::AmqpClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = AmqpClient::new(
"amqp://guest:guest@localhost:5672".to_string(),
10 // max pool size
)?;
Ok(())
}
Connection String Format
amqp://username:password@host:port/vhost
Examples:
// Local RabbitMQ with default credentials
"amqp://guest:guest@localhost:5672"
// Custom credentials
"amqp://admin:password@localhost:5672"
// With virtual host
"amqp://guest:guest@localhost:5672/my_vhost"
// Remote server
"amqp://user:pass@rabbitmq.example.com:5672"
// With SSL (AMQPS)
"amqps://user:pass@rabbitmq.example.com:5671"
Pool Size Configuration
Choose pool size based on your workload:
// Low concurrency (1-5 concurrent operations)
let client = AmqpClient::new("amqp://guest:guest@localhost:5672".to_string(), 5)?;
// Medium concurrency (5-20 concurrent operations)
let client = AmqpClient::new("amqp://guest:guest@localhost:5672".to_string(), 15)?;
// High concurrency (20+ concurrent operations)
let client = AmqpClient::new("amqp://guest:guest@localhost:5672".to_string(), 50)?;
Pool Size Guidelines:
- Development: 5-10 connections
- Production: 10-50 connections (based on workload)
- High Throughput: 50-100 connections
Environment Variables
Store configuration in environment variables:
use std::env;
fn get_rabbitmq_url() -> String {
env::var("RABBITMQ_URL")
.unwrap_or_else(|_| "amqp://guest:guest@localhost:5672".to_string())
}
fn get_pool_size() -> u32 {
env::var("RABBITMQ_POOL_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(10)
}
let client = AmqpClient::new(
get_rabbitmq_url(),
get_pool_size()
)?;
Create .env file:
RABBITMQ_URL=amqp://admin:password@localhost:5672
RABBITMQ_POOL_SIZE=20
Use with dotenv:
[dependencies]
dotenv = "0.15"
dotenv::dotenv().ok();
RabbitMQ Setup
Docker (Recommended)
Using Docker CLI
docker run -d --name rabbitmq \
-p 5672:5672 \
-p 15672:15672 \
rabbitmq:3-management
Using Docker Compose
Create docker-compose.yml:
version: '3.8'
services:
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
ports:
- "5672:5672" # AMQP port
- "15672:15672" # Management UI
environment:
RABBITMQ_DEFAULT_USER: admin
RABBITMQ_DEFAULT_PASS: password
volumes:
- rabbitmq_data:/var/lib/rabbitmq
restart: unless-stopped
volumes:
rabbitmq_data:
Start RabbitMQ:
docker-compose up -d
Local Installation
macOS
brew install rabbitmq
brew services start rabbitmq
Ubuntu/Debian
sudo apt-get update
sudo apt-get install rabbitmq-server
sudo systemctl start rabbitmq-server
Windows
Download and install from RabbitMQ official website
Verify Installation
Check if RabbitMQ is running:
# Check if port 5672 is open
netstat -an | grep 5672
# Or use curl to check management API
curl http://localhost:15672/api/overview
Access the Management UI at: http://localhost:15672
- Default username:
guest(oradminif using Docker Compose) - Default password:
guest(orpasswordif using Docker Compose)
Testing
Test Connection
use easy_rmq_rs::AmqpClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = AmqpClient::new(
"amqp://guest:guest@localhost:5672".to_string(),
10
)?;
// Try to publish a test message
client.publisher()
.publish_text("test", "Connection test")
.await?;
println!("✅ Connection successful!");
Ok(())
}
Health Check
async fn health_check(client: &AmqpClient) -> bool {
match client.publisher()
.publish_text("health.check", "ping")
.await
{
Ok(_) => true,
Err(_) => false,
}
}
Troubleshooting
Connection Refused
Problem: Connection refused error
Solutions:
- Verify RabbitMQ is running:
docker psorsystemctl status rabbitmq-server - Check port is accessible:
telnet localhost 5672 - Verify connection string format
- Check firewall settings
Authentication Failed
Problem: Authentication failed error
Solutions:
- Verify username and password
- Check user permissions in RabbitMQ Management UI
- Ensure user has access to the virtual host
What's Next
- Publisher - Learn about publishers
- Subscriber - Learn about subscribers
- Dependency Injection - Explore DI patterns