private static void addHostToClient(HostAndPort host, TransportClient client) { try { client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host.getHostText()), host.getPortOrDefault(9001))); } catch (UnknownHostException e) { throw new IllegalStateException("Can not resolve host [" + host + "]", e); } }
@Override public TransportClient createClient(Map<String, String> clientConfig) { Settings settings = Settings.builder().put(clientConfig) .put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME) .put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME) .build(); TransportClient transportClient = new PreBuiltTransportClient(settings); for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) { transportClient.addTransportAddress(transport); } // verify that we actually are connected to a cluster if (transportClient.connectedNodes().isEmpty()) { // close the transportClient here IOUtils.closeQuietly(transportClient); throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!"); } if (LOG.isInfoEnabled()) { LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes()); } return transportClient; }
@Override public Client get() { Settings settings = Settings.builder() .put("client.transport.ignore_cluster_name", true) .put("client.transport.sniff", true) .build(); TransportClient tc = new PreBuiltTransportClient(settings); List<URI> clusterAddresses = configuration.getURIs(); if (clusterAddresses.isEmpty()) { logger.warn(ElasticSearchConfiguration.ELASTIC_SEARCH_URL_PROPERTY_NAME + " is not set. Indexing will remain DISABLED."); } for (URI hostAddress : clusterAddresses) { int port = Optional.ofNullable(hostAddress.getPort()).orElse(9200); try { tc.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAddress.getHost()), port)); } catch (UnknownHostException uhe){ throw new ProvisionException("Invalid host" + hostAddress.getHost(), uhe); } } return tc; } }
@Override public TransportClient createClient(Map<String, String> clientConfig) { Settings settings = Settings.settingsBuilder().put(clientConfig).build(); TransportClient transportClient = TransportClient.builder().settings(settings).build(); for (TransportAddress address : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) { transportClient.addTransportAddress(address); } // verify that we actually are connected to a cluster if (transportClient.connectedNodes().isEmpty()) { // close the transportClient here IOUtils.closeQuietly(transportClient); throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!"); } if (LOG.isInfoEnabled()) { LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes()); } return transportClient; }
/** * create a elasticsearch transport client (remote elasticsearch) * @param addresses an array of host:port addresses * @param clusterName */ public ElasticsearchClient(final String[] addresses, final String clusterName) { // create default settings and add cluster name Settings.Builder settings = Settings.builder() .put("cluster.name", clusterName) .put("cluster.routing.allocation.enable", "all") .put("cluster.routing.allocation.allow_rebalance", "true"); // create a client TransportClient tc = TransportClient.builder() .settings(settings.build()) .build(); for (String address: addresses) { String a = address.trim(); int p = a.indexOf(':'); if (p >= 0) try { InetAddress i = InetAddress.getByName(a.substring(0, p)); int port = Integer.parseInt(a.substring(p + 1)); tc.addTransportAddress(new InetSocketTransportAddress(i, port)); } catch (UnknownHostException e) { DAO.severe(e); } } this.elasticsearchClient = tc; }
/** * Open client to elaticsearch cluster * * @param clusterName */ private void openClient(String clusterName) { logger.info("Using ElasticSearch hostnames: {} ", Arrays.toString(serverAddresses)); Settings settings = ImmutableSettings.settingsBuilder() .put("cluster.name", clusterName).build(); TransportClient transportClient = new TransportClient(settings); for (InetSocketTransportAddress host : serverAddresses) { transportClient.addTransportAddress(host); } if (client != null) { client.close(); } client = transportClient; }
for (String host : hostArray) { int i = host.indexOf(":"); transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host.substring(0, i)), Integer.parseInt(host.substring(i + 1))));
@Override public Connection connect(Configuration config) throws IOException { log.debug("Configuring TransportClient"); ImmutableSettings.Builder settingsBuilder = settingsBuilder(config); if (config.has(ElasticSearchIndex.CLIENT_SNIFF)) { String k = "client.transport.sniff"; settingsBuilder.put(k, config.get(ElasticSearchIndex.CLIENT_SNIFF)); log.debug("Set {}: {}", k, config.get(ElasticSearchIndex.CLIENT_SNIFF)); } TransportClient tc = new TransportClient(settingsBuilder.build()); int defaultPort = config.has(INDEX_PORT) ? config.get(INDEX_PORT) : ElasticSearchIndex.HOST_PORT_DEFAULT; for (String host : config.get(INDEX_HOSTS)) { String[] hostparts = host.split(":"); String hostname = hostparts[0]; int hostport = defaultPort; if (hostparts.length == 2) hostport = Integer.parseInt(hostparts[1]); log.info("Configured remote host: {} : {}", hostname, hostport); tc.addTransportAddress(new InetSocketTransportAddress(hostname, hostport)); } return new Connection(null, tc); } },
for (final InetSocketAddress host : esHosts) { try { transportClient.addTransportAddress(new InetSocketTransportAddress(host)); } catch (IllegalArgumentException iae) { log.error("Could not add transport address {}", new Object[]{host});
String[] nodes = h.split(":"); try { tClient.addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName(nodes[0]), Integer.parseInt(nodes[1])
throw new IllegalArgumentException("unable to parse port [" + nodes[1] + "]", e); client.addTransportAddress(new InetSocketTransportAddress(address, port));
/** * Create the transport client * @return */ private Client createTransportClient() { final String clusterName = indexFig.getClusterName(); final int port = indexFig.getPort(); ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put( "cluster.name", clusterName ) .put( "client.transport.sniff", true ); String nodeName = indexFig.getNodeName(); if ( "default".equals( nodeName ) ) { // no nodeName was specified, use hostname try { nodeName = InetAddress.getLocalHost().getHostName(); } catch ( UnknownHostException ex ) { nodeName = "client-" + RandomStringUtils.randomAlphabetic( 8 ); logger.warn( "Couldn't get hostname to use as ES node name, using {}", nodeName ); } } settings.put( "node.name", nodeName ); TransportClient transportClient = new TransportClient( settings.build() ); // we will connect to ES on all configured hosts for ( String host : indexFig.getHosts().split( "," ) ) { transportClient.addTransportAddress( new InetSocketTransportAddress( host, port ) ); } return transportClient; }
transportClient.addTransportAddress(transport);
protected TransportClient getUserTransportClient(ClusterInfo info, String keyStore, Settings initTransportClientSettings) { final String prefix = getResourceFolder()==null?"":getResourceFolder()+"/"; Settings tcSettings = Settings.builder() .put("cluster.name", info.clustername) .put("searchguard.ssl.transport.truststore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+"truststore.jks")) .put("searchguard.ssl.transport.enforce_hostname_verification", false) .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+keyStore)) .put(initTransportClientSettings) .build(); TransportClient tc = new TransportClientImpl(tcSettings, asCollection(Netty4Plugin.class, SearchGuardPlugin.class)); tc.addTransportAddress(new TransportAddress(new InetSocketAddress(info.nodeHost, info.nodePort))); return tc; }
protected TransportClient getInternalTransportClient(ClusterInfo info, Settings initTransportClientSettings) { final String prefix = getResourceFolder()==null?"":getResourceFolder()+"/"; Settings tcSettings = Settings.builder() .put("cluster.name", info.clustername) .put("searchguard.ssl.transport.truststore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+"truststore.jks")) .put("searchguard.ssl.transport.enforce_hostname_verification", false) .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath(prefix+"kirk-keystore.jks")) .put(initTransportClientSettings) .build(); TransportClient tc = new TransportClientImpl(tcSettings, asCollection(Netty4Plugin.class, SearchGuardPlugin.class)); tc.addTransportAddress(new TransportAddress(new InetSocketAddress(info.nodeHost, info.nodePort))); return tc; }
if (hostparts.length == 2) hostport = Integer.parseInt(hostparts[1]); log.info("Configured remote host: {} : {}", hostname, hostport); tc.addTransportAddress(new InetSocketTransportAddress(hostname, hostport));
for (final InetSocketAddress host : esHosts) { try { transportClient.addTransportAddress(new InetSocketTransportAddress(host)); } catch (IllegalArgumentException iae) { log.error("Could not add transport address {}", new Object[]{host});
@Bean public Client client() { TransportClient client = new TransportClient(); TransportAddress address = new InetSocketTransportAddress(hostname, port); client.addTransportAddress(address); return client; } }
/** * create a elasticsearch transport client (remote elasticsearch) * @param addresses an array of host:port addresses * @param clusterName */ public ElasticsearchClient(final String[] addresses, final String clusterName) { // create default settings and add cluster name Settings.Builder settings = Settings.builder() .put("cluster.routing.allocation.enable", "all") .put("cluster.routing.allocation.allow_rebalance", "always"); if (clusterName != null) settings.put("cluster.name", clusterName); // create a client TransportClient tc = new PreBuiltTransportClient(settings.build()); for (String address: addresses) { String a = address.trim(); int p = a.indexOf(':'); if (p >= 0) try { InetAddress i = InetAddress.getByName(a.substring(0, p)); int port = Integer.parseInt(a.substring(p + 1)); tc.addTransportAddress(new InetSocketTransportAddress(i, port)); } catch (UnknownHostException e) { Data.logger.warn("", e); } } this.elasticsearchClient = tc; }
tc.addTransportAddress(new TransportAddress(new InetSocketAddress(info.nodeHost, info.nodePort))); Assert.assertEquals(info.numNodes, tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());