简体   繁体   English

StackExchange.Redis - 如何在运行时更改配置?

[英]StackExchange.Redis - How can I change configuration at runtime?

Basically, I have a "DON'T DO THIS" Sentinel scenario. 基本上,我有一个“不要这样”的 Sentinel场景。 Because Sentinel is not safe in such scenario, I've implemented the following 因为Sentinel在这种情况下不安全,所以我实现了以下内容

var main   = "192.168.XXX.YY:6379,abortConnect=false";
var backup = "192.168.XXX.YY:6379,abortConnect=false";

IConnectionMultiplexer redis = ConnectionMultiplexer.Connect(main);

redis.ConnectionFailed += (src, args) =>
{
    if ((src as ConnectionMultiplexer).Configuration != backup) {
        using (var writer = new StringWriter()) {
            writer.Write(backup);

            (src as ConnectionMultiplexer).Configure(writer);
            /**
              * Just for checking. It does not save
              **/
            (src as ConnectionMultiplexer).GetDatabase().StringSet("aaa", "bbb");
        }
    }
};

So, when my main connection is down, I change the configuration, by calling (src as ConnectionMultiplexer).Configure(writer) , so that ConnectionMultiplexer can use the new configuration. 因此,当我的主连接断开时,我通过调用(src作为ConnectionMultiplexer)更改配置。配置(编写器) ,以便ConnectionMultiplexer可以使用新配置。 However, ConnectionMultiplexer continue to use the old one. 但是,ConnectionMultiplexer继续使用旧的。

Question: How can I change ConnectionMultiplexer.configuration in the ConnectionFailed event ? 问题:如何在ConnectionFailed事件中更改ConnectionMultiplexer.configuration?

I looked at the source code of the library, it seems there is no desired functionality. 我查看了库的源代码 ,似乎没有所需的功能。 There is internal method Reconfigure, but it tries to connect to other servers from the configuration. 有内部方法重新配置,但它尝试从配置连接到其他服务器。

I would suggest you to refactor, if your application is not very large. 如果你的申请不是很大,我建议你重构一下。 Make a wrapper over ConnectionMultiplexer, pass wrapper to objects where the connection is used. 在ConnectionMultiplexer上创建一个包装器,将包装器传递给使用连接的对象。 We do wrap method GetConnection, which returns all the links on a single object. 我们做了包装方法GetConnection,它返回单个对象上的所有链接。 All who need the connection will call this method, no needs to store connection. 所有需要连接的人都会调用此方法,无需存储连接。 Inside the wrapper OnFailed subscribe to an event handler to create a new connection to a Backup. 在包装器内部OnFailed订阅事件处理程序以创建到Backup的新连接。

Not sure if that would be acceptable, not exactly switching the config but more like rebuilding the multiplixer 不确定这是否可以接受,不是完全切换配置,而是更像重建多路复用器

private static Lazy<IConnectionMultiplexer> _redisMux = new Lazy<ConnectionMultiplexer>(CreateMultiplexer);
public static IConnectionMultiplexer Multiplexer { get { return _redisMux.Value; } }

private const string Main   = "192.168.XXX.YY:6379,abortConnect=false";
private const string Backup = "192.168.XXX.YY:6379,abortConnect=false";

private static string ActiveConfig = Main;

private static ConnectionMultiplexer CreateMultiplexer()
{
    var mux = ConnectionMultiplexer.Connect(ActiveConfig));
    mux.ConnectionFailed += OnConnectionFailed;

    return mux;
}

[MethodImpl(MethodImplOptions.Synchronized)]
private static void OnConnectionFailed(object sender, ConnectionFailedEventArgs e)
{
    ActiveConfig = Backup;

    try { Multiplexer.Dispose(); } catch { }
    _redisMux = new Lazy<ConnectionMultiplexer>(CreateMultiplexer);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM