我试图找出如何配置。一个服务器端的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.
[...]
}
}
我的问题是:
- 我的解决方案可以作为服务器端解决方案吗,即它可以处理并发请求吗?
- 是否有声明性(quarkus)方式(@RegisterRestClient.等)来实现相同的目标?
- 编辑
说清楚:我希望服务 B 能够根据 HTTP 获取请求和特定资源控制缓存。另外我想避免B提供的大文件服务的不必要传输。
-- 米克