Ignite实战之二 第一个Data Grid应用

启动一个Ignite实例

bin/ignite.sh

[17:10:44] Ignite node started OK (id=c44bebb1)
[17:10:44] Topology snapshot [ver=1, servers=1, clients=0, CPUs=4, heap=1.8GB]

创建maven工程,并加入相关依赖


    2.2.0


    
        org.apache.ignite
        ignite-core
        ${ignite.version}
    
    
        org.apache.ignite
        ignite-spring
        ${ignite.version}
    
    
        org.apache.ignite
        ignite-indexing
        ${ignite.version}
    


创建Data Grid应用FirstCacheDemo


public class FirstCacheDemo {
    public static void main(String[] args) throws IgniteException {
        Ignite ignite = Ignition.start();
        IgniteCache cache = ignite.getOrCreateCache("c1");
        cache.clear();

        int keyCnt = 10;
        System.out.println("Synchronously put records ...");
        for (int i = 0; i < keyCnt; i++)
            cache.put(i, Integer.toString(i));

        for (int i = 0; i < keyCnt; i++)
            System.out.println("Got [key=" + i + ", val=" + cache.get(i) + "] ");

    }
}

运行后输出为

[17:18:48] Ignite node started OK (id=0957a691)
[17:18:48] Topology snapshot [ver=12, servers=2, clients=0, CPUs=4, heap=3.6GB]
Synchronously put records ...
Got [key=0, val=0]
Got [key=1, val=1]
Got [key=2, val=2]
Got [key=3, val=3]
Got [key=4, val=4]
Got [key=5, val=5]
Got [key=6, val=6]

Got [key=7, val=7]
Got [key=8, val=8]
Got [key=9, val=9]

可以看出,put 是一个同步方法,其相对应的异步方法为 putAsync, 这也是Ignite大多数异步方法的命名规则。 我们将 put 改为 putAsync

System.out.println("Asynchronously put records ...");
for (int i = 0; i < keyCnt; i++)
    cache.putAsync(i, Integer.toString(i));

for (int i = 0; i < keyCnt; i++)
    System.out.println("Got [key=" + i + ", val=" + cache.get(i) + "] ");

输出结果为

Asynchronously put records ...
Got [key=0, val=null]
Got [key=1, val=1]
Got [key=2, val=2]
Got [key=3, val=3]
Got [key=4, val=4]
Got [key=5, val=5]
Got [key=6, val=6]
Got [key=7, val=7]
Got [key=8, val=8]
Got [key=9, val=9]

可以看出,简单的 get 方法不能保证数据能正常读出,需要配合使用 IgniteFuture,等待所有 future 结束。

System.out.println("Asynchronously put records ...");
Collection> futs = new ArrayList<>();
for (int i = 0; i < keyCnt; i++)
    futs.add(cache.putAsync(i, Integer.toString(i)));

// Wait for completion of all futures.
for (IgniteFuture fut : futs)
    fut.get();

for (int i = 0; i < keyCnt; i++)
    System.out.println("Got [key=" + i + ", val=" + cache.get(i) + "] ");

在执行就一切正常了

Asynchronously put records ...
Got [key=0, val=0]
Got [key=1, val=1]
Got [key=2, val=2]
Got [key=3, val=3]
Got [key=4, val=4]
Got [key=5, val=5]
Got [key=6, val=6]
Got [key=7, val=7]
Got [key=8, val=8]
Got [key=9, val=9]

Configure Ignite

之前我们使用了Ignite的默认配置,直接调用 Ignition.start() 就启动了实例,同样我们可以对所启动的实例做特殊配置,一般有两种方法

  • 通过code进行配置
 TcpDiscoverySpi spi = new TcpDiscoverySpi();
 TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
 ipFinder.setAddresses(Arrays.asList("10.xxx.xxx.xxx:47500..47509"));
 spi.setIpFinder(ipFinder);
 IgniteConfiguration cfg = new IgniteConfiguration();
 cfg.setDiscoverySpi(spi);
 // Enable client mode.
 cfg.setClientMode(true);
 Ignite ignite = Ignition.start(cfg);
  • 通过配置文件导入
  Ignite ignite = Ignition.start("config/xxx.xml");

直接这么写会出错,网上有人说在IDE里需要设置JVM变量 -DIGNITE_HOME 即可,但仍然失败, 最终发现如下写法是OK的

  Ignite ignite = Ignition.start("file:///D:\Work\ignitedemo\resource\example-cache.xml");

至此,一个比较完整的First Data Grid应用就算是OK了。

你可能感兴趣的:(Ignite实战之二 第一个Data Grid应用)