Java Code Examples for org.elasticsearch.client.RestClientBuilder#setHttpClientConfigCallback()
The following examples show how to use
org.elasticsearch.client.RestClientBuilder#setHttpClientConfigCallback() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchConnection.java From components with Apache License 2.0 | 6 votes |
public static RestClient createClient(ElasticsearchDatastoreProperties datastore) throws MalformedURLException { String urlStr = datastore.nodes.getValue(); String[] urls = urlStr.split(","); HttpHost[] hosts = new HttpHost[urls.length]; int i = 0; for (String address : urls) { URL url = new URL("http://" + address); hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); i++; } RestClientBuilder restClientBuilder = RestClient.builder(hosts); if (datastore.auth.useAuth.getValue()) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(datastore.auth.userId.getValue(), datastore.auth.password.getValue())); restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); } return restClientBuilder.build(); }
Example 2
Source File: ElasticSearchFilter.java From timbuctoo with GNU General Public License v3.0 | 6 votes |
@JsonCreator public ElasticSearchFilter(@JsonProperty("hostname") String hostname, @JsonProperty("port") int port, @JsonProperty("username") Optional<String> username, @JsonProperty("password") Optional<String> password) { Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"), new BasicHeader("Role", "Read")}; final RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostname, port)) .setDefaultHeaders(headers); if (username.isPresent() && !username.get().isEmpty() && password.isPresent() && !password.get().isEmpty()) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(username.get(), password.get()) ); restClientBuilder.setHttpClientConfigCallback(b -> b.setDefaultCredentialsProvider(credentialsProvider)); } restClient = restClientBuilder.build(); mapper = new ObjectMapper(); }
Example 3
Source File: RestAutoConfigure.java From microservices-platform with Apache License 2.0 | 6 votes |
/** * 异步httpclient连接数配置 */ private void setHttpClientConfig(RestClientBuilder builder, RestClientPoolProperties poolProperties, RestClientProperties restProperties){ builder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setMaxConnTotal(poolProperties.getMaxConnectNum()) .setMaxConnPerRoute(poolProperties.getMaxConnectPerRoute()); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); map.from(restProperties::getUsername).to(username -> { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, restProperties.getPassword())); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }); return httpClientBuilder; }); }
Example 4
Source File: ElasticsearchConfig.java From staccato with Apache License 2.0 | 6 votes |
/** * Registers an instance of the high level client for Elasticsearch. * * @return An instance of Elasticsearch's high level rest client */ @Bean public RestHighLevelClient restHighLevelClient() { RestClientBuilder builder = RestClient.builder(new HttpHost(configProps.getHost(), configProps.getPort(), configProps.getScheme())); RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback = httpAsyncClientBuilder -> { httpAsyncClientBuilder .setMaxConnTotal(configProps.getRestClientMaxConnectionsTotal()) .setMaxConnPerRoute(configProps.getRestClientMaxConnectionsPerRoute()); if (null != configProps.getUser() && !configProps.getUser().isEmpty()) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(configProps.getUser(), configProps.getPassword())); httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } return httpAsyncClientBuilder; }; builder.setHttpClientConfigCallback(httpClientConfigCallback); builder.setMaxRetryTimeoutMillis(configProps.getRestClientMaxRetryTimeoutMillis()); //return new RestHighLevelClient(builder.build()); return new RestHighLevelClient(builder); }
Example 5
Source File: AbstractBeyonderTest.java From elasticsearch-beyonder with Apache License 2.0 | 6 votes |
private static void startRestClient() throws IOException { if (client == null) { RestClientBuilder builder = RestClient.builder(HttpHost.create(testCluster)); if (testClusterUser != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(testClusterUser, testClusterPass)); builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder .setDefaultCredentialsProvider(credentialsProvider)); } client = builder.build(); testClusterRunning(); } }
Example 6
Source File: ElasticsearchAuditLogSink.java From Groza with Apache License 2.0 | 6 votes |
@PostConstruct public void init() { try { log.trace("Adding elastic rest endpoint... host [{}], port [{}], scheme name [{}]", host, port, schemeName); RestClientBuilder builder = RestClient.builder( new HttpHost(host, port, schemeName)); if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(password)) { log.trace("...using username [{}] and password ***", userName); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); } this.restClient = builder.build(); } catch (Exception e) { log.error("Sink init failed!", e); throw new RuntimeException(e.getMessage(), e); } }
Example 7
Source File: ElasticsearchClientRest.java From c2mon with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void setup() { RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(properties.getHost(), properties.getPort(), properties.getScheme())); if (StringUtils.isNotEmpty(properties.getUsername()) && StringUtils.isNotEmpty(properties.getPassword())) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(properties.getUsername(), properties.getPassword()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, credentials); restClientBuilder.setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); } else { if (StringUtils.isNotEmpty(properties.getUsername()) || StringUtils.isNotEmpty(properties.getPassword())) { log.warn("Both username and password must be configured to setup ES authentication."); } } client = new RestHighLevelClient(restClientBuilder); }
Example 8
Source File: ElasticSearchClient.java From scava with Eclipse Public License 2.0 | 5 votes |
private boolean createClientDocker() { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin")); TrustStrategy trustStrategy = new TrustSelfSignedStrategy(); SSLContext sslContext; try { sslContext = SSLContexts.custom().loadTrustMaterial(trustStrategy).build(); HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; RestClientBuilder restClientBuilder = createRestClientBuilder(hostname, scheme); restClientBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(hostnameVerifier).build(); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); return httpClientBuilder; } }); return createHighLevelClient(restClientBuilder); } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { logger.error("Error while creating secure connection to ElasticSearch: ", e); } return false; }
Example 9
Source File: ElasticsearchCollector.java From karaf-decanter with Apache License 2.0 | 5 votes |
public void activate(Dictionary<String, Object> configuration) { this.configuration = configuration; String addressesString = (configuration.get("addresses") != null) ? configuration.get("addresses").toString() : "http://localhost:9200"; String username = (configuration.get("username") != null) ? configuration.get("username").toString() : null; String password = (configuration.get("password") != null) ? configuration.get("password").toString() : null; Set<String> addresses = new HashSet<>(Arrays.asList(addressesString.split(","))); HttpHost[] hosts = new HttpHost[addresses.size()]; int i = 0; for (String address : addresses) { try { URL url = new URL(address); hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); i++; } catch (Exception e) { LOGGER.warn("Bad elasticsearch address {}", address, e); } } RestClientBuilder restClientBuilder = RestClient.builder(hosts); restClientBuilder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(1000) .setSocketTimeout(10000)); if (username != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); restClientBuilder.setHttpClientConfigCallback( new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } } ); } restClient = new RestHighLevelClient(restClientBuilder); }
Example 10
Source File: ElasticsearchClientFactory.java From metron with Apache License 2.0 | 5 votes |
/** * 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); }
Example 11
Source File: ElasticSearchImpl.java From core-ng-project with Apache License 2.0 | 5 votes |
public void initialize() { RestClientBuilder builder = RestClient.builder(hosts); builder.setRequestConfigCallback(config -> config.setSocketTimeout((int) timeout.toMillis()) .setConnectionRequestTimeout((int) timeout.toMillis())); // timeout of requesting connection from connection pool builder.setHttpClientConfigCallback(config -> config.setMaxConnTotal(100) .setMaxConnPerRoute(100) .setKeepAliveStrategy((response, context) -> Duration.ofSeconds(30).toMillis())); client = new RestHighLevelClient(builder); }
Example 12
Source File: ElasticsearchAutoConfiguration.java From spring-boot-demo with MIT License | 5 votes |
/** * get restHistLevelClient * * @param builder RestClientBuilder * @param elasticsearchProperties elasticsearch default properties * @return {@link org.elasticsearch.client.RestHighLevelClient} * @author fxbin */ private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder builder, ElasticsearchProperties elasticsearchProperties) { // Callback used the default {@link RequestConfig} being set to the {@link CloseableHttpClient} builder.setRequestConfigCallback(requestConfigBuilder -> { requestConfigBuilder.setConnectTimeout(elasticsearchProperties.getConnectTimeout()); requestConfigBuilder.setSocketTimeout(elasticsearchProperties.getSocketTimeout()); requestConfigBuilder.setConnectionRequestTimeout(elasticsearchProperties.getConnectionRequestTimeout()); return requestConfigBuilder; }); // Callback used to customize the {@link CloseableHttpClient} instance used by a {@link RestClient} instance. builder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setMaxConnTotal(elasticsearchProperties.getMaxConnectTotal()); httpClientBuilder.setMaxConnPerRoute(elasticsearchProperties.getMaxConnectPerRoute()); return httpClientBuilder; }); // Callback used the basic credential auth ElasticsearchProperties.Account account = elasticsearchProperties.getAccount(); if (!StringUtils.isEmpty(account.getUsername()) && !StringUtils.isEmpty(account.getUsername())) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(account.getUsername(), account.getPassword())); } return new RestHighLevelClient(builder); }
Example 13
Source File: ElasticSearchHighSink.java From ns4_gear_watchdog with Apache License 2.0 | 5 votes |
@Override public synchronized void start() { logger.info("start elasticsearch sink......"); HttpHost[] httpHosts = new HttpHost[serverAddresses.length]; for (int i = 0; i < serverAddresses.length; i++) { String[] hostPort = serverAddresses[i].trim().split(":"); String host = hostPort[0].trim(); int port = hostPort.length == 2 ? Integer.parseInt(hostPort[1].trim()) : DEFAULT_PORT; logger.info("elasticsearch host:{},port:{}", host, port); httpHosts[i] = new HttpHost(host, port, "http"); } RestClientBuilder builder = RestClient.builder(httpHosts); if (StringUtils.isNotBlank(serverUser) || StringUtils.isNotBlank(serverPassword)) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(serverUser, serverPassword)); builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient( HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder .setDefaultCredentialsProvider(credentialsProvider); } }); } client = new RestHighLevelClient(builder); sinkCounter.start(); super.start(); }
Example 14
Source File: ElasticsearchRestWriter.java From incubator-gobblin with Apache License 2.0 | 4 votes |
private static RestClient buildRestClient(List<InetSocketTransportAddress> hosts, int threadCount, boolean sslEnabled, String keyStoreType, String keyStoreFilePassword, String identityFilepath, String trustStoreType, String trustStoreFilePassword, String cacertsFilepath) throws Exception { HttpHost[] httpHosts = new HttpHost[hosts.size()]; String scheme = sslEnabled?"https":"http"; for (int h = 0; h < httpHosts.length; h++) { InetSocketTransportAddress host = hosts.get(h); httpHosts[h] = new HttpHost(host.getAddress(), host.getPort(), scheme); } RestClientBuilder builder = RestClient.builder(httpHosts); if (sslEnabled) { log.info("ssl configuration: trustStoreType = {}, cacertsFilePath = {}", trustStoreType, cacertsFilepath); KeyStore truststore = KeyStore.getInstance(trustStoreType); FileInputStream trustInputStream = new FileInputStream(cacertsFilepath); try { truststore.load(trustInputStream, trustStoreFilePassword.toCharArray()); } finally { trustInputStream.close(); } SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null); log.info("ssl key configuration: keyStoreType = {}, keyFilePath = {}", keyStoreType, identityFilepath); KeyStore keystore = KeyStore.getInstance(keyStoreType); FileInputStream keyInputStream = new FileInputStream(identityFilepath); try { keystore.load(keyInputStream, keyStoreFilePassword.toCharArray()); } finally { keyInputStream.close(); } sslBuilder.loadKeyMaterial(keystore, keyStoreFilePassword.toCharArray()); final SSLContext sslContext = sslBuilder.build(); builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder // Set ssl context .setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()) // Configure number of threads for clients .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build())); } else { builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder // Configure number of threads for clients .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build())); } // Configure timeouts builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder .setConnectionRequestTimeout(0)); // Important, otherwise the client has spurious timeouts return builder.build(); }
Example 15
Source File: HttpClient.java From deprecated-security-advanced-modules with Apache License 2.0 | 4 votes |
private HttpClient(final KeyStore trustStore, final String basicCredentials, final KeyStore keystore, final char[] keyPassword, final String keystoreAlias, final boolean verifyHostnames, final boolean ssl, String[] supportedProtocols, String[] supportedCipherSuites, final String... servers) throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { super(); this.trustStore = trustStore; this.basicCredentials = basicCredentials; this.keystore = keystore; this.keyPassword = keyPassword; this.verifyHostnames = verifyHostnames; this.ssl = ssl; this.supportedProtocols = supportedProtocols; this.supportedCipherSuites = supportedCipherSuites; this.keystoreAlias = keystoreAlias; HttpHost[] hosts = Arrays.stream(servers) .map(s->s.split(":")) .map(s->new HttpHost(s[0], Integer.parseInt(s[1]),ssl?"https":"http")) .collect(Collectors.toList()).toArray(new HttpHost[0]); RestClientBuilder builder = RestClient.builder(hosts); //builder.setMaxRetryTimeoutMillis(10000); builder.setFailureListener(new RestClient.FailureListener() { @Override public void onFailure(Node node) { } }); builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { try { return asyncClientBuilder(httpClientBuilder); } catch (Exception e) { log.error("Unable to build http client",e); throw new RuntimeException(e); } } }); rclient = new RestHighLevelClient(builder); }
Example 16
Source File: ElasticsearchIO.java From beam with Apache License 2.0 | 4 votes |
@VisibleForTesting RestClient createClient() throws IOException { HttpHost[] hosts = new HttpHost[getAddresses().size()]; int i = 0; for (String address : getAddresses()) { URL url = new URL(address); hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); i++; } RestClientBuilder restClientBuilder = RestClient.builder(hosts); if (getUsername() != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(getUsername(), getPassword())); restClientBuilder.setHttpClientConfigCallback( httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); } if (getKeystorePath() != null && !getKeystorePath().isEmpty()) { try { KeyStore keyStore = KeyStore.getInstance("jks"); try (InputStream is = new FileInputStream(new File(getKeystorePath()))) { String keystorePassword = getKeystorePassword(); keyStore.load(is, (keystorePassword == null) ? null : keystorePassword.toCharArray()); } final TrustStrategy trustStrategy = isTrustSelfSignedCerts() ? new TrustSelfSignedStrategy() : null; final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(keyStore, trustStrategy).build(); final SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sslContext); restClientBuilder.setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setSSLContext(sslContext).setSSLStrategy(sessionStrategy)); } catch (Exception e) { throw new IOException("Can't load the client certificate from the keystore", e); } } restClientBuilder.setRequestConfigCallback( new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig( RequestConfig.Builder requestConfigBuilder) { if (getConnectTimeout() != null) { requestConfigBuilder.setConnectTimeout(getConnectTimeout()); } if (getSocketTimeout() != null) { requestConfigBuilder.setSocketTimeout(getSocketTimeout()); } return requestConfigBuilder; } }); return restClientBuilder.build(); }
Example 17
Source File: ESMetricExporter.java From sofa-lookout with Apache License 2.0 | 4 votes |
public ESMetricExporter(String name, Registry registry, ESProperties esProperties) { super(name, registry, 100, DataType.METRIC); int timeout = esProperties.getTimeout(); HttpHost httpHost = new HttpHost(esProperties.getHost(), esProperties.getPort(), "http"); RestClientBuilder restClientBuilder = RestClient.builder(httpHost) .setMaxRetryTimeoutMillis(timeout) .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(timeout)); String username = esProperties.getUsername(); String password = esProperties.getPassword(); // basic auth if (StringUtils.isNotEmpty(username)) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); restClientBuilder.setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); } client = restClientBuilder.build(); String index = esProperties.getIndex(); if (esProperties.getOperation().isAuto()) { logger.info("ElasticSearch operator is active"); ESOperatorBuilder esOperatorBuilder = new ESOperatorBuilder(ESDataType.METRIC) .httpHost(httpHost.toURI()) .index(esProperties.getIndex()) .mapping(esProperties.getType()); esOperatorBuilder.build().initializeDatabase(); //replace index with alias index = esOperatorBuilder.getAlias(); } String type = esProperties.getType(); if ("metrics".equalsIgnoreCase(index)) { actionMetaData = String.format("{ \"index\" : { \"_type\" : \"%s\" } }%n", type); } else { actionMetaData = String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\" } }%n", index, type); } endpoint = String.format("/%s/%s/_bulk", index, type); }
Example 18
Source File: ElasticEndpoint.java From sagacity-sqltoy with Apache License 2.0 | 4 votes |
/** * @param restClient the restClient to set */ public void initRestClient() { if (StringUtil.isBlank(this.getUrl())) return; if (restClient == null) { // 替换全角字符 String[] urls = this.getUrl().replaceAll("\\;", ";").replaceAll("\\,", ",").replaceAll("\\;", ",") .split("\\,"); // 当为单一地址时使用httpclient直接调用 if (urls.length < 2) return; List<HttpHost> hosts = new ArrayList<HttpHost>(); for (String urlStr : urls) { try { if (StringUtil.isNotBlank(urlStr)) { URL url = new java.net.URL(urlStr.trim()); hosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol())); } } catch (MalformedURLException e) { e.printStackTrace(); } } if (!hosts.isEmpty()) { HttpHost[] hostAry = new HttpHost[hosts.size()]; hosts.toArray(hostAry); RestClientBuilder builder = RestClient.builder(hostAry); final ConnectionConfig connectionConfig = ConnectionConfig.custom() .setCharset(Charset.forName(this.charset == null ? "UTF-8" : this.charset)).build(); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(this.requestTimeout) .setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).build(); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); final boolean hasCrede = (StringUtil.isNotBlank(this.getUsername()) && StringUtil.isNotBlank(getPassword())) ? true : false; // 凭据提供器 if (hasCrede) { credsProvider.setCredentials(AuthScope.ANY, // 认证用户名和密码 new UsernamePasswordCredentials(getUsername(), getPassword())); } builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.setDefaultConnectionConfig(connectionConfig) .setDefaultRequestConfig(requestConfig); if (hasCrede) { httpClientBuilder.setDefaultCredentialsProvider(credsProvider); } return httpClientBuilder; } }); restClient = builder.build(); } } }
Example 19
Source File: ElasticsearchAppender.java From karaf-decanter with Apache License 2.0 | 4 votes |
public void open(Dictionary<String, Object> config) { this.config = config; String addressesString = getValue(config, ADDRESSES_PROPERTY, ADDRESSES_DEFAULT); String username = getValue(config, USERNAME_PROPERTY, USERNAME_DEFAULT); String password = getValue(config, PASSWORD_PROPERTY, PASSWORD_DEFAULT); Set<String> addresses = new HashSet<String>(Arrays.asList(addressesString.split(","))); HttpHost[] hosts = new HttpHost[addresses.size()]; int i = 0; for (String address : addresses) { try { URL url = new URL(address); hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); i++; } catch (Exception e) { LOGGER.warn("Bad elasticsearch address {}", address, e); } } RestClientBuilder restClientBuilder = RestClient.builder(hosts); restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectTimeout(1000) .setSocketTimeout(10000); } }); if (username != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); restClientBuilder.setHttpClientConfigCallback( new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } } ); } client = restClientBuilder.build(); TimeZone tz = TimeZone.getTimeZone( "UTC" ); tsFormat.setTimeZone(tz); indexDateFormat.setTimeZone(tz); }
Example 20
Source File: ElasticsearchClient.java From presto with Apache License 2.0 | 4 votes |
private static RestHighLevelClient createClient( ElasticsearchConfig config, Optional<AwsSecurityConfig> awsSecurityConfig, Optional<PasswordConfig> passwordConfig) { RestClientBuilder builder = RestClient.builder( new HttpHost(config.getHost(), config.getPort(), config.isTlsEnabled() ? "https" : "http")) .setMaxRetryTimeoutMillis(toIntExact(config.getMaxRetryTime().toMillis())); builder.setHttpClientConfigCallback(ignored -> { RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(toIntExact(config.getConnectTimeout().toMillis())) .setSocketTimeout(toIntExact(config.getRequestTimeout().toMillis())) .build(); IOReactorConfig reactorConfig = IOReactorConfig.custom() .setIoThreadCount(config.getHttpThreadCount()) .build(); // the client builder passed to the call-back is configured to use system properties, which makes it // impossible to configure concurrency settings, so we need to build a new one from scratch HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create() .setDefaultRequestConfig(requestConfig) .setDefaultIOReactorConfig(reactorConfig) .setMaxConnPerRoute(config.getMaxHttpConnections()) .setMaxConnTotal(config.getMaxHttpConnections()); if (config.isTlsEnabled()) { buildSslContext(config.getKeystorePath(), config.getKeystorePassword(), config.getTrustStorePath(), config.getTruststorePassword()) .ifPresent(clientBuilder::setSSLContext); if (config.isVerifyHostnames()) { clientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); } } passwordConfig.ifPresent(securityConfig -> { CredentialsProvider credentials = new BasicCredentialsProvider(); credentials.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(securityConfig.getUser(), securityConfig.getPassword())); clientBuilder.setDefaultCredentialsProvider(credentials); }); awsSecurityConfig.ifPresent(securityConfig -> clientBuilder.addInterceptorLast(new AwsRequestSigner( securityConfig.getRegion(), getAwsCredentialsProvider(securityConfig)))); return clientBuilder; }); return new RestHighLevelClient(builder); }