#[cfg(feature = "cluster")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
use asynq::backend::RedisBroker;
tracing_subscriber::fmt::init();
println!("Redis Cluster PubSub 示例");
println!("Redis Cluster PubSub Example\n");
let cluster_config = asynq::backend::RedisConnectionType::cluster(vec![
"redis://127.0.0.1:7000".to_string(),
"redis://127.0.0.1:7001".to_string(),
"redis://127.0.0.1:7002".to_string(),
])?;
println!("配置信息 / Configuration:");
println!();
let broker = RedisBroker::new(cluster_config).await?;
println!("✓ Broker 创建成功 / Broker created successfully");
println!();
match broker.get_pubsub().await {
Ok(pubsub) => {
println!("✓ PubSub 连接创建成功 / PubSub connection created successfully");
println!();
println!("说明 / Notes:");
println!(" - 使用 RESP3 协议和 push_sender 实现 / Uses RESP3 protocol with push_sender");
println!(" - 通过 tokio channel 接收推送消息 / Receives push messages via tokio channel");
println!(" - 订阅应通过连接对象的 subscribe() 方法完成 / Subscription should be done via connection.subscribe()");
println!();
let _stream = pubsub.into_on_message();
println!("✓ 消息流创建成功 / Message stream created successfully");
}
Err(e) => {
println!("✗ 创建 PubSub 连接失败 / Failed to create PubSub connection: {e}");
println!();
println!("可能的原因 / Possible reasons:");
println!(" - Redis Cluster 未运行 / Redis Cluster is not running");
println!(" - 节点地址不正确 / Node addresses are incorrect");
println!(" - 未启用 RESP3 协议 / RESP3 protocol is not enabled");
return Err(e.into());
}
}
println!();
println!("示例完成 / Example completed");
Ok(())
}
#[cfg(not(feature = "cluster"))]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}