使用 Azure SQL 作为配置存储的 AppFabric 缓存

AppFabric Caching with Azure SQL as configuration storage

AppFabric 缓存服务是否可以与 Azure SQL 数据库一起作为缓存集群配置的存储?

它绝对支持 SQL 服务器,正如它在此处所述:https://msdn.microsoft.com/en-us/library/ee790826.aspx,但只字未提 Azure SQL 支持。 Azure SQL 它是 HA 技术,所以看起来像 "organically" 存储配置的方式应该是高可用的,以保持缓存集群正常运行。

更新。 AppFabric 配置 UI 不提供使用 SQL 凭据进行访问的可能性 - 此处只有 "Intergrated Security" 可用选项它不是 Azure SQL 服务的选项,但可以通过 DistributedCacheService.exe.config.

手动编辑连接字符串

<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
  <dataCacheConfig cacheHostName="AppFabricCachingService">
    ...
    <clusterConfig provider="System.Data.SqlClient" connectionString="Server=tcp:azuredbnamehere.database.windows.net,1433;Database=AppFabricConfigHolderTest;User ID=user@azuredbnamehere;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" />
  </dataCacheConfig>
...
</configuration>

更新 2. 要从我开始,我已经将 AppFabric 配置为使用我的本地 SQL 服务器并成功了——我能够创建新的缓存、检查集群状态、管理缓存主机,没有任何问题。

然后我将生成的 AppFabric DB 部署到 Azure SQL 并在从安全对象中删除 Windows 用户主体后成功,因为 Azure SQL 不支持它。

然后我手动修改了 AppFabric 主机配置文件 (DistributedCacheService.exe.config) 中的连接字符串以指向 Azure SQL 数据库而不是本地数据库。

我卡在这里了——Administrative PowerShell SnapIn 仍然尝试连接到我的本地数据库实例。看起来 关于集群配置存储的信息是重复的 并且不仅 DistributedCacheService.exe.config 知道它。

更新 3. 我反编译了 SQL AppFabric 服务器注册的实用程序 - DistributedCache.SqlConfiguration.exe。我已经意识到 "registration" 阶段发生了什么:

using (PowerShell powerShellHost = PowerShellHelper.GetPowerShellHost())
{
    powerShellHost.AddCommand("Set-CacheConnectionString");
    powerShellHost.AddParameter("Provider", "System.Data.SqlClient");
    powerShellHost.AddParameter("ConnectionString", connectionString);
    PowerShellHelper.TraceCommands(powerShellHost);
    powerShellHost.Invoke();
    PowerShellHelper.TraceErrorsAndWarnings(powerShellHost);
}

我们到了!

PS > Get-Help Set-CacheConnectionString

NAME Set-CacheConnectionString

SYNOPSIS Persists the connection string for temporary usage. The string can then be retrieved with Get-CacheConnectionString. Note that this command does not change the connection string used by the cache cluster configuration store.

事实证明集群配置存储信息确实重复。它的存储位置我已经意识到改变反编译执行 Set-CacheConnectionString.

的 PowerShell 模块
using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\Microsoft\AppFabric\V1.0\Temp"))

更新 4. AppFabric Cache Administrative PowerShell SnapIn 使用的最有趣的事情不同的注册表路径(反编译显示):HKLM\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration.修改 ConnectionString 属性 值后,我终于得到了 Use-CacheCluster 命令。

集群也启动了,现在看起来可以运行了。

我们可以 "cheat" 并使用更新中描述的方法设置 AppFabric 缓存集群,但遗憾的是, 确保我们确实使用 Integrated Security 的代码被缝合了进入 PowerShell 管理管理单元:

// Microsoft.ApplicationServer.Caching.Configuration.ConfigurationBase
...
if (provider.Equals("System.Data.SqlClient"))
{
    ConfigurationBase.ValidateSqlConnectionString(connectionString);
}
...
internal static void ValidateSqlConnectionString(string connectionString)
{
    SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
    if (!sqlConnectionStringBuilder.IntegratedSecurity || !string.IsNullOrEmpty(sqlConnectionStringBuilder.UserID) || !string.IsNullOrEmpty(sqlConnectionStringBuilder.Password))
    {
        int errorCode = 17032;
        string sqlAuthenticationNotSupported = Resources.SqlAuthenticationNotSupported;
        throw new DataCacheException("DistributedCache.ConfigurationCommands", errorCode, sqlAuthenticationNotSupported);
    }
}

如果您尝试 Register-CacheHost(Add-Host 和其他一些命令),您将面临以下错误:

Register-CacheHost : ErrorCode:SubStatus:Only Windows authentication is supportedwith SQL Server provider. Specify a valid connection string for Windows authentication without any User ID or Password.
At line:1 char:1
+ Register-CacheHost -Provider "System.Data.SqlClient" -ConnectionString "Server=t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Register-CacheHost], DataCacheException
    + FullyQualifiedErrorId : SqlAuthenticationNotSupported,Microsoft.ApplicationServer.Caching.Configuration.Commands.RegisterCacheHostCommand

因此,使用 Azure SQL 作为缓存集群配置持有者的唯一现实选项从头开始编写自定义配置提供程序: https://msdn.microsoft.com/en-us/library/ff718169.aspx 或尝试使用不同名称下已实现的 SqlServer 提供程序 (Microsoft.ApplicationServer.Caching.SqlServerCustomProvider) 来欺骗 PowerShell 模块(未测试)。

UPDATE. 我已经通过欺骗 AppFabric 并使用不同名称的内置 SQL 服务器提供程序测试了该方法。它就像一个魅力。