首页 > 代码库 > Ignite集群管理——基于静态IP的节点发现

Ignite集群管理——基于静态IP的节点发现

Ignite作为分布式内存,集群管理必不可少,Ignite支持基于组播,静态IP,Zookeeper,JDBC等方式发现节点,本文主要介绍基于静态IP的节点发现。

两个最重要的TCP通信设置类:

1、 TcpDiscoverySpi

用于设置集群维持与节点发现的tcp通信ip,port。

2、 TcpCommunicationSpi

用于设置业务数据(缓存数据)tcp通信的ip,port。

3、 两者的区别与联系

TcpDiscoverySpi用于维持管理集群,交换的是用户不感知的ignite内部数据;

TcpCommunicationSpi用于业务数据交换;

TcpCommunicationSpi设置的业务数据交换ip,port通过TcpDiscoverySpi在集群间传递。

对于集群管理,我们主要关心TcpDiscoverySpi的设置,TcpDiscoverySpi可以设置节点发现方法(setDiscoverySpi),TcpDiscoveryVmIpFinder用于设置静态IP,TcpDiscoveryVmIpFinder设置了一个IP,即可以感知到此IP能感知到的所有节点信息。

我用了2台笔记本电脑运行了3个Ignite节点,并配置了静态IP发现规则,每个节点都成功的获取到了不同节点缓存的数据。

3个Ignite节点类结构一致,代码如下:

 

package com.coshaho.learn.ignite.cluster;

import java.util.Arrays;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
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;

/**
 * 
 * IgniteCluster1.java Create on 2017年5月30日 下午8:42:18    
 *    
 * 类功能说明:   运行在IP为192.168.1.103的笔记本上的Ignite节点
 *
 * Copyright: Copyright(c) 2013 
 * Company: COSHAHO
 * @Version 1.0
 * @Author coshaho
 */
public class IgniteCluster1 
{
    public static void main(String[] args)
    {
        IgniteConfiguration cfg=new IgniteConfiguration();
        // Explicitly configure TCP discovery SPI to provide list of initial nodes
        // from the first cluster.
        
        TcpDiscoverySpi discoverySpi=new TcpDiscoverySpi();
        // Initial local port to listen to.
        discoverySpi.setLocalPort(49100);
        // Changing local port range. This is an optional action.
        discoverySpi.setLocalPortRange(20);

        TcpDiscoveryVmIpFinder ipFinder=new TcpDiscoveryVmIpFinder();
        ipFinder.setAddresses(Arrays.asList("192.168.1.105:49300..49320"));
        // Overriding IP finder.
        discoverySpi.setIpFinder(ipFinder);
        // Overriding discovery SPI.
        cfg.setDiscoverySpi(discoverySpi);

        // Explicitly configure TCP communication SPI by changing local port number for
        // the nodes from the first cluster.
        TcpCommunicationSpi commSpi=new TcpCommunicationSpi();
        commSpi.setLocalPort(48100);
        // Overriding communication SPI.
        cfg.setCommunicationSpi(commSpi);

        // Starting a node.
        Ignite ignite = Ignition.start(cfg);
        System.out.println("IgniteCluster1 start OK.");
        CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<Integer, String>();
        cacheCfg.setName("myCache");
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cacheCfg);
        System.out.println(cache.get(1));
        System.out.println(cache.get(2));
        System.out.println(cache.get(3));
    }
}

 

package com.coshaho.learn.ignite.cluster;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;

/**
 * 
 * IgniteCluster2.java Create on 2017年5月30日 下午8:42:07    
 *    
 * 类功能说明:   运行在IP为192.168.1.105的笔记本上的Ignite节点
 *
 * Copyright: Copyright(c) 2013 
 * Company: COSHAHO
 * @Version 1.0
 * @Author coshaho
 */
public class IgniteCluster2 
{
    public static void main(String[] args)
    {
        IgniteConfiguration cfg=new IgniteConfiguration();
        // Explicitly configure TCP discovery SPI to provide list of initial nodes
        // from the second cluster.
        
        TcpDiscoverySpi discoverySpi=new TcpDiscoverySpi();
        // Initial local port to listen to.
        discoverySpi.setLocalPort(49200);
        // Changing local port range. This is an optional action.
        discoverySpi.setLocalPortRange(20);
        // Overriding discovery SPI.
        cfg.setDiscoverySpi(discoverySpi);

        // Explicitly configure TCP communication SPI by changing local port number for
        // the nodes from the second cluster.
        TcpCommunicationSpi commSpi=new TcpCommunicationSpi();
        commSpi.setLocalPort(48200);
        cfg.setCommunicationSpi(commSpi);

        // Starting a node.
        Ignite ignite =Ignition.start(cfg);
        System.out.println("IgniteCluster2 start OK.");
        CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<Integer, String>();
        cacheCfg.setBackups(1);
        cacheCfg.setCacheMode(CacheMode.PARTITIONED);
        cacheCfg.setName("myCache");
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cacheCfg);
        cache.put(2, "ignite2");
        System.out.println(cache.get(1));
        System.out.println(cache.get(2));
        System.out.println(cache.get(3));
        
    }
}
package com.coshaho.learn.ignite.cluster;

import java.util.Arrays;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
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;

/**
 * 
 * IgniteCluster3.java Create on 2017年5月30日 下午8:41:22    
 *    
 * 类功能说明:   运行在IP为192.168.1.105的笔记本上的Ignite节点
 *
 * Copyright: Copyright(c) 2013 
 * Company: COSHAHO
 * @Version 1.0
 * @Author coshaho
 */
public class IgniteCluster3 {
    public static void main(String[] args)
    {
        // 192.168.1.105  192.168.1.103
        IgniteConfiguration cfg=new IgniteConfiguration();

        // Explicitly configure TCP discovery SPI to provide list of initial nodes
        // from the first cluster.
        TcpDiscoverySpi discoverySpi=new TcpDiscoverySpi();
        // Initial local port to listen to.
        discoverySpi.setLocalPort(49300);
        // Changing local port range. This is an optional action.
        discoverySpi.setLocalPortRange(20);
        
        TcpDiscoveryVmIpFinder ipFinder=new TcpDiscoveryVmIpFinder();
        ipFinder.setAddresses(Arrays.asList("192.168.1.105:49200..49220"));
        // Overriding IP finder.
        discoverySpi.setIpFinder(ipFinder);
        // Overriding discovery SPI.
        cfg.setDiscoverySpi(discoverySpi);

        TcpCommunicationSpi commSpi=new TcpCommunicationSpi();
        commSpi.setLocalPort(48300);
        cfg.setCommunicationSpi(commSpi);

        // Starting a node.
        Ignite ignite = Ignition.start(cfg);
        System.out.println("IgniteCluster3 start OK.");
        CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<Integer, String>();
        cacheCfg.setBackups(1);
        cacheCfg.setCacheMode(CacheMode.PARTITIONED);
        cacheCfg.setName("myCache");
        IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cacheCfg);
        cache.put(3, "ignite3");
        System.out.println(cache.get(1));
        System.out.println(cache.get(2));
        System.out.println(cache.get(3));
    }
}

 

Ignite集群管理——基于静态IP的节点发现