我尝试使用Hazelcast进行缓存时遇到了错误。经过多次搜索及在GitHub上请求帮助,仍未找到解决方案。我的问题是,我想要在一个IMap中存储一个booster(增益器),这个IMap包含了uuid以及Booster对象。在Booster对象中,又包含了uuid、booster的类型、激活者以及激活时间。但在尝试将它保存到map中时,出现了错误。
下面是相关代码及遇到的错误信息:
ClientConfig clientConfig = new ClientConfig();
clientConfig.getSerializationConfig().setAllowOverrideDefaultSerializers(true);
clientConfig.getSerializationConfig()
.getCompactSerializationConfig()
.addSerializer(new CustomUUIDSerializer());
clientConfig.getNetworkConfig().addAddress("hazelcast-service:5701");
this.hazelcastInstance = HazelcastClient.newHazelcastClient(clientConfig);
以及自定义序列化类的代码:
public class CustomUUIDSerializer implements CompactSerializer<UUID> {
@Override
public UUID read(CompactReader reader) {
return UUID.fromString(Objects.requireNonNull(reader.readString("uuidField")));
}
@Override
public void write(CompactWriter writer, UUID object) {
writer.writeString("uuidField", object.toString());
}
@Override
public Class<UUID> getCompactClass() {
return UUID.class;
}
@Override
public String getTypeName() {
return "UUID";
}
}
遇到的第一个错误信息是:
java.lang.IllegalArgumentException: Compact serializer for the class 'class java.util.UUID' can not be registered as it overrides the default serializer for that class provided by Hazelcast.
当我尝试不使用自定义序列化器时,遇到了另一个错误:
com.hazelcast.nio.serialization.HazelcastSerializationException: The 'class java.util.UUID' cannot be serialized with zero configuration Compact serialization because this type is not supported yet. If you want to serialize 'class net.star.api.impl.common.booster.Booster' which uses this class in its fields, consider writing a CompactSerializer for either the 'class net.star.api.impl.common.booster.Booster' or the 'class java.util.UUID'.
我已经尝试了多种方法,但似乎都无法绕过这些错误,我需要一个能够有效解决这个问题的方法。