【问题标题】:Redis Server and connecting with multiple clientsRedis 服务器并连接多个客户端
【发布时间】:2012-06-15 22:14:46
【问题描述】:

我有一个在 Windows 2008 机器上运行的 Redis 服务器。我有多个网站,每个网站都使用BasicRedisClientManager. 这个实例在应用程序启动时设置一次并设置到Application[] 商店。这样它就可以在所有用户中重复使用。我遇到的问题是一个站点在专用服务器上运行,而其他站点在运行 Redis 服务器的 localhost 上运行。一个是生产,另一个是新工作/演示的测试地点。远程客户端工作正常,但本地主机客户端不会与服务器通信,并且似乎处于锁定/死机状态。当我运行该站点时,我只会遇到堆栈溢出,并且该站点会自行回收。下面是我所拥有的设置的一些示例代码。我看到有一些关于在客户端之间创建锁的信息,但我不确定这是否是我需要采取的路线。

http://www.servicestack.net/docs/redis-client/distributed-locking-with-redis

示例代码:

    protected override void Application_Start(object sender, EventArgs e)
    {
        /*
         * Create basicRedisClientManager for redis.
         */
        var redisHost = ConfigurationManager.AppSettings["RedisHostIp"].ToString();
        BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(redisHost);
        Application["BasicRedisClientManager"] = basicRedisClientManager;

     .....
    }

然后在类构造函数中使用

    public CacheManager()
    {
        if (Application["BasicRedisClientManager"] != null)
        {
            /*local variable*/
            basicRedisClientManager = (BasicRedisClientManager)Application["BasicRedisClientManager"];
        }
    }

在课堂上使用管理器。

        if (basicRedisClientManager != null)
        {
            using (var redisClient = (RedisClient)basicRedisClientManager.GetClient())
            {
                if (!saveToInProcOnly && redisClient != null)
                {
                    using (var redis = redisClient.GetTypedClient<T>())
                    {
                        redis.SetEntry(key, value);
                    }
                }
            }
        }

这个逻辑在所有网站上都是一样的。

非常感谢任何见解!

【问题讨论】:

    标签: c# .net nosql redis


    【解决方案1】:

    我不知道BasicRedisClientManager,但我建议您使用BookSleeve 客户端库。关于连接重用,答案是不要重用。也就是说,按照 BookSleeve 的作者的建议,每次需要时重新创建连接。

    请参阅this questionaccepted answer 了解如何创建防弹的 Redis 客户端连接。

    【讨论】:

    • 抱歉耽搁了,但我们不能使用书套,因为它需要 .net4。我必须相信除了使用书套之外,还有一个答案。我是否没有实施从多个网站连接到 Redis 的权利?我认为一个新客户端会从我的第二个站点实例中启动并使用该客户端进行通信。
    猜你喜欢
    • 2021-08-17
    • 2018-07-29
    • 2020-02-21
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多