我正在尝试使用 Bucket4j 和 Infinispan 实现速率限制器。下面是我用来在 Infinispan 缓存中存储 LockFreeBucket 对象的代码:
public boolean isRateLimited(String key) {
Bucket bucket = cacheManager.getCache("rate-limiter").get(key,
Bucket.class);
if (bucket == null) {
bucket = bucketSupplier.get();
Cache<String, Bucket> cache = cacheManager.getCache("rate-limiter");
cache.put(key, bucket);
}
return !bucket.tryConsume(1);
}
当它尝试放置密钥时,即cache.put(key,bucket),我收到异常org.infinispan.client.hotrod.exceptions.HotRodClientException :: Unable to marshall object of type [io.github.bucket4j.local .LockFreeBucket]] 的根本原因
java.io.NotSerializedException:io.github.bucket4j.local.LockFreeBucket 下面是我的 RemoteCacheManagerConfiguration
@Bean
public RemoteCacheManager getRemoteCacheManager() {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder
.addServer()
.host("somehost")
.port(11222)
.marshaller(new JavaSerializationMarshaller())
.addJavaSerialWhiteList("io.github.bucket4j.*")
.security()
.ssl()
.sniHostName("infinispan")
.trustStoreFileName("./cacerts 2")
.trustStorePassword("changeit".toCharArray())
.authentication()
.username("some")
.password("somepassword")
.addContextInitializer(new CreateProtoImpl())
.clientIntelligence(ClientIntelligence.BASIC);
return new RemoteCacheManager(configurationBuilder.build());
}
LockFreeBucket
未实现 java.io.Serializable
。最简单的方法是使用 Protostream 适配器,尽管序列化似乎是一个复杂的类:https://infinispan.org/docs/stable/titles/encoding/encoding.html#creating-protostream-adapter_marshalling
您在使用bucket4j-infinispan吗?