how to connect to different Hosts in elasticsearch using Node Client - java

i am using ElasticSearch node client to perform queries but i want to connect to different IP address then localhost, I want to achieve this
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
thing in node client
Node node = nodeBuilder().clusterName("yourcluster").client(true).node();
Client client = node.client();
please help me, I want to use Node Client with other hosts not TransportClient

Node client automatically discovers all the nodes of the cluster because it's actually a part of the cluster itself, you don't need to list the hosts manually. But if your cluster resides in another network which can't be discovered automatically you may also specify at least one cluster host with settings.
Settings.Builder settings = Settings.builder()
.put("path.home", ".")
.put("discovery.zen.ping.unicast.hosts","192.168.1.1");
Node node = NodeBuilder.nodeBuilder().settings(settings)
.clusterName("elasticsearch")
.client(true)
.node();
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html

Related

How to connect Apache Ignite Thin Client to Apache Ignite Cluster?

I have a running Ignite cluster and I use AWS S3 for node discovery:
TcpDiscoveryS3IpFinder ipFinder = new TcpDiscoveryS3IpFinder();
BasicAWSCredentials awsCredentials =
new BasicAWSCredentials(igniteAccessKey, igniteSecretAccessKey);
ipFinder.setAwsCredentials(awsCredentials);
ipFinder.setBucketName(igniteBucketName);
ipFinder.setBucketEndpoint("s3.eu-central-1.amazonaws.com");
TcpDiscoverySpi spi = new TcpDiscoverySpi();
spi.setIpFinder(ipFinder);
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setClientMode(true);
cfg.setDiscoverySpi(spi);
Ignition.start(cfg);
It works very well and I can connect to this cluster using Apache Ignite Client nodes.
But what about Apache Ignite Thin Client? Thin client uses ClientConfiguration class (instead of IgniteConfiguration) that requires a list of IP addresses of cluster nodes. AFAIK one can only hardcode that list of IP addresses. From official documentation:
ClientConfiguration cfg = new ClientConfiguration().setAddresses("127.0.0.1:10800");
try (IgniteClient client = Ignition.startClient(cfg)) {
ClientCache<Integer, String> cache = client.cache("myCache");
// Get data from the cache
}
So I have questions:
How should I handle situations when a list of IP addresses change?
Is there any way to use node discovery for Thin clients?
You can use domain names as well as IP addresses. But you can't use node discovery with thin clients.

How to set Eureka URL from code at client application startup

We need to set Eureka server URL at client application from startup code, but it seems there is no way how to do it.
We have a mechanism how to discover Eureka server on network by UDP multicast broadcasting. Server sends response back to the client with information about IP address and port where Eureka server is running. But we don't know how to set this URL in Eureka client application from code. It seems the only way how to set Eureka server URL is the property eureka.client.serviceUrl.defaultZone in application.property file.
// Server - start a new thread with UDP packet detection and reply mechanism
LocationService.listenAndReplyToEurekaClients(thisServerPort);
// Server - application start
SpringApplication.run(EurekaServerApplication.class, args);
// Client - send UDP packet and receive reply with Eureka server IP and port
Response response = LocationService.findEurekaServerAddress(5, 3, TimeUnit.SECONDS);
var hostProtocol = "http";
var eurekaUrl = new URL(
hostProtocol,
response.getEurekaAddress(),
response.getEurekaPort(),"").toString();
We would like to set this eurekaURL to the client before it starts registering to Eureka server.
In this case, we can do following things-
Extend EurekaClientConfigBean and override getEurekaServerServiceUrls method. Method returns a List of String which is nothing but list of all the URLS, of eureka instances. You need to set the URL here from your response which has IP and port.
Later create the discovery client using- DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config). (Its going to be a Bean for sure).
That should work.
You can create ApplicationInfoManager as-
ApplicationInfoManager applicationInfoManager =
initializeApplicationInfoManager(webAppInstanceConfig);
Where WebAppInstanceConfig is-
class WebAppInstanceConfig extends MyDataCenterInstanceConfig {// Override all the needed properties from MyDataCenterInstanceConfig}

Does Jest Client support Cluster name?

I have implemented elasticsearch with native client. This is my implementation:
Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch").build();
TransportClient client = TransportClient.builder().settings(settings).build();
Now I want to implement the same with Jest client. I have created the client, but I am missing cluster name:
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(
new HttpClientConfig
.Builder("http://127.0.0.1:9301")
.multiThreaded(true)
.build()
);
JestClient client = factory.getObject();
Is there any way to implement with cluster name?
Jest uses the HTTP protocol since it's hitting the REST API, hence you don't need to specify the cluster name like you do with the native TransportClient does.
Also make sure to use the port 9201 and not 9301

Where Paho gets connected to when used with list of brokers

How to find out where Paho Java client got actually connected to when it was called with list of severURIs.
I work on a client application where multiple equal brokers are used to achieve “High Availability” system. Paho is provided with list of all brokers to connect to:
MqttConnectOptions options = new MqttConnectOptions();
options.setServerURIs( new String[]{"tcp://broker1.com:1883",
"tcp://broker2.com:1883", "tcp://broker3.com:1883"} );
MqttClient mqttClient = new MqttClient("tcp://broker1.com:1883",
clientId, new MemoryPersistence());
mqttClient.connect(options);
After connect I would like to report where it has got actually connected to, but I do not know how.
MqttClient.getServerURI() returns URI provided in MqttClient constructor regardless it is connected to.
According to documentation:
When an attempt to connect is initiated the client will start with the
first serverURI in the list and work through the list until a
connection is established with a server. If a connection cannot be
made to any of the servers then the connect attempt fails.

Netty is giving me a wrong port using TCP

I'm using Netty with Java trying to configure a TCP client. Everything is working so far, except that I'm connecting on port 1050 but when I call messageEvent.getRemoteAddress() on messageReceived() method of the handler, I'm getting the port 1500. I changed the port to 1049 but I'm still receiving 1500. This is Netty's problem or can it be the server's problem?
My hardware setup here is: this netty client running on a Java server, and several access control equipments spread through the area here. The equipments act as tcp servers and the netty as the client, that process everything the server sends and just reply to them.
The tcp server initialization is this:
private ChannelFactory fabrica;
private ServerBootstrap bootstrap;
public void iniciarServidorTCP() {
fabrica = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
bootstrap = new ServerBootstrap(fabrica);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
#Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoderDeMensagem", new MensagemDecoderTCP());
pipeline.addLast("handlerGerente", new GerenteTCP());
pipeline.addLast("encoder de mensagem", new MensagemEncoderTCP());
return pipeline;
}
});
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.reuseAddress", true);
bootstrap.bind(new InetSocketAddress(1050));
}
Any idea why I'm getting 1500 instead of 1050? Could it be a problem with the equipment?
Every TCP connection has a source port and a destination port. When you connect to a server, the server sees the destination port as its well-known address. The client picks the source port. On either end, getting the "remote address" gets the other side's address. So when you call get remote address on the server, you get the client's address, not the server's.
Imagine you have a server with one IP address and one well-known port. Now, say you have a client machine with one IP address. If it make's four connections to the server, how can either end tell those connections apart? The answer is that the client port is different.

Categories

Resources