创建多个具有相同接口的 HTTP 客户端

Create multiple HTTP clients with same interface

我们需要能够连接到多个 Elasticsearch 服务器。我们有一个简单的 Elasticsearch 客户端,使用 Micronaut 的声明式方法定义。

但是,作为一个多租户环境,我们需要能够定义许多这样的客户端。这些客户端中的每一个显然都有不同的 URL 并且需要使用不同的 HTTPFilter 进行身份验证。

Micronaut 是一个专注于编译时的框架,我如何动态创建许多这样的 bean,由配置选项定义?

更新: 我看到 @Factory@EachBean 注释相结合可能是一种很有前途的方法,但声明性 HTTP 客户端是一个接口,而不是具体的 class。我如何仅基于接口实例化这样的客户端?

https://docs.micronaut.io/latest/guide/index.html#eachBean

正常创建声明式@Client 接口,除了 2 个细节:

  1. 使其指向 url 本地“/proxy-elastic”
  2. 向每个声明的方法添加一个@Header("X-Elastic-Cluster") String cluster
@Client("/proxy-elastic")
public interface DeclarativeHttpClient {
    @Get("/connectors/{name}")
    void diplay(@Header(value = "X-Elastic-Cluster") String cluster, String name);
}

然后,创建一个 ProxyFilter 并根据 Header 值改变 HttpRequest (https://docs.micronaut.io/latest/guide/index.html#proxyClient)

@Filter("/proxy-elastic/**")
public class KafkaConnectClientProxy extends OncePerRequestHttpServerFilter {
    @Inject
    ProxyHttpClient client;

    @Override
    protected Publisher<MutableHttpResponse<?>> doFilterOnce(HttpRequest<?> request, ServerFilterChain chain) {
        // retrieve the config for this cluster
        String cluster = request.getHeaders().get("X-Elastic-Cluster");
        var config = getConfigForCluster(cluster);
        URI newURI = URI.create(config.url);

        // call kafka connect with proper URL and Auth
        return Publishers.map(client.proxy(
                request.mutate()
                        .uri(b -> b
                                .scheme(newURI.getScheme())
                                .host(newURI.getHost())
                                .port(newURI.getPort())
                                .replacePath(StringUtils.prependUri(
                                        newURI.getPath(),
                                        request.getPath().substring("/proxy-elastic".length())
                                ))
                        )
                        .basicAuth(config.basicAuthUsername, config.basicAuthPassword)
        ), response -> response.header("X-My-Response-Header", "YYY"));
    }
}