首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅沿已部署的服务在选定节点上分发分区IgniteCache

问仅沿已部署的服务在选定节点上分发分区IgniteCache
EN

Stack Overflow用户
提问于 2016-10-17 15:32:30
回答 1查看 330关注 0票数 1

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

其中一些服务正在使用Ignite,但我希望这些特定于服务的缓存只部署在关联的服务节点上。因此,我在缓存配置中添加了一个节点筛选器:

代码语言:javascript
复制
// 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"));

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

No server nodes found for cache client: MyServiceCache

当我停止第二个节点时,就会出现另一个日志消息:

代码语言:javascript
复制
[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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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());

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-18 19:49:14

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

代码语言:javascript
复制
cfg.setNodeFilter((node) -> Objects.equals(node.attribute("role"), NodeConfig.myServiceNode().getUserAttributes().get("role")));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40090369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档