A study of Ignite CS mode java

Keywords: Apache Java github Maven

In the form of command line, expanding cluster is to increase Server continuously , in many practical business scenarios, we need to implement CS pattern with Ignite, and this article will provide detailed code.

Major maven dependencies:

        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-core</artifactId>
            <version>${ignite.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-spring</artifactId>
            <version>${ignite.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ignite</groupId>
            <artifactId>ignite-indexing</artifactId>
            <version>${ignite.version}</version>
        </dependency>

        <properties>
               <ignite.version>2.3.0</ignite.version>
        </properties>

code implementation

Server side implementation:

import com.jc.searchengine.po.Person;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteCompute;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cluster.ClusterGroup;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;

import java.util.Set;

/**
 * @Author: wangjie
 * @Description:
 * @Date: Created in 10:20 2018/3/23
 */
public class Application {

    public static void main(String[] args) {

        Ignite ignite = Ignition.start();

        CacheConfiguration cacheCfg = new CacheConfiguration();
        cacheCfg.setName("serverCache");
        cacheCfg.setCacheMode(CacheMode.PARTITIONED);

        IgniteCache<Long,Person> cache = ignite.getOrCreateCache(cacheCfg);

        ClusterGroup clusterGroup = ignite.cluster().forServers();

        cache.put(1L,new Person(1,"Sofiya",2));
        cache.put(2L,new Person(1,"Sofiya",2));
        cache.put(3L,new Person(1,"Sofiya",666666666));
        }
    }

In essence, the code on the Server side mainly performs two operations:

  • Create a cache called "serverCache"
  • Insert data in cache

Client side implementation:

import com.jc.searchengine.po.Person;
import org.apache.ignite.*;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.query.ContinuousQuery;
import org.apache.ignite.cache.query.ScanQuery;
import org.apache.ignite.cluster.ClusterGroup;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.util.lang.IgnitePredicate2X;
import org.apache.ignite.lang.IgniteBiPredicate;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;

import javax.cache.Cache;
import java.time.Period;
import java.util.Arrays;

/**
 * @Author: wangjie
 * @Description:
 * @Date: Created in 10:55 2018/3/23
 */

public class Application{
    public static void main(String[] args) {

        //This node is configured as a client
        Ignition.setClientMode(true);
        IgniteConfiguration cfg = new IgniteConfiguration();

        TcpCommunicationSpi commSpi = new TcpCommunicationSpi();
        commSpi.setSlowClientQueueLimit(1000);

        TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder ipfinder = new TcpDiscoveryVmIpFinder();
        ipfinder.setAddresses(Arrays.asList("localhost"));
        discoverySpi.setIpFinder(ipfinder);

        cfg.setCommunicationSpi(commSpi);
        cfg.setDiscoverySpi(discoverySpi);

        Ignite ignite = Ignition.start(cfg);

        ClusterGroup clusterGroup = ignite.cluster().forClients();

      IgniteCache<Long,Person> cache = ignite.getOrCreateCache("serverCache");
        IgniteCompute compute = ignite.compute(clusterGroup);
            compute.broadcast(() -> {
                Person s1 = cache.get(1L);
                Person s2 = cache.get(2L);
                Person s3 = cache.get(3L);
                System.out.println(s1.toString() + " " + s2.toString() + " " + s3.toString());
            });
    }
}

The Client side code does the following:

  • Configure this node as client mode
  • Get the "serverCache" buffer just created by the Server segment
  • Show the data on the Server side

Operation procedure

Start the programs on the Server side and the Client side respectively, and view the console:

Server terminal:

Server.png

Client terminal:

Client.png

Project code github address Server
Project code github address Client

Cheng Xuyuan, a small white, if there is a mistake, please criticize and correct! (1.

Posted by Bit343 on Wed, 01 Apr 2020 04:33:11 -0700