Apache Ignite內存數據組織平台是一個高性能、集成化、混合式的企業級分布式架構解決方案,核心價值在於可以幫助我們實現分布式架構透明化,開發人員根本不知道分布式技術的存在,可以使分布式緩存、計算、存儲等一系列功能嵌入應用內部,和應用的生命周期一致,大幅降低了分布式應用開發、調試、測試、部署的難度和復雜度。
4.2.Ignite服務網格
Ignite服務網格以一種優雅的方式實現了分布式RPC,定義一個服務非常簡單:
下面通過一個簡單的示例演示下Ignite服務的定義、實現、部署和調用:
4.2.1.服務定義
public interface MyCounterService {
int get() throws CacheException;
}
4.2.2.服務實現
public class MyCounterServiceImpl implements Service, MyCounterService {
@Override public int get() {
return 0;
}
}
4.2.3.服務部署
ClusterGroup cacheGrp = ignite.cluster().forCache("myCounterService");
IgniteServices svcs = ignite.services(cacheGrp);
svcs.deployNodeSingleton("myCounterService", new MyCounterServiceImpl());
4.2.4.服務調用
MyCounterService cntrSvc = ignite.services().
serviceProxy("myCounterService", MyCounterService.class, /*not-sticky*/false);
System.out.println("value : " + cntrSvc.get());
是不是很簡單?
關於服務網格的詳細描述,請看這里。
4.3.Ignite計算網格
Ignite的分布式計算是通過IgniteCompute接口提供的,它提供了在集群節點或者一個集群組中運行很多種類型計算的方法,這些方法可以以一個分布式的形式執行任務或者閉包。
本方案中采用的是ComputeTask方式,它是Ignite對於簡化內存內MapReduce的抽象。ComputeTask定義了要在集群內執行的作業以及這些作業到節點的映射,還定義了如何處理作業的返回值(Reduce)。所有的IgniteCompute.execute(...)方法都會在集群上執行給定的任務,應用只需要實現ComputeTask接口的map(...)和reduce(...)方法即可,這幾個方法的詳細描述不在本文討論的范圍內。
下面是一個ComputeTask的簡單示例:
IgniteCompute compute = ignite.compute();
int cnt = compute.execute(CharacterCountTask.class, "Hello Grid Enabled World!");
System.out.println(">>> Total number of characters in the phrase is '" + cnt + "'.");
private static class CharacterCountTask extends ComputeTaskSplitAdapter<String, Integer> {
@Override
public List<ClusterNode> split(int gridSize, String arg) {
String[] words = arg.split(" ");
List<ComputeJob> jobs = new ArrayList<>(words.length);
for (final String word : arg.split(" ")) {
jobs.add(new ComputeJobAdapter() {
@Override public Object execute() {
System.out.println(">>> Printing '" + word + "' on from compute job.");
return word.length();
}
});
}
return jobs;
}
@Override
public Integer reduce(List<ComputeJobResult> results) {
int sum = 0;
for (ComputeJobResult res : results)
sum += res.<Integer>getData();
return sum;
}
}
通過這樣一個簡單的類,就實現了夢寐以求的分布式計算!
關於計算網格的詳細描述,請看這里。
參考:http://www.infoq.com/cn/articles/ignite-lucene-log4j2-log-query
