java - 仅沿着已部署的服务在选定的节点上分发分区的 IgniteCache

标签 java ignite

我正在运行一个 Apache Ignite 集群,其中包含提供服务的各种节点。每个节点都被分配一个特定的服务组(自定义节点属性),描述该节点提供的服务,例如授权、付款、...

其中一些服务正在利用 Ignite 缓存,但我希望这些服务特定的缓存仅部署在关联的服务节点上。因此,我将节点过滤器添加到我的缓存配置中:

// configuration with custom attribute is provided at node start up
IgniteConfiguration nodeConfig = new IgniteConfiguration();
Map<String,String> nodeAttributes = Collections.singletonMap("role", "MyService");
nodeConfig.setUserAttributes(nodeAttributes);

CacheConfiguration cfg = new CacheConfiguration<>("MyServiceCache");
cfg.setNodeFilter((node) -> node.attribute("role") == nodeConfig .getUserAttributes().get("role"));

缓存部署按预期工作(至少没有显示错误)。但是,当我添加提供相同服务的第二个节点(出于扩展原因)时,会显示日志条目:

找不到缓存客户端的服务器节点:MyServiceCache

一旦我停止第二个节点,就会显示另一条日志消息:

[17:26:29] Topology snapshot [ver=9, servers=1, clients=0, CPUs=4, heap=1.7GB]
[17:26:29] All server nodes for the following caches have left the cluster: 'MyServiceCache'
[17:26:29] Must have server nodes for caches to operate.

据我了解,第一个节点应该仍然提供服务和缓存。所以这些消息对我来说没有多大意义。有人可以详细说明一下吗?

这是一个具体的例子:

  • 运行 MyService.java 两次或更多次
  • 停止第一个节点
  • 观察第二个节点的日志输出

NodeConfig.java

public class NodeConfig {

    public static IgniteConfiguration myServiceNode()
    {
        IgniteConfiguration nodeConfig = new IgniteConfiguration();
        Map<String,String> nodeAttributes = Collections.singletonMap("role", "myService");
        nodeConfig.setUserAttributes(nodeAttributes);
        return nodeConfig;
    }

}

CacheConfig.java

public class CacheConfig {

    public static CacheConfiguration<Long, String> myServiceCache() {

        CacheConfiguration<Long, String> cfg = new CacheConfiguration<>("MyServiceCache");

        cfg.setBackups(2);

        cfg.setNodeFilter((node) -> node.attribute("role") == NodeConfig.myServiceNode().getUserAttributes().get("role"));

        return cfg;
    }

}

MyService.java

public class MyService implements Service {

    @IgniteInstanceResource
    private Ignite ignite;
    private IgniteCache cache;

    @Override
    public void cancel(ServiceContext serviceContext) {
        System.out.println("Service " + serviceContext.name() + " cancelled.");
    }

    @Override
    public void init(ServiceContext serviceContext) throws Exception {
        System.out.println("Service " + serviceContext.name() + " initialized.");
    }

    @Override
    public void execute(ServiceContext serviceContext) throws Exception {

        CacheConfiguration config = CacheConfig.myServiceCache();
        cache = ignite.getOrCreateCache(config).withExpiryPolicy(new CreatedExpiryPolicy(Duration.ONE_MINUTE));

        System.out.println("Service " + serviceContext.name() + " executing.");
    }

    public static void main(String[] args) {

        Ignite ignite = Ignition.start(NodeConfig.myServiceNode());

        IgniteServices svcs = ignite.services(ignite.cluster().forAttribute("role", NodeConfig.myServiceNode().getUserAttributes().get("role")));

        svcs.deployNodeSingleton("MyService", new MyService());

    }

}

最佳答案

您不应使用 == 来比较属性值。这种情况效果很好:

cfg.setNodeFilter((node) -> Objects.equals(node.attribute("role"), NodeConfig.myServiceNode().getUserAttributes().get("role")));

关于java - 仅沿着已部署的服务在选定的节点上分发分区的 IgniteCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40090369/

相关文章:

java - 使用 Facebook 登录的应用程序在开发中无法正常工作!它只是加载并关闭- Android Studio

java - 点燃连续查询: Failed to initialize a remote filter

java - 打开 QueryParallelism 时,Apache Ignite 的结果有问题

Ignite 中类似 JavaSpaces 的模式

java - 如何使用 Apache ignite 作为后端层来计算/聚合报告 web 应用程序

java - 使用 Gson 如何将变量名称添加为 REST 调用的 json 的顶级参数

java初学者想学Hibernate

java - Apache 连接 Pool2 - POJO 作为关键

java - 同步,不总是正确的?

java - 点燃缓存错误