如何在没有系统属性的情况下在新的 Algolia API v3 中定义代理设置

How to define the proxy settings in the new Algolia API v3 without System properties

我在Java8.

下使用Algolia 3.9.0客户端

按照 docs you need to set up proxy settings using the system properties. I prefer not to use this mechanism because properties can interfere with other components' properties as they can set the same properties too (Read more about 中的建议)。有没有办法像 v2 客户端那样设置值?

 client.setProxy(new HttpHost(conf.getProxyHost(), conf.getProxyPort())); 

要使其按照您的意愿工作,您应该自定义 HttpRequester,稍后使用具有您的代理配置的 HttpClientConfiguration 使用新的 HttpClient 创建它的实例,这是一个有效的代码:

MyCustomRequester class:

public class MyCustomRequester implements HttpRequester {

    private final HttpClient httpClient;

    public MyCustomRequester(HttpClient httpClient) {
        this.httpClient = httpClient;
    }

    @Override
    public CompletableFuture<HttpResponse> performRequestAsync(HttpRequest request) {
        return ... httpClient.execute(...) ...;
    }

    @Override
    public void close() throws IOException {
   ...
    }

}

主要测试方法:

public static void main(String[] args) {
    SearchConfig config = new SearchConfig
            .Builder("undefined", "undefined").build();

    HttpClientConfiguration httpClientConfiguration = new HttpClientConfiguration();
    httpClientConfiguration.setProxyHost("host");
    httpClientConfiguration.setProxyPort(2020);

    HttpClient httpClient = // create a new httpClient with custom httpClientConfiguration;
    ...
    SearchClient client = new SearchClient(config, myCustomRequester);
}

If you want to inject your own HttpClient you can inject it by constructor into all the clients classes of library. The latter has to implement the HttpRequester interface. In that case you don’t need algoliasearch-apache anymore as a dependency.

有关详细信息,请参阅此 documentation