如何连接到缓存
将您的应用程序连接到 NCache 提供安全、可靠且高可用的缓存连接,支持本地、云端和容器化环境。本页介绍如何使用。 NCache API(主要是 CacheManager.GetCache())用于建立与单个或多个分布式缓存的连接、配置重试、保持连接、负载均衡以及使用安全凭据进行身份验证。无论您是在本地网络、公有云 IP 还是负载均衡器上部署, NCache 确保客户端与集群之间无缝通信。
您还将学习如何使用 CacheConnectionOptions 自定义缓存连接、连接到集群缓存和客户端缓存,以及如何排查常见的连接问题。本指南提供 .NET、Java、Python 和 Node.js 的示例,帮助开发人员快速集成。 NCache 在保持性能、可扩展性和容错性的同时,将其集成到现有应用程序中。
默认连接(本地网络): 如果你正在使用 NCache 在本地网络上,您可以使用 私人服务器IP。然而,这并不总是可能的,因此, NCache 提供了多种 API 来克服如下所述的潜在问题。例如,有时您可能需要在单个应用程序中同时初始化集群缓存和客户端缓存。为此,您可以使用…… 获取缓存 方法初始化两个缓存。同样,如果您希望防止未经授权的缓存访问并启用了安全性,则可以使用 安全API.
与 LoadBalancer 的连接:但是,如果您的应用程序部署了 负载均衡器 连接到所有服务器来管理事务负载,您可以使用 NCache 是负载均衡器 API 来确保缓存连接性。
使用云上的公共 IP 进行连接:如果您使用此选项,您可以通过 客户端.ncconf 文件。这个过程详细 如下。。如果您想连接到 NCache Cloud 服务器使用其私有 IP 地址并且需要通过公共网络进行通信,则可以为缓存服务器分配一个公共 IPv4 地址 NCache 通过执行 设置CacheServerPublicIP 小命令。
使用端口转发的连接:部署时 NCache in 容器化环境,一个有效的方法是 分配静态 IP 地址 至 NCache 自定义网络中的容器。此设置允许客户端应用程序使用一致的 IP 地址可靠地连接到缓存服务器,从而促进跨不同网络环境的稳定通信。
重要提示
.NET 8.0 的线程池设置
对于 Windows,必须设置
UseWindowsThreadPool标记为 true。对于已构建的应用程序,设置UseWindowsThreadPool标志在 运行时配置.json 文件:{ "runtimeOptions": { "configProperties": { "System.Threading.ThreadPool.UseWindowsThreadPool": true } } }在开发阶段,此文件将在每次编译时被覆盖。因此,定义
UseWindowsThreadPool属性,如下所示:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <UseWindowsThreadPool>true</UseWindowsThreadPool> </PropertyGroup> </Project>要了解更多关于
UseWindowsThreadPool标记,点击 开始.对于 Linux,必须设置
MinimumThreads标记为 100 ,在 运行时配置.json, 如下:{ "runtimeOptions": { "configProperties": { "System.Threading.ThreadPool.MinThreads": 100 } } }要了解更多关于
MinThreads标记,点击 开始.
硬件需求
使用前 NCache 客户端 API 应确保满足以下先决条件:
如何连接到单个缓存
NCache 提供了一个 ICache 获取实例的接口 NCache的缓存。 此外, CacheManager 类允许您通过 GetCache 方法。以下示例连接到名为 demoCache 的缓存,该缓存处于运行状态。
try
{
// Specify the cache name
string cacheName = "demoCache";
// Connect to cache
ICache cache = CacheManager.GetCache(cacheName);
}
catch (OperationFailedException ex)
{
// NCache specific exception
if (ex.ErrorCode == NCacheErrorCodes.NO_SERVER_AVAILABLE)
{
// Make sure NCache Service and cache is running
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (ConfigurationException ex)
{
if(ex.ErrorCode == NCacheErrorCodes.SERVER_INFO_NOT_FOUND)
{
// client.ncconf must have server information
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
// Argument exception occurs in case of empty string name
}
注意:
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
如何连接到多个缓存
使用 GetCache 通过这种方法,您可以在单个应用程序中连接到多个缓存。这将帮助您使用一个应用程序管理多个缓存的数据。以下示例演示了如何从同一客户端连接到两个缓存:demoCache 和 demoClusteredCache。
// Specify cache names
string cacheName1 = "demoCache";
string cacheName2 = "demoClusteredCache";
// Connect to the caches
ICache cache1 = CacheManager.GetCache(cacheName1);
ICache cache2 = CacheManager.GetCache(cacheName2);
如何使用 CacheConnectionOptions 连接到缓存
此 CacheConnectionOptions 允许在建立到缓存的连接时指定缓存属性的值。 这些值是相同的,可以通过 客户端.ncconf 文件中。
注意:
通过指定的任何配置 CacheConnectionOptions 将覆盖 客户端.ncconf 对于那个特定的客户。
在此示例中,值 重试间隔, 连接重试, 启用KeepAlive和 保活时间间隔 属性可以改变。 对于此应用程序,将使用这些值而不是客户端配置文件中指定的值。
// Create new CacheConnectionOptions instance
var options = new CacheConnectionOptions();
// Specify the cache connection options to be set
options.RetryInterval = TimeSpan.FromSeconds(5);
options.ConnectionRetries = 2;
options.EnableKeepAlive = true;
options.KeepAliveInterval = TimeSpan.FromSeconds(30);
// Specify the cache name
string cacheName = "demoCache";
// Connect to cache with CacheConnectionOptions
ICache cache = CacheManager.GetCache(cacheName, options);
如何使用负载均衡器的 CacheConnectionOptions 连接到缓存
通常,应用程序部署使用负载平衡器来防止来自压倒性服务器的请求,即防止客户端直接访问服务器。在这种情况下,为了确保您的集群连接到所有服务器, NCache 提供 是负载均衡器 API,如下所示:
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.ServerList = new List<ServerInfo>()
{
new ServerInfo(true, "20.200.20.40", 9800)
};
ICache cache = CacheManager.GetCache("demoCache", cacheConnectionOptions);
注意:
了解更多有关如何 NCache 处理这些负载均衡器 开始.
如何使用安全 API 连接到缓存
您也可以使用 SecurityOptions 类用于配置缓存连接的身份验证,可以通过启用基于 gMSA 的无密码身份验证或提供显式用户凭据来实现。
使用 gMSA 安全连接
NCache 允许您使用 集团托管服务账户 (gMSA)用于在连接到缓存时进行安全、无密码的身份验证。启用 gMSA 身份验证后,客户端应用程序将使用计算机上配置的 gMSA 身份,该身份由 Active Directory 管理。
重要提示
请注意,客户端应用程序也必须在已配置的 gMSA 用户下运行。
以下示例演示了如何使用基于 gMSA 的身份验证连接到缓存。 CreateWithGMSA 方法。
string cacheName = "demoCache";
var cacheConnectionOptions = new CacheConnectionOptions();
// If the user wants to use GMSA, they can set the Security property to use GMSA credentials
cacheConnectionOptions.Security = SecurityOptions.CreateWithGMSA();
ICache cache = CacheManager.GetCache(cacheName, cacheConnectionOptions);
使用 CreateWithCredentials API 进行连接
本示例展示了如何使用以下方式连接到缓存: CreateWithCredentials 用户名/密码认证方法。
string cacheName = "demoCache";
var cacheConnectionOptions = new CacheConnectionOptions();
// If the user wants to use username/password credentials, they can set the Security property to use those credentials
cacheConnectionOptions.Security = SecurityOptions.CreateWithCredentials("your-username", "your-password");
ICache cache = CacheManager.GetCache(cacheName, cacheConnectionOptions);
如何连接到集群和客户端缓存
注意:
使用 GetCache 通过一次调用连接到集群和客户端缓存的方法 不推荐.
可以使用以下方式在单个调用中连接到集群和客户端缓存: GetCache 方法。 这将在单个应用程序中初始化两个缓存,从而帮助您通过一个应用程序管理多个缓存的数据。
注意:
如果本地缓存连接到客户端缓存,则序列化方式必须与集群缓存相同。两个缓存都必须事先配置好,并且必须处于运行状态。
以下示例使用以下命令连接到两个缓存 GetCache 方法。
// Specify the cache names
string clusteredCache = "demoCache";
string clientCache = "myClientCache";
// Create and set CacheConnectionOptions for clustered cache
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.LoadBalance = true;
cacheConnectionOptions.ConnectionRetries = 5;
// Create and set CacheConnectionOptions for Client Cache
CacheConnectionOptions clientCacheConnectionOptions = new CacheConnectionOptions();
clientCacheConnectionOptions.LoadBalance = true;
clientCacheConnectionOptions.ConnectionRetries = 5;
clientCacheConnectionOptions.Mode = IsolationLevel.OutProc;
// Connect to the caches in a single call
ICache cache = CacheManager.GetCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
如何使用公网IP连接到Cache
如果您已订阅 NCache Cloud 并选择公共 IP 连接选项。您需要修改 客户端.ncconf 文件不同,并确保提到了缓存服务器的公共 IP,而不管缓存服务器是否也有在缓存集群中使用的私有 IP。
请参阅 客户端.ncconf 了解更多信息。
注意:
如果您在使用时无法连接到缓存服务器 NCache Cloud 如果您使用的是私有 IP 地址,而需要改用公共网络,则需要配置公共 IP 地址。为此,请运行以下命令: 设置CacheServerPublicIP cmdlet 到有问题的缓存服务器,为缓存服务器分配一个公共 IPv4 地址 NCache 簇。
故障排除
端口不可访问
有时,由于您的防火墙配置,客户端可能无法连接到缓存。
解决方法: 使用我们的详细信息 安装指南 更改您的防火墙设置。
无法找到 client.ncconf
有时客户可能无法找到 客户端.ncconf 连接到缓存时的文件。 发生这种情况时 NCache 目录(%NCHOME%) 未设置。
解决方法
If %NCHOME% 未设置,请按照以下步骤为其设置环境变量:
- 用鼠标右键单击 开始 按钮并选择 系统.
- 在右侧窗格中,单击 高级系统设置.
- 在 系统属性 窗口,转到 先进的 标签并点击 环境变量 按钮。
- 在 环境变量 窗下,下 系统变量, 点击 全新发布.
- 对于 变量名称,输入
%NCHOME%. - 对于 变量值,输入
C:\Program Files\NCache. - 点击 OK.
- 现在更新系统路径相同 环境变量 窗口中,找到 途径 变量并单击 编辑.
- 在 编辑环境变量 窗口中,单击 全新发布 并添加
%NCHOME%\bin到列表中。 这确保了这些工具在全球范围内可用。 - 点击 OK 关闭每个窗口。
没有可用于处理请求的服务器
有时客户端可能无法连接到缓存,因为客户端/服务器端口被防火墙阻止。
解决方法
确保客户端/服务器端口(在哪个端口上) NCache 服务启动并接受不同的客户端连接)不会被防火墙阻止。 该端口的默认值为“9800”。 您可以更改默认值 Alachisoft.NCache.Service.exe.config or Alachisoft.NCache.Service.dll.config 文件中。
无法与服务器节点通信
由于集群端口被防火墙阻止,集群节点之间也可能无法通信。
Error: "Unable to communicate with server node"
解决方法
检查您的防火墙是否允许客户端/服务器端口和集群端口。集群端口是集群中节点之间通信的端口。您可以从以下位置更改这些端口的默认值: NCache 管理中心。 如果您在中指定了端口范围 NCache 管理中心,解除对该范围内所有端口的封锁。
客户端套接字死锁
由于长时间等待来自缓存服务器的响应,您可能会在客户端套接字上遇到死锁情况。 这也可能导致大量等待线程,从而导致性能问题。
解决方法
步骤 1:服务器端的更改
进入服务配置文件:
- Windows: Alachisoft.NCache.Service.dll.config 位于 %NCHOME%\bin\服务
- Linux: Alachisoft.NCache.Daemon.dll.config 位于 /opt/ncache/bin/服务
在配置文件中添加以下行:
<add key ="NCacheServer.EnableBadClientDetection" value = "true"/>
价值 true 表示 BadClientDetection 已在客户端套接字上启用。这意味着,如果客户端套接字出现死锁情况,套接字将被重置。此属性的默认值为 false.
<add key ="NCacheServer.ClientSocketSendTimeout" value = "10"/>
当您启用 BadClientDetection,您可以指定发生死锁时重置套接字的时间间隔。此属性的默认值为 10 并且该值不能小于 1.
还引入了一些其他属性 Alachisoft.NCache.Service.exe.config 帮助解决这个问题。 请按如下方式配置这些属性:
<add key ="NCacheServer.EventPriorityRatio" value="30"/>
在服务器端,优先级队列用于事件和缓存操作。您可以通过名为的属性配置此队列的事件和缓存操作的比例 EventPriorityRatio。此属性的默认值是 30 这表明 30:70 用于事件和缓存操作。此属性的值不能小于 1.
<add key ="NCacheServer.EventBulkCount" value="50"/>
缓存服务器现在会批量向客户端发送事件。通过此属性,您可以设置批量发送的项目数量。默认情况下,此值为 50 并且该值不能小于 1. 客户端版本可用 4124 以上。
<add key ="NCacheServer.EventBulkCollectionInterval" value="2"/>
此属性用于设置时间间隔(以秒为单位),在此时间间隔后,即使批量计数未达到指定值,也会触发批量事件。 EventBulkCount 用户指定的。此属性的默认值为 2秒 并且该值不能小于 1.
重新启动 NCache 服务以使这些更改生效。
步骤 2:客户端的更改
请在您的 应用配置/网页配置 文件:
<configuration>
<appSettings>
<add key ="NCacheClient.AsynchronousEventNotification" value="true"/>
<add key ="NCacheClient.NumberofEventProccesingThreads" value="2"/>
</appSettings>
</configuration>
AsynchronousEventNotification:指定在客户端是否以异步或同步方式触发事件。默认值为 true 表示事件将异步触发。值 false 意味着事件将在客户端同步触发。NumberofEventProcessingThreads:当用户将事件配置为同步触发时,此标志用于指定在客户端处理这些事件的线程数。过大的值可能会导致性能问题。
参见
.NET: Alachisoft.NCache。客户 命名空间。
Java的: com.alachisoft.ncache.client 命名空间。
Python: ncache.客户端 模块。
节点.js: 缓存 类。