【问题标题】:Unable to connect Twemproxy using StackExchange redis in C#无法在 C# 中使用 StackExchange redis 连接 Twemproxy
【发布时间】:2015-06-24 14:17:22
【问题描述】:

我正在尝试使用 TWEMPROXY 服务器的 IP 地址使用 StackExchange redis 执行下面的 C# 代码,它给出了以下错误:

未处理的类型异常 'StackExchange.Redis.RedisConnectionException' 发生在 StackExchange.Redis.dll

附加信息:无法连接到 redis 服务器;要创建断开连接的多路复用器,请禁用 中止连接失败。 PING 上的 Socket 失败

但是,当我使用本地主机时,它可以正常工作并将数据存储在本地 Redis 缓存中

带有'localhost'的代码示例如下:

using System;
namespace WinRedis
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            new MainClass().testingCache();
            Console.ReadLine();
        }
        public void testingCache()
        {
            SimpleCache<User> cache = new RedisCache<User>("mycache", "localhost:6379");
            cache.Put ("user1", new User () { Name = "test", Email = "test@email.com", Password = "secured" });
            User user = cache.Get("user1");
            Console.WriteLine(user);
        }
    }
    [Serializable]
    class User{
        public string Name { set; get; }
        public string Email { set; get; }
        public string Password {set;get;}

        public override string ToString()
        {
            return "User(Name: " + Name + ", Email: " + Email + ", Password: " + Password + ")";
        }
    }
}


using System;
using StackExchange.Redis;

namespace WinRedis
{
    public class RedisCache<T> : SimpleCache<T>
    {
        private ConnectionMultiplexer redisConnection = null;
        private IDatabase redis = null;
        private string name = null;
        public RedisCache(string name = "redis-cache",
            string connectionOptions = "localhost:6379")
        {
            this.redisConnection = ConnectionMultiplexer.Connect(connectionOptions); 
            this.redis = redisConnection.GetDatabase ();
            this.name = name;

        }

        public T Get(string key)
        {
            byte[] result = this.redis.HashGet (name, key);

            if (result == null)
                return default(T);
            else
                return result.Deserialize<T>();

        }

        public void Put(string key, T value)
        {
            this.redis.HashSet (name, key, value.SerializeToByteArray() );
        }

        public void Close()
        {
            this.redisConnection.Close ();
        }
    }
}

对于上面的相同代码,当我将 localhost 替换为 TWEMPROXY IP 地址时,它会出错。

【问题讨论】:

    标签: redis stackexchange.redis twemproxy


    【解决方案1】:

    https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Configuration.md#twemproxy

    建议也许这样的事情可能会起作用......也许

    var options = new ConfigurationOptions
                {
                    EndPoints = { "your_endpoint:port" },
                    Proxy = Proxy.Twemproxy
                };
    
                ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(options);
    

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 1970-01-01
      • 2016-02-18
      • 2016-11-05
      • 2015-02-06
      • 2015-11-06
      • 1970-01-01
      • 2018-11-04
      • 2016-10-18
      相关资源
      最近更新 更多