org.elasticsearch.client.RestClientBuilder.setHttpClientConfigCallback()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(13.1k)|赞(0)|评价(0)|浏览(168)

本文整理了Java中org.elasticsearch.client.RestClientBuilder.setHttpClientConfigCallback方法的一些代码示例,展示了RestClientBuilder.setHttpClientConfigCallback的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RestClientBuilder.setHttpClientConfigCallback方法的具体详情如下:
包路径:org.elasticsearch.client.RestClientBuilder
类名称:RestClientBuilder
方法名:setHttpClientConfigCallback

RestClientBuilder.setHttpClientConfigCallback介绍

[英]Sets the HttpClientConfigCallback to be used to customize http client configuration
[中]设置用于自定义http客户端配置的HttpClientConfigCallback

代码示例

代码示例来源:origin: spring-projects/spring-data-elasticsearch

builder.setHttpClientConfigCallback(clientBuilder -> {

代码示例来源:origin: apache/incubator-gobblin

builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder
builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder

代码示例来源:origin: testcontainers/testcontainers-java

private RestClient getClient(ElasticsearchContainer container) {
  if (client == null) {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
      new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD));
    client = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
      .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
      .build();
  }
  return client;
}

代码示例来源:origin: apache/nifi

.setHttpClientConfigCallback(httpClientBuilder -> {
  if (sslContext != null) {
    httpClientBuilder = httpClientBuilder.setSSLContext(sslContext);

代码示例来源:origin: apache/storm

/**
   * Creates a new {@link RestClient} using given {@link EsConfig}.
   *
   * @return {@link RestClient} for Elasticsearch connection
   */
  public RestClient construct() {
    RestClientBuilder builder = RestClient.builder(esConfig.getHttpHosts());
    if (esConfig.getMaxRetryTimeoutMillis() != null) {
      builder.setMaxRetryTimeoutMillis(esConfig.getMaxRetryTimeoutMillis());
    }
    if (esConfig.getDefaultHeaders() != null) {
      builder.setDefaultHeaders(esConfig.getDefaultHeaders());
    }
    if (esConfig.getFailureListener() != null) {
      builder.setFailureListener(esConfig.getFailureListener());
    }
    if (esConfig.getHttpClientConfigCallback() != null) {
      builder.setHttpClientConfigCallback(esConfig.getHttpClientConfigCallback());
    }
    if (esConfig.getRequestConfigCallback() != null) {
      builder.setRequestConfigCallback(esConfig.getRequestConfigCallback());
    }
    if (esConfig.getPathPrefix() != null) {
      builder.setPathPrefix(esConfig.getPathPrefix());
    }
    return builder.build();
  }
}

代码示例来源:origin: dadoonet/fscrawler

private static RestClientBuilder buildRestClient(Elasticsearch settings) {
  List<HttpHost> hosts = new ArrayList<>(settings.getNodes().size());
  settings.getNodes().forEach(node -> hosts.add(HttpHost.create(node.getDecodedUrl())));
  RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[hosts.size()]));
  if (settings.getUsername() != null) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.getUsername(), settings.getPassword()));
    builder.setHttpClientConfigCallback(httpClientBuilder ->
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
  }
  return builder;
}

代码示例来源:origin: dadoonet/fscrawler

private static RestClientBuilder buildRestClient(Elasticsearch settings) {
  List<HttpHost> hosts = new ArrayList<>(settings.getNodes().size());
  settings.getNodes().forEach(node -> hosts.add(HttpHost.create(node.getDecodedUrl())));
  RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[hosts.size()]));
  if (settings.getUsername() != null) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.getUsername(), settings.getPassword()));
    builder.setHttpClientConfigCallback(httpClientBuilder ->
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
  }
  return builder;
}

代码示例来源:origin: dadoonet/fscrawler

private static RestClientBuilder buildRestClient(Elasticsearch settings) {
  List<HttpHost> hosts = new ArrayList<>(settings.getNodes().size());
  settings.getNodes().forEach(node -> hosts.add(HttpHost.create(node.getDecodedUrl())));
  RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[hosts.size()]));
  if (settings.getUsername() != null) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(settings.getUsername(), settings.getPassword()));
    builder.setHttpClientConfigCallback(httpClientBuilder ->
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
  }
  return builder;
}

代码示例来源:origin: apache/metron

/**
 * Creates an Elasticsearch client from settings provided via the global config.
 *
 * @return new client
 */
