TCP/IP discovery

The default cluster discovery type is TCP/IP, and it is recommend for a 100-200-node deployment. Apache Ignite's DiscoverySpi is used to discover the nodes, as shown in the following screenshot. TcpDiscoverySpi is the default implementation:

TcpDiscoverySpi needs to know about IPs to discover the nodes. The TcpDiscoveryIpFinder implementation is used to configure the TCP/IP finder. The following is the default implementation of TcpDiscoveryIpFinder:

Apart from JDBC, SharedFile, VM, and multicast IP finders, there are a couple of custom IP finders from the vendors. Some of the popular implementations are as follows:

  • ZooKeeper IP finder
  • Kubernetes IP finder
  • Google Cloud Storage IP finder
  • Amazon S3 IP finder
  • Amazon ELB IP finder
  • Apache jCloud IP finder

We will be interested in exploring the multicast and VM IP finders, as they cover most of the use cases. The multicast IP finder is the default IP finder; we don't need to change any configuration to discover nodes using IP multicast, but multicasting always has an overhead.

The following code snippet creates a TcpDiscoverySpi, which will instantiate a TcpDiscoveryMulticastIPFinder and sets the multicastGroup with a multicast IP address. You can find the multicast address by running the following command in a DOS prompt, that is, netsh interface ip show joins:

package com.packt;

import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;

public class Multicast {
public static void main(String[] args) throws InterruptedException {
TcpDiscoverySpi spi = new TcpDiscoverySpi();

TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();

ipFinder.setMulticastGroup("239.255.255.250");

spi.setIpFinder(ipFinder);

IgniteConfiguration cfg = new IgniteConfiguration();

// Override default discovery SPI.
cfg.setDiscoverySpi(spi);
cfg.setIgniteInstanceName("The Painter");
// Start Ignite node.
try(Ignite ignite =Ignition.start(cfg)){
System.out.println("Node name "+ignite.name());
Thread.sleep(9999999);
}
}
}

It will print the following log in the console:

Now, run another instance of the same code but change the multicast group IP. Comment out the setMulticastGroup to use the default multicast IP address:

//ipFinder.setMulticastGroup("239.255.255.250");

It will create a separate cluster and not join the old cluster:

Now, uncomment the setMulticastGroup line and rerun the program. It will join the old cluster. You can find vers=2 and servers=2 in the console log:

To work with a set of static IP addresses, you need to create a new class with the following code snippet. The TcpDiscoveryVmIpFinder serves the static IP discovery task. This class has a method, setAddresses, to pass a list of static IP addresses:

package com.packt;

import java.util.Arrays;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;

public class StaticIp {
public static void main(String[] args) throws InterruptedException {
TcpDiscoverySpi spi = new TcpDiscoverySpi();

TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();

// Set initial IP addresses.
// Note that you can optionally specify a port or a port range.
ipFinder.setAddresses(Arrays.asList( "127.0.0.1:47500..47509"));

spi.setIpFinder(ipFinder);

IgniteConfiguration cfg = new IgniteConfiguration();

// Override default discovery SPI.
cfg.setDiscoverySpi(spi);

cfg.setIgniteInstanceName("The Painter");
// Start Ignite node.
try(Ignite ignite =Ignition.start(cfg)){
System.out.println("Node name "+ignite.name());
Thread.sleep(9999999);
}
}
}

When you run this program, the console log will look as follows:

Rerun the same program; the old program is still running as it has a thread.sleep. It will join the old cluster:

We looked at how static and multicast IP finders work. Next, we will look at the most efficient ZooKeeper way of clustering.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.55