在之前的例子中, 对cache的input和get操作都是在当前节点完成的。而作为分布式应用,一个最基本的诉求就是在A点存储,在B点操作。我们看在Ingite中如何实现所谓的Computing Grid应用。
依然使用之前的例子,只是对get逻辑稍作改动
public class FirstCacheDemo {
public static void main(String[] args) throws IgniteException {
Ignite ignite = Ignition.start();
IgniteCache<Integer, Object> 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));
IgniteCompute compute = ignite.compute(ignite.cluster().forRemotes());
compute.broadcast(() -> {for (int i = 0; i < keyCnt; i++)
System.out.println("Got [key=" + i + ", val=" + ignite.getOrCreateCache("c1").get(i) + "] ");});
}
}
运行后的输出中没有get逻辑的日志
[14:47:01] Topology snapshot [ver=8, servers=2, clients=0, CPUs=4, heap=3.6GB]
Synchronously put records ...
在其他Ingite实例的终端中有如下输出
[14:46:57] Topology snapshot [ver=7, servers=1, clients=0, CPUs=4, heap=1.8GB]
[14:46:59] Topology snapshot [ver=8, servers=2, clients=0, CPUs=4, heap=3.6GB]
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]
Got [key=10, val=10]
可见,get的逻辑在remote实例中得到了执行。值得注意的是, 该段逻辑
System.out.println("Got [key=" + i + ", val=" + ignite.getOrCreateCache("c1").get(i) + "] ");});
并没有在其他节点上显性的部署,但依然可以执行成功。这就是所谓的Zero Deploment的威力了,是不是有点儿意思了。