具有Karaf实例的Infinispan集群

时间:2018-10-15 06:27:24

标签: apache-karaf infinispan

对于Infinispan来说我们是新手,对Apache Karaf来说也是新手。在Karaf中安装Infinispan很容易,我们确实编写了两个OSGi捆绑包以形成一个群集,其中有两个节点在一个主机上运行。我们在Infinispan网站(tutorial)的分布式缓存中使用了该教程。不幸的是,集群似乎没有构建,我们无法确定原因。任何帮助或朝正确方向的推动都将不胜感激。

在缓存中写入内容的捆绑软件代码如下:

import org.infinispan.Cache;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.context.Flag;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CacheProducer implements BundleActivator{

    private static Logger LOG = LoggerFactory.getLogger(CacheProducer.class );
    private static DefaultCacheManager cacheManager;

    @Override
     public void start( BundleContext context ) throws Exception{
        LOG.info( "Start Producer" );
        GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
        global.transport().clusterName("ClusterTest");

        // Make the default cache a distributed synchronous one
        ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.clustering().cacheMode(CacheMode.DIST_SYNC);

        // Initialize the cache manager
        cacheManager = new DefaultCacheManager(global.build(), builder.build());

        // Obtain the default cache
        Cache<String, String> cache = cacheManager.getCache();

        cache.put( "message", "Hello World!" );

        LOG.info( "Producer: whole cluster content!" );
        cache.entrySet().forEach(entry -> LOG.info(entry.getKey()+ ": " + entry.getValue()));
        LOG.info( "Producer: current cache content!" );
        cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP)
                    .entrySet().forEach(entry -> LOG.info(entry.getKey()+ ": " + entry.getValue()));
}

    @Override
    public void stop( BundleContext context ) throws Exception{
        cacheManager.stop();
    }

}

然后尝试打印出缓存中的内容的代码:

package metdoc81.listener;

import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.infinispan.Cache;
import org.infinispan.manager.DefaultCacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Activator implements BundleActivator{

    private static Logger LOG = LoggerFactory.getLogger(Activator.class);
    private static DefaultCacheManager cacheManager;

    public void start( BundleContext bundleContext ) throws Exception{
        LOG.info("start cluster listener");
        GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
        global.transport().clusterName("ClusterTest");

        // Make the default cache a distributed synchronous one
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.clustering().cacheMode(CacheMode.DIST_SYNC);

        // Initialize the cache manager
        cacheManager = new DefaultCacheManager(global.build(), builder.build());

        // Obtain the default cache
        Cache<String, String> cache = cacheManager.getCache();

        LOG.info("After configuration");
        cache.entrySet().forEach(entry -> LOG.info(entry.getKey()+ ": " + entry.getValue()));
        LOG.info("After logging");
    }

    public void stop( BundleContext bundleContext ) throws Exception{

    }

}

CacheProducer的打印有效,而Listener的打印无效。

1 个答案:

答案 0 :(得分:0)

我们自己找到了解决方案。

当您尝试在MacOS上运行代码时,仅在Windows正常运行时才会出现问题。 discussion at JBossDeveloper表示,MacOS上的多播路由存在问题。即使他们在示例代码中添加了解决方法,您仍然必须在运行它时添加-Djava.net.preferIPv4Stack=true标志,或者您必须添加以下两行代码:

Properties properties = System.getProperties();
properties.setProperty( "java.net.preferIPv4Stack", "true" );