问题描述
我试图找出如何配置。服务器端的rest客户端(即微服务A使用rest调用其他微服务B)使用http缓存。
背景是,通过网络传输的二进制实体可能非常大。整体性能可以受益于微服务 A 端的缓存,该缓存采用微服务 B 提供的 http 缓存标头和 etag。
我找到了一个似乎有效的解决方案,但我不确定它是否是一个合适的解决方案,可以与当前请求配合使用,并且可以随时在微服务 A 上发生。
@Inject
/* package private */ ManagedExecutor executor;
//
// Instead of using a declarative rest client we create it ourselves,because we can then supply a server-side cache: See ctor()
//
private ServiceBApi serviceClientB;
@ConfigProperty(name="serviceB.url")
/* package private */ String serviceBUrl;
@ConfigProperty(name="cache-entries")
/* package private */ int cacheEntries;
@ConfigProperty(name="cache-entrysize")
/* package private */ int cacheEntrySize;
@postconstruct
public void ctor()
{
// Create proxy ourselves,because we can then supply a server-side cache
final CacheConfig cc = CacheConfig.custom()
.setMaxCacheEntries(cacheEntries)
.setMaxobjectsize(cacheEntrySize)
.build();
final CloseableHttpClient httpClient = CachingHttpClientBuilder.create()
.setCacheConfig(cc)
.build();
final ResteasyClient client = new ResteasyClientBuilderImpl()
.httpEngine(new ApacheHttpClient43Engine(httpClient))
.executorService(executor)
.build();
final ResteasyWebTarget target = (ResteasyWebTarget) client.target(serviceBUrl);
this.serviceClientB = target.proxy(ServiceBApi.class);
}
@Override
public byte[] getDoc(final String id)
{
try (final Response response = serviceClientB.getDoc(id)) {
[...]
// Use normally and no need to handle conditional gets and caching headers and other HTTP protocol stuff here,because this does underlying impl.
[...]
}
}
我的问题是:
-- 编辑
澄清一下:我希望服务 B 能够根据 HTTP 获取请求和特定资源来控制缓存。另外我想避免不必要的大文件服务 B 提供的传输。
-- 米克
解决方法
假设您之前使用过使用 Quarkus' REST Client 的声明方式,您只需将客户端注入到您的 serviceB 消费类中。将调用服务 B 的方法应使用 @CacheResult
进行注释。这将根据传入的 id
缓存结果。另见Quarkus Cache Guide。
请注意:由于 Quarkus 和 Vert.x 都是关于非阻塞操作,您应该使用 async support of the REST Client。
@Inject
@RestClient
ServiceBApi serviceB;
...
@Override
@CacheResult(cacheName = "service-b-cache")
public Uni<byte[]> getDoc(final String id) {
return serviceB.getDoc(id).map(...);
}
...