尝试在独立进程上启用tcp-ip设置HazelCast群集。 我的班级看起来像这样
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
int personId;
String name;
Person(){};
//getters and setters
}
Hazelcast已加载为
final Config config = createNewConfig(mapName);
HazelcastInstance node = Hazelcast.newHazelcastInstance(config);`
Config createNewConfig(mapName){
final PersonStore personStore = new PersonStore();
XmlConfigBuilder configBuilder = new XmlConfigBuilder();
Config config = configBuilder.build();
config.setClassLoader(LoadAll.class.getClassLoader());
MapConfig mapConfig = config.getMapConfig(mapName);
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setImplementation(personStore);
return config;
}
我的myhazelcast配置有这个
<tcp-ip enabled="true">
<member>machine-1</member>
<member>machine-2</member>
</tcp-ip>
我是否需要在xml中填充此标记?
第二个实例启动时出现此错误
com.hazelcast.nio.serialization.HazelcastSerializationException: No DataSerializerFactory registered for namespace: 0
2275 at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:98)
2276 at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:39)
2277 at com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSerializerAdapter.java:41)
2278 at com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:276)
任何帮助都非常有用。
答案 0 :(得分:2)
解决了我的问题,我有一个带有hazelcast-wm的pom.xml,所以我在捆绑的jar中没有真正的hazelcast jar。包括那个解决了我的问题。
答案 1 :(得分:0)
请注意,当您尝试在同一个VM中使用多个Hazelcast实例但是从不同的bundle初始化实例时,OSGi环境中也会出现同样的“No DataSerializerFactory注册为namespace:0”错误消息。原因是com.hazelcast.util.ServiceLoader.findHighestReachableClassLoader()方法有时会在Hazelcast初始化期间选择错误的类加载器(因为它不会总是选择你在配置上设置的类加载器),然后它最终使用DataSerializerFactory实例的空列表(因此导致错误消息,它无法找到ID为0的请求工厂)。以下显示了一种利用Java的上下文类加载器解决该问题的方法:
private HazelcastInstance createHazelcastInstance() {
// Use the following if you're only using the Hazelcast data serializers
final ClassLoader classLoader = Hazelcast.class.getClassLoader();
// Use the following if you have custom data serializers that you need
// final ClassLoader classLoader = this.getClass().getClassLoader();
final com.hazelcast.config.Config config = new com.hazelcast.config.Config();
config.setClassLoader(classLoader);
final ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(classLoader);
return Hazelcast.newHazelcastInstance(config);
} finally {
if(previousContextClassLoader != null) {
Thread.currentThread().setContextClassLoader(previousContextClassLoader);
}
}
}