我们需要能够连接到多个 Elasticsearch 服务器。我们有一个简单的 Elasticsearch 客户端,使用 Micronaut 的声明式方法进行定义。
然而,作为一个多租户环境,我们需要能够定义许多这样的客户端。这些客户端中的每一个显然都有不同的 URL,并且需要使用不同的 HTTPFilter 进行身份验证。
Micronaut 是一个专注于编译时的框架,我如何动态创建许多由配置选项定义的此类 bean?
更新:
我发现@Factory与@EachBean注释相结合可能是一种有前途的方法,但声明性 HTTP 客户端是一个接口,而不是一个具体的类。如何仅基于接口实例化这样的客户端?
请参阅https://docs.micronaut.io/latest/guide/index.html#eachBean
通常创建声明性 @Client 接口,除了 2 个细节:
@Client("/proxy-elastic")
public interface DeclarativeHttpClient {
@Get("/connectors/{name}")
void diplay(@Header(value = "X-Elastic-Cluster") String cluster, String name);
}
Run Code Online (Sandbox Code Playgroud)
然后,创建一个 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"));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
899 次 |
| 最近记录: |