【问题标题】:Quarkus server-side http-cacheQuarkus 服务器端 http-cache
【发布时间】:2021-02-25 11:53:50
【问题描述】:

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

-- 米克

【问题讨论】:

    标签: resteasy quarkus quarkus-rest-client


    【解决方案1】:

    假设您之前使用过使用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(...); 
        }
        ...
    

    【讨论】:

    • 是的,我在创建初始帖子之前阅读了缓存指南。缓存 API 与我的用例不同。我想使用 HTTP 内置缓存,这意味着缓存标头和 etag,因为我希望微服务 B 控制缓存传输。
    • 嗯,现在我明白了! RESTEasy 对此有自己的解决方案,但它看起来与您的解决方案非常相似:docs.jboss.org/resteasy/docs/4.6.0.Final/userguide/html_single/…
    猜你喜欢
    • 2015-04-08
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多