Hazelcast ClassNotFound的客户端中使用近缓存(Hazelcast ClassN

2019-11-05 08:05发布

我尝试以下列方式使用Hazelcast(3.9.2,3.11没有区别):我Hazelcast服务器(成员)。 我运行它们专用的,不嵌入。 我不想教Hazelcast成员我想在其中存储类。 我用捆绑hazelcast.xml文件,并做了以下插件(3.9.2)

<replicatedmap name="default">
    <in-memory-format>BINARY</in-memory-format>
    <statistics-enabled>true</statistics-enabled>
</replicatedmap>

我也被激活TCP,而不是多播(真/假)就是说所有的变化我做到了。 我开始与一个会员听127.0.0.1:5701

然后我尝试Hazelcast客户端连接到成员用于存储和检索地图(主要ReplicatedMaps,但也地图不要在我的情况下工作)

我的客户端代码看起来是这样的(高速缓存仅仅是一个序列化的类无属性):

public class Main {
  public static final String HAZELCAST_INSTANCE_NAME = "HAZI";
  public static final String REPLICATEDMAP_NAME = "REP_MAP";
  public static final String MAP_NAME = "NORMAL_MAP";

public static void main(String[] args) {
    init();
    HazelcastInstance instance = HazelcastClient.getHazelcastClientByName(HAZELCAST_INSTANCE_NAME);
    Map<String, Object> repMap = instance.getReplicatedMap(REPLICATEDMAP_NAME);
    repMap.put("MyKey", new Cache());
    System.err.println("Retrieve " + repMap.get("MyKey"));
    Map<String, Object> normalMap = instance.getReplicatedMap(MAP_NAME);
    normalMap.put("MyKey", new Cache());
    System.err.println("Retrieve " + normalMap.get("MyKey"));
    System.exit(1);
}

private static void init() {
    ClientConfig cfg = new ClientConfig();
    cfg.setInstanceName(HAZELCAST_INSTANCE_NAME);
    cfg.addNearCacheConfig(defineNearCache(REPLICATEDMAP_NAME));
    cfg.addNearCacheConfig(defineNearCache(MAP_NAME));
    // for analysis in the hazelcast management console
    cfg.getProperties().put("hazelcast.client.statistics.enabled", "true");
    cfg.getProperties().put("hazelcast.client.statistics.period.seconds", "60");
    cfg.getNetworkConfig().addAddress("127.0.0.1:5701");
    if (HazelcastClient.newHazelcastClient(cfg) == null) {
        System.err.println(" !!! ERROR in Cache Config !!!");
    }
}
private static NearCacheConfig defineNearCache(String mapName) {
    EvictionConfig evictionConfig = new EvictionConfig()
            .setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT)
            .setSize(200);
    return new NearCacheConfig()
            .setName(mapName)
            .setInMemoryFormat(InMemoryFormat.BINARY)
            .setInvalidateOnChange(true)
            .setEvictionConfig(evictionConfig);
}

}

我现在的问题是:使用这个代码,我得到一个ClassNotFoundError想放放东西到复制的地图或地图规律,但在专用服务器Hazelcast(成员),而不是在客户端。

    SCHWERWIEGEND: [127.0.0.1]:5701 [dev] [3.9.2] hz._hzInstance_1_dev.event-3 caught an exception while processing task:com.hazelcast.spi.impl.eventservice.impl.LocalEventDispatcher@eeed098
com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: de.empic.hazelwar.model.Cache
        at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:224)
        at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
        at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:185)
        at com.hazelcast.map.impl.DataAwareEntryEvent.getValue(DataAwareEntryEvent.java:90)
        at com.hazelcast.client.impl.protocol.task.replicatedmap.AbstractReplicatedMapAddEntryListenerMessageTask.handleEvent(AbstractReplicatedMapAddEntryListenerMessageTask.java:92)
        at com.hazelcast.client.impl.protocol.task.replicatedmap.AbstractReplicatedMapAddEntryListenerMessageTask.entryAdded(AbstractReplicatedMapAddEntryListenerMessageTask.java:132)
        at com.hazelcast.replicatedmap.impl.ReplicatedMapEventPublishingService.dispatchEvent(ReplicatedMapEventPublishingService.java:82)
        at com.hazelcast.replicatedmap.impl.ReplicatedMapService.dispatchEvent(ReplicatedMapService.java:247)
        at com.hazelcast.spi.impl.eventservice.impl.LocalEventDispatcher.run(LocalEventDispatcher.java:64)
        at com.hazelcast.util.executor.StripedExecutor$Worker.process(StripedExecutor.java:225)
        at com.hazelcast.util.executor.StripedExecutor$Worker.run(StripedExecutor.java:208)
Caused by: java.lang.ClassNotFoundException: de.empic.hazelwar.model.Cache
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.hazelcast.nio.ClassLoaderUtil.tryLoadClass(ClassLoaderUtil.java:173)
    at com.hazelcast.nio.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:147)
    at com.hazelcast.nio.IOUtil$ClassLoaderAwareObjectInputStream.resolveClass(IOUtil.java:591)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1868)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
    at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:219)
    ... 10 more

每当我从客户端配置删除近缓存配置,所有工作得完美,但我没有当然的近缓存。

我怎么会错过吗?

Answer 1:

@magicroomy,我跑在两个3.9.2和3.11相同。 我可以确认:

  • 如果更改复制映射到地图,它的工作原理有或无近缓存。
  • 当使用复制地图,如果近缓存定义,除了在服务器端抛出。
  • 如果没有近缓存,ReplicatedMap也适用。

我创建了一个github上的问题,以及: https://github.com/hazelcast/hazelcast/issues/14210



Answer 2:

我的问题是使用3.11.1版本hazelcast的解决。



文章来源: Hazelcast ClassNotFound using Near Cache in Client