0

我试图找出如何配置。一个服务器端的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提供的大文件服务的不必要传输。

-- 米克

4

1 回答 1

0

假设您之前使用过使用Quarkus 的 REST 客户端的声明方式,您只需将客户端注入到您的 serviceB 消费类中。将调用服务 B 的方法应使用 注释@CacheResult。这将根据传入的id. 另请参阅Quarkus 缓存指南

请注意:由于 Quarkus 和 Vert.x 都是关于非阻塞操作的,您应该使用REST Client 的异步支持


    @Inject
    @RestClient
    ServiceBApi serviceB;

    ...
    @Override
    @CacheResult(cacheName = "service-b-cache")
    public Uni<byte[]> getDoc(final String id) {
        return serviceB.getDoc(id).map(...); 
    }
    ...
于 2021-03-01T12:38:09.683 回答