问题描述
我试图找出如何配置。服务器端的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 提供的传输。
-- 米克
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)