Expand description
Defines a Sentinel type that connects to Redis sentinels and creates clients to master or replica nodes.
§Example
use redis::Commands;
use redis::sentinel::Sentinel;
let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let mut sentinel = Sentinel::build(nodes).unwrap();
let mut master = sentinel.master_for("master_name", None).unwrap().get_connection().unwrap();
let mut replica = sentinel.replica_for("master_name", None).unwrap().get_connection().unwrap();
let _: () = master.set("test", "test_data").unwrap();
let rv: String = replica.get("test").unwrap();
assert_eq!(rv, "test_data");
There is also a SentinelClient which acts like a regular Client, providing the
get_connection
and get_async_connection
methods, internally using the Sentinel
type to create clients on demand for the desired node type (Master or Replica).
§Example
use redis::Commands;
use redis::sentinel::{ SentinelServerType, SentinelClient };
let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let mut master_client = SentinelClient::build(nodes.clone(), String::from("master_name"), None, SentinelServerType::Master).unwrap();
let mut replica_client = SentinelClient::build(nodes, String::from("master_name"), None, SentinelServerType::Replica).unwrap();
let mut master_conn = master_client.get_connection().unwrap();
let mut replica_conn = replica_client.get_connection().unwrap();
let _: () = master_conn.set("test", "test_data").unwrap();
let rv: String = replica_conn.get("test").unwrap();
assert_eq!(rv, "test_data");
If the sentinel’s nodes are using TLS or require authentication, a full SentinelNodeConnectionInfo struct may be used instead of just the master’s name:
§Example
use redis::{ Commands, RedisConnectionInfo };
use redis::sentinel::{ Sentinel, SentinelNodeConnectionInfo };
let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let mut sentinel = Sentinel::build(nodes).unwrap();
let mut master_with_auth = sentinel
.master_for(
"master_name",
Some(&SentinelNodeConnectionInfo {
tls_mode: None,
redis_connection_info: Some(RedisConnectionInfo {
db: 1,
username: Some(String::from("foo")),
password: Some(String::from("bar")),
}),
}),
)
.unwrap()
.get_connection()
.unwrap();
let mut replica_with_tls = sentinel
.master_for(
"master_name",
Some(&SentinelNodeConnectionInfo {
tls_mode: Some(redis::TlsMode::Secure),
redis_connection_info: None,
}),
)
.unwrap()
.get_connection()
.unwrap();
§Example
use redis::{ Commands, RedisConnectionInfo };
use redis::sentinel::{ SentinelServerType, SentinelClient, SentinelNodeConnectionInfo };
let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let mut master_client = SentinelClient::build(
nodes,
String::from("master1"),
Some(SentinelNodeConnectionInfo {
tls_mode: Some(redis::TlsMode::Insecure),
redis_connection_info: Some(RedisConnectionInfo {
db: 0,
username: Some(String::from("user")),
password: Some(String::from("pass")),
}),
}),
redis::sentinel::SentinelServerType::Master,
)
.unwrap();
Structs§
- The Sentinel type, serves as a special purpose client which builds other clients on demand.
- An alternative to the Client type which creates connections from clients created on-demand based on information fetched from the sentinels. Uses the Sentinel type internally. This is basic an utility to help make it easier to use sentinels but with an interface similar to the client (
get_connection
andget_async_connection
). The type of server (master or replica) and name of the desired master are specified when constructing an instance, so it will always return connections to the same target (for example, always to the master with name “mymaster123”, or always to replicas of the master “another-master-abc”). - Holds the connection information that a sentinel should use when connecting to the servers (masters and replicas) belonging to it.
Enums§
- Enum defining the server types from a sentinel’s point of view.