You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache.Ignite.NET:如何定义缓存特定亲和性的节点

在Ignite.NET中指定节点缓存特定亲和性的实现方案

当然可以!在Ignite.NET里,你完全可以通过自定义亲和性策略,精准控制"customer"和"member"这类特定亲和性的缓存落到指定节点上,完美适配你的测试场景需求。下面是具体的实现步骤:

1. 给目标节点打上专属标识

首先,在启动节点的时候,给每个节点设置自定义属性,用来区分它们的"职责"——比如给要缓存customer数据的节点标记cacheGroup=customer,给缓存member数据的节点标记cacheGroup=member。示例代码如下:

// 启动专门处理customer缓存的节点
var customerNodeConfig = new IgniteConfiguration
{
    NodeConfiguration = new NodeConfiguration
    {
        UserAttributes = new Dictionary<string, object> { { "cacheGroup", "customer" } }
    }
};
Ignition.Start(customerNodeConfig);

// 启动专门处理member缓存的节点
var memberNodeConfig = new IgniteConfiguration
{
    NodeConfiguration = new NodeConfiguration
    {
        UserAttributes = new Dictionary<string, object> { { "cacheGroup", "member" } }
    }
};
Ignition.Start(memberNodeConfig);

2. 实现自定义亲和性策略

接下来,我们需要自定义一个亲和性函数,继承AffinityFunctionBase,重写分区分配的逻辑,让特定类型的缓存分区只映射到对应标识的节点上。示例代码:

public class GroupedAffinityFunction : AffinityFunctionBase
{
    // 给不同类型的键分配固定分区
    public override int GetPartition(object key)
    {
        // 这里可以根据键的类型、缓存名称或者自定义规则区分
        if (key is CustomerKey)
            return 0; // customer类键分配到分区0
        else if (key is MemberKey)
            return 1; // member类键分配到分区1
        return base.GetPartition(key);
    }

    // 将分区映射到对应标识的节点
    public override IEnumerable<IEnumerable<IClusterNode>> AssignPartitions(AffinityFunctionContext context)
    {
        // 筛选出标记为customer的节点
        var customerNodes = context.TopologySnapshot
            .Where(n => n.Attributes.TryGetValue("cacheGroup", out var val) && val.ToString() == "customer")
            .ToList();
        
        // 筛选出标记为member的节点
        var memberNodes = context.TopologySnapshot
            .Where(n => n.Attributes.TryGetValue("cacheGroup", out var val) && val.ToString() == "member")
            .ToList();

        // 把分区0绑定到customer节点,分区1绑定到member节点
        return new List<IEnumerable<IClusterNode>>
        {
            customerNodes,
            memberNodes
        };
    }
}

3. 给缓存配置自定义亲和性策略

最后,在创建缓存的时候,指定我们刚才写的自定义亲和性函数,这样缓存就会按照我们的规则落到指定节点上:

// 创建customer缓存,使用自定义亲和性策略
var customerCacheConfig = new CacheConfiguration("customerCache")
{
    Affinity = new GroupedAffinityFunction()
};
var customerCache = ignite.GetOrCreateCache<int, Customer>(customerCacheConfig);

// 创建member缓存,使用自定义亲和性策略
var memberCacheConfig = new CacheConfiguration("memberCache")
{
    Affinity = new GroupedAffinityFunction()
};
var memberCache = ignite.GetOrCreateCache<int, Member>(memberCacheConfig);

验证方式

测试的时候,你可以通过以下方式确认配置是否生效:

  • 查看节点属性:ignite.GetCluster().GetLocalNode().Attributes,确认节点的cacheGroup标识正确
  • 检查分区映射:用cache.GetPartition(key)获取键对应的分区,再通过ignite.GetAffinity(cacheName).MapPartitionToNodes(partition)查看该分区对应的节点,确认是否和预期一致

内容的提问来源于stack exchange,提问作者sa.he

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:03:22