public static ElasticsearchClient create(Map<String, Object> globalConfig) {
 ElasticsearchClientConfig esClientConfig = new ElasticsearchClientConfig(
   getEsSettings(globalConfig));
 HttpHost[] httpHosts = getHttpHosts(globalConfig, esClientConfig.getConnectionScheme());
 RestClientBuilder builder = RestClient.builder(httpHosts);
 builder.setRequestConfigCallback(reqConfigBuilder -> {
  // Modifies request config builder with connection and socket timeouts.
  // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_timeouts.html
  reqConfigBuilder.setConnectTimeout(esClientConfig.getConnectTimeoutMillis());
  reqConfigBuilder.setSocketTimeout(esClientConfig.getSocketTimeoutMillis());
  return reqConfigBuilder;
 });
 builder.setMaxRetryTimeoutMillis(esClientConfig.getMaxRetryTimeoutMillis());
 builder.setHttpClientConfigCallback(clientBuilder -> {
  clientBuilder.setDefaultIOReactorConfig(getIOReactorConfig(esClientConfig));
  clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider(esClientConfig));
  clientBuilder.setSSLContext(getSSLContext(esClientConfig));
  return clientBuilder;
 });
 RestClient lowLevelClient = builder.build();
 RestHighLevelClient client = new RestHighLevelClient(lowLevelClient);
 return new ElasticsearchClient(lowLevelClient, client);
}

代码示例来源:origin: datacleaner/DataCleaner

private ElasticSearchRestClient getClientForRestProtocol() {
  final String scheme = _ssl ? "https" : "http";
  final HttpHost hosts = new HttpHost(_hostname, _port, scheme);
  final RestClientBuilder restClientBuilder = RestClient.builder(hosts);
  if (!Strings.isNullOrEmpty(_username)) {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(_username, _password));
    restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
      @Override
      public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
      }
    });
  }
  final ElasticSearchRestClient elasticSearchRestClient = new ElasticSearchRestClient(restClientBuilder.build());
  return elasticSearchRestClient;
}

代码示例来源:origin: gengstrand/clojure-news-feed

@Bean
public RestHighLevelClient client() {
  return new RestHighLevelClient(RestClient.builder(new HttpHost(host, port)).setHttpClientConfigCallback(new OverrideConnectionPool()));
}

代码示例来源:origin: apache/metamodel

private ElasticSearchRestClient createClient(final DataContextProperties properties) throws MalformedURLException {
  final URL url = new URL(properties.getUrl());
  final RestClientBuilder builder = RestClient.builder(new HttpHost(url.getHost(), url.getPort()));
  
  if (properties.getUsername() != null) {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(properties.getUsername(),
        properties.getPassword()));
    builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(
        credentialsProvider));
  }
  return new ElasticSearchRestClient(builder.build());
}

代码示例来源:origin: couchbase/couchbase-elasticsearch-connector

public static RestHighLevelClient newElasticsearchClient(List<HttpHost> hosts, String username, String password, boolean secureConnection, Supplier<KeyStore> trustStore) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
 credentialsProvider.setCredentials(AuthScope.ANY,
   new UsernamePasswordCredentials(username, password));
 final SSLContext sslContext = !secureConnection ? null :
   SSLContexts.custom().loadTrustMaterial(trustStore.get(), null).build();
 final RestClientBuilder builder = RestClient.builder(Iterables.toArray(hosts, HttpHost.class))
   .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
     .setSSLContext(sslContext)
     .setDefaultCredentialsProvider(credentialsProvider))
   .setFailureListener(new RestClient.FailureListener() {
    @Override
    public void onFailure(HttpHost host) {
     Metrics.elasticsearchHostOffline().mark();
    }
   });
 return new RestHighLevelClient(builder);
}

代码示例来源:origin: com.strapdata.elasticsearch.test/framework

protected static RestClient createRestClient(final List<NodeInfo> nodes,
                       RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback, String protocol) {
  List<HttpHost> hosts = new ArrayList<>();
  for (NodeInfo node : nodes) {
    if (node.getHttp() != null) {
      TransportAddress publishAddress = node.getHttp().address().publishAddress();
      InetSocketAddress address = publishAddress.address();
      hosts.add(new HttpHost(NetworkAddress.format(address.getAddress()), address.getPort(), protocol));
    }
  }
  RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[hosts.size()]));
  if (httpClientConfigCallback != null) {
    builder.setHttpClientConfigCallback(httpClientConfigCallback);
  }
  return builder.build();
}

代码示例来源:origin: hakdogan/ElasticSearch

@Profile("test")
@Bean(destroyMethod = "close")
public RestHighLevelClient getRestClientForTest() {
  return new RestHighLevelClient(RestClient.builder(new HttpHost(getFixedHostPortGenericContainer().getContainerIpAddress(),
      props.getClients().getHttpPort())).setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(getCredentialsProvider())));
}

代码示例来源:origin: org.nuxeo.elasticsearch/nuxeo-elasticsearch-core

private void addClientCallback(ElasticSearchClientConfig config, RestClientBuilder builder) {
  BasicCredentialsProvider credentialProvider = getCredentialProvider(config);
  SSLContext sslContext = getSslContext(config);
  if (sslContext == null && credentialProvider == null) {
    return;
  }
  builder.setHttpClientConfigCallback(httpClientBuilder -> {
    httpClientBuilder.setSSLContext(sslContext);
    httpClientBuilder.setDefaultCredentialsProvider(credentialProvider);
    return httpClientBuilder;
  });
}

代码示例来源:origin: zeebe-io/zeebe

private RestHighLevelClient createClient() {
 final HttpHost httpHost = urlToHttpHost(configuration.url);
 // use single thread for rest client
 final RestClientBuilder builder =
   RestClient.builder(httpHost)
     .setHttpClientConfigCallback(
       httpClientBuilder ->
         httpClientBuilder.setDefaultIOReactorConfig(
           IOReactorConfig.custom().setIoThreadCount(1).build()));
 return new RestHighLevelClient(builder);
}

代码示例来源:origin: org.janusgraph/janusgraph-es

protected RestClient getRestClient(HttpHost[] hosts, Configuration config) {
  final RestClientBuilder restClientBuilder = getRestClientBuilder(hosts);
  final HttpClientConfigCallback httpClientConfigCallback = getHttpClientConfigCallback(config);
  if (httpClientConfigCallback != null) {
    restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
  }
  final RequestConfigCallback requestConfigCallback = getRequestConfigCallback(config);
  if (requestConfigCallback != null) {
    restClientBuilder.setRequestConfigCallback(requestConfigCallback);
  }
  if (config.has(ElasticSearchIndex.MAX_RETRY_TIMEOUT)) {
    restClientBuilder.setMaxRetryTimeoutMillis(config.get(ElasticSearchIndex.MAX_RETRY_TIMEOUT));
  }
  return restClientBuilder.build();
}

代码示例来源:origin: org.hibernate.search/hibernate-search-backend-elasticsearch

private RestClient createClient(ConfigurationPropertySource propertySource, int maxRetryTimeoutMillis) {
  ServerUris hosts = ServerUris.fromStrings( HOST.get( propertySource ) );
  return RestClient.builder( hosts.asHostsArray() )
      /*
       * Note: this timeout is currently only used on retries,
       * but should we start using the synchronous methods of RestClient,
       * it would be applied to synchronous requests too.
       * See https://github.com/elastic/elasticsearch/issues/21789#issuecomment-287399115
       */
      .setMaxRetryTimeoutMillis( maxRetryTimeoutMillis )
      .setRequestConfigCallback( b -> customizeRequestConfig( propertySource, b ) )
      .setHttpClientConfigCallback( b -> customizeHttpClientConfig( propertySource, hosts, b ) )
      .build();
}

代码示例来源:origin: hibernate/hibernate-search

private RestClient createClient(ConfigurationPropertySource propertySource, int maxRetryTimeoutMillis) {
  ServerUris hosts = ServerUris.fromStrings( HOST.get( propertySource ) );
  return RestClient.builder( hosts.asHostsArray() )
      /*
       * Note: this timeout is currently only used on retries,
       * but should we start using the synchronous methods of RestClient,
       * it would be applied to synchronous requests too.
       * See https://github.com/elastic/elasticsearch/issues/21789#issuecomment-287399115
       */
      .setMaxRetryTimeoutMillis( maxRetryTimeoutMillis )
      .setRequestConfigCallback( b -> customizeRequestConfig( propertySource, b ) )
      .setHttpClientConfigCallback( b -> customizeHttpClientConfig( propertySource, hosts, b ) )
      .build();
}

相关文章