c# - 关于ServiceStack.Redis的一些问题

标签 c# redis

  1. 支持哪种代理?如果有的话,我该如何使用它?

  2. 是否支持主题标签?或者类似的东西?

  3. 除了单元测试之外,还有完整的用例吗? (即虽然我阅读了GitHub官方文档,但我仍然不明白如何使用它。)

Official GitHub Docs

最佳答案

您正在链接到 Configure Redis Sentinel Servers文档,所以我假设您想要配置 ServiceStack.Redis 实例以使用 Redis Sentinel 配置。

注意Redis Sentinel是 Redis 的高可用性解决方案(它不是代理),我建议阅读 Redis's official Redis Sentinel docs了解它是如何工作的。

首先,您需要设置 Redis Sentinel 配置。一种流行的设置是拥有1x Redis Master2x Redis 从属副本,此外,每个服务器上通常都有一个单独的 Redis Sentinel 实例(用于监视正在运行的 Redis 实例)运行 redis 实例的服务器。为了方便与您一起开发,可以使用 ServiceStack's redis-config项目,可以轻松地在同一服务器上运行1x master2x Slave3x Sentinel进程。

然后,当您运行 Redis 配置时(假设是 localhost),您可以使用 ServiceStack 的 RedisSentinel 类通过传入每个哨兵实例的 IP 和端口来连接到它,例如:

var sentinelHosts = new[]{ 
   "127.0.0.1:26380", 
   "127.0.0.1:26381", 
   "127.0.0.1:26382", 
};
var sentinel = new RedisSentinel(sentinelHosts, masterName: "mymaster");
IRedisClientsManager redisManager = sentinel.Start();

Note: you don't have to include the IP and ports for Redis master or Redis slave instances as they'll be automatically discovered and can even change. You also can start with a single Redis Sentinel Instance as RedisSentinel will also be able to discover other sentinels in the same "mymaster" group.

一旦调用sentinel.Start(),它将返回一个已配置的IRedisClientsManager,它维护一个打开的Redis客户端连接池,并监听Redis的sentinel服务器实例对 Redis Sentinel 配置的任何更改,例如以防 Redis 主服务器故障转移到正在运行的从服务器副本之一。

您应该将 redisManager 维护为单例,并使用它来解析您需要的所有 Redis 客户端,例如如果使用 IOC,您可以将其注册为单例:

container.Register<IRedisClientsManager>(redisManager);

每当您需要与 Redis 连接时,都可以使用 GetClient() 来解析与当前主实例的 Redis 连接:

using (var redis = redisManager.GetClient())
{
}

并且在using语句结束时(或者调用.Dispose()时),你打开的Redis连接将会被返回到内部连接池,等待下次解析。

关于c# - 关于ServiceStack.Redis的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588011/

相关文章:

c# - 如何检测 .NET 中的不可打印字符?

c# - C# 事件参数有什么作用?

C# 停止一次又一次调用 MDI 子项

python - 使用 'spawn'启动Redis进程但面临TypeError:无法腌制_thread.lock对象

java - Spring Data Redis过期键

memory-management - 为什么删除一半键时redis内存使用量没有减少

c# - 在 ActionFilterAttribute 中设置 TempData

c# - 在 C# 中使用数组实现通用堆栈

redis - 当 Redis 中有超过 1 个命令时,我是否应该始终使用流水线?

node.js - 错误 : Redis connection to 127. 0.0.1:6379 失败 - 连接 EMFILE