Skip to content

Commit

Permalink
自动检测集群节点AutoDetect默认值修改为false。公有云Redis一般放在代理背后,主从架构,如果开启自动检测,将会自动识别主从…
Browse files Browse the repository at this point in the history
…,导致得到无法连接的内网主从库地址。
  • Loading branch information
nnhy committed Mar 17, 2024
1 parent b06fc61 commit f37f3c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
18 changes: 10 additions & 8 deletions NewLife.Redis/FullRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public static FullRedis Create(String config)
/// <summary>键前缀</summary>
public String? Prefix { get; set; }

/// <summary>自动检测集群节点。默认true</summary>
public Boolean AutoDetect { get; set; } = true;
/// <summary>自动检测集群节点。默认false</summary>
/// <remarks>
/// 公有云Redis一般放在代理背后,主从架构,如果开启自动检测,将会自动识别主从,导致得到无法连接的内网主从库地址。
/// </remarks>
public Boolean AutoDetect { get; set; }

/// <summary>模式</summary>
public String? Mode { get; private set; }
Expand Down Expand Up @@ -115,14 +118,13 @@ public override void Init(String config)

if (config.IsNullOrEmpty()) return;

var dic =
config.Contains(',') && !config.Contains(';') ?
config.SplitAsDictionary("=", ",", true) :
config.SplitAsDictionary("=", ";", true);
var dic = ParseConfig(config);
if (dic.Count > 0)
{
if (dic.TryGetValue("ThrowOnFailure", out var str))
ThrowOnFailure = str.ToBoolean();
if (dic.TryGetValue("Prefix", out var str))
Prefix = str;
if (dic.TryGetValue("AutoDetect", out str))
AutoDetect = str.ToBoolean();
}

_configOld = config;
Expand Down
19 changes: 15 additions & 4 deletions NewLife.Redis/Redis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ public override void Init(String config)
if (pk != null && pk.Secret != null) connStr = pk.Unprotect(connStr);

// 默认分号分割,旧版逗号分隔。可能只有一个server=后续多个含逗号的地址
var dic =
connStr.Contains(';') || connStr.Split('=').Length <= 2 ?
connStr.SplitAsDictionary("=", ";", true) :
connStr.SplitAsDictionary("=", ",", true);
var dic = ParseConfig(connStr);
if (dic.Count > 0)
{
Server = dic["Server"]?.Trim();
Expand Down Expand Up @@ -249,6 +246,20 @@ public override void Init(String config)
_configOld = config;
}

/// <summary>分析配置连接字符串</summary>
/// <param name="connStr"></param>
/// <returns></returns>
protected IDictionary<String, String> ParseConfig(String connStr)
{
// 默认分号分割,旧版逗号分隔。可能只有一个server=后续多个含逗号的地址
var dic =
connStr.Contains(';') || connStr.Split('=').Length <= 2 ?
connStr.SplitAsDictionary("=", ";", true) :
connStr.SplitAsDictionary("=", ",", true);

return dic;
}

void IConfigMapping.MapConfig(IConfigProvider provider, IConfigSection section)
{
if (section != null && section.Value != null) Init(section.Value);
Expand Down

0 comments on commit f37f3c4

Please sign in to comment.