Ignite Compute helloworld-分布式计算
目录
1. 背景
2. 步骤
2.1. 在pom.xml里加ignite依赖
2.2. Ignite compute server 1
2.2. Ignite compute server 2
2.3. Ignite compute client
2.4. Ignite compute client - 异步
1. 背景
利用Ignite做一个分布式计算。1个Ignite Client 和 2 ignite server.
2. 步骤
2.1. 在pom.xml里加ignite依赖
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><ignite.version>2.11.1</ignite.version></properties><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><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-web</artifactId><version>${ignite.version}</version></dependency><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-ssh</artifactId><version>${ignite.version}</version></dependency></dependencies>
2.2. Ignite compute server 1
import java.util.Collections;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;public class IgniteComputeServer1 {public static void main(String[] args){// Preparing IgniteConfiguration using Java APIsIgniteConfiguration cfg = new IgniteConfiguration();// Classes of custom Java logic will be transferred over the wire from this app.cfg.setPeerClassLoadingEnabled(true);// Setting up an IP Finder to ensure the client can locate the servers.TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));System.out.println("starting ignition...");Ignite ignite = Ignition.start(cfg);System.out.println("ignite server started");}
}
运行结果
[22:45:22] To start Console Management & Monitoring run ignitevisorcmd.{sh|bat}
[22:45:22]
[22:45:22] Ignite node started OK (id=866b923f)
[22:45:22] Topology snapshot [ver=1, locNode=866b923f, servers=1, clients=0, state=ACTIVE, CPUs=16, offheap=3.2GB, heap=3.5GB]
[22:45:22] ^-- Baseline [id=0, size=1, online=1, offline=0]
ignite server started
2.2. Ignite compute server 2
import java.util.Collections;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;public class IgniteComputeServer2 {public static void main(String[] args){IgniteConfiguration cfg = new IgniteConfiguration();cfg.setPeerClassLoadingEnabled(true);TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));System.out.println("starting ignition...");Ignite ignite = Ignition.start(cfg);System.out.println("ignite server started");}
}
运行结果
[22:45:22] Ignite node started OK (id=866b923f)
[22:45:22] Topology snapshot [ver=1, locNode=866b923f, servers=1, clients=0, state=ACTIVE, CPUs=16, offheap=3.2GB, heap=3.5GB]
[22:45:22] ^-- Baseline [id=0, size=1, online=1, offline=0]
ignite server started
2.3. Ignite compute client
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.lang.IgniteCallable;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;public class IgniteClient {public static void main(String[] args){Ignition.setClientMode(true);IgniteConfiguration cfg = new IgniteConfiguration();cfg.setPeerClassLoadingEnabled(true);TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder)); Ignite ignite = Ignition.start(cfg);Collection<IgniteCallable<Integer>> calls = new ArrayList<>();// Iterate through all the words in the sentence and create Callable jobs.for (final String word : "Count characters using callable".split(" ")) {calls.add(new IgniteCallable<Integer>() {@Override public Integer call() throws Exception {System.out.println("IgniteCallable:"+word);return word.length();}});}// Execute collection of Callables on the grid.Collection<Integer> res = ignite.compute().call(calls);int sum = 0;// Add up individual word lengths received from remote nodes.for (int len : res)sum += len;System.out.println(">>> Total number of characters in the phrase is '" + sum + "'."); }
}
运行结果
Ignite client 把4个线程分发到两个Ignite compute server 上。计算完后返回回来
[22:46:35] Ignite node started OK (id=65c8572d)
[22:46:35] Topology snapshot [ver=3, locNode=65c8572d, servers=2, clients=1, state=ACTIVE, CPUs=16, offheap=6.4GB, heap=11.0GB]
[22:46:35] ^-- Baseline [id=0, size=2, online=2, offline=0]
>>> Total number of characters in the phrase is '28'.
Ignite compute server 1执行了两个线程
[22:46:20] Joining node doesn't have stored group keys [node=d99385c3-6303-4366-bbd2-df4847d6bbb2]
[22:46:20] Topology snapshot [ver=2, locNode=866b923f, servers=2, clients=0, state=ACTIVE, CPUs=16, offheap=6.4GB, heap=7.1GB]
[22:46:20] ^-- Baseline [id=0, size=2, online=2, offline=0]
[22:46:35] Topology snapshot [ver=3, locNode=866b923f, servers=2, clients=1, state=ACTIVE, CPUs=16, offheap=6.4GB, heap=11.0GB]
[22:46:35] ^-- Baseline [id=0, size=2, online=2, offline=0]
IgniteCallable:Count
IgniteCallable:using
Ignite compute server 2执行了两个线程
ignite server started
[22:46:35] Topology snapshot [ver=3, locNode=d99385c3, servers=2, clients=1, state=ACTIVE, CPUs=16, offheap=6.4GB, heap=11.0GB]
[22:46:35] ^-- Baseline [id=0, size=2, online=2, offline=0]
IgniteCallable:characters
IgniteCallable:callable
2.4. Ignite compute client - 异步
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.lang.IgniteCallable;
import org.apache.ignite.lang.IgniteFuture;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;public class IgniteAsyncClient {public static void main(String[] args){Ignition.setClientMode(true);// Preparing IgniteConfiguration using Java APIsIgniteConfiguration cfg = new IgniteConfiguration();// Classes of custom Java logic will be transferred over the wire from this app.cfg.setPeerClassLoadingEnabled(true);// Setting up an IP Finder to ensure the client can locate the servers.TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));Ignite ignite = Ignition.start(cfg);Collection<IgniteCallable<Integer>> calls = new ArrayList<>();// Iterate through all the words in the sentence and create Callable jobs.for (final String word : "Count characters using callable".split(" ")) {calls.add(new IgniteCallable<Integer>() {@Override public Integer call() throws Exception {System.out.println(System.currentTimeMillis()+"start IgniteCallable:"+word);Thread.sleep(1000+word.length());System.out.println(System.currentTimeMillis()+"end IgniteCallable:"+word);return word.length();}});}// Execute collection of Callables on the grid.IgniteFuture<Collection<Integer>> res1 = ignite.compute().callAsync(calls);
// Collection<Integer> res= res1.get();
// int sum = 0;
// // Add up individual word lengths received from remote nodes.
// for (int len : res)
// sum += len;
// System.out.println(System.currentTimeMillis()+">>> Total number of characters in the phrase is '" + sum + "'.");System.out.println(System.currentTimeMillis()+"Finished");}
}
如果 加上 Collection<Integer> res= res1.get(); 其实结果又变成同步了。会等待所有线程完成。
Ignite Compute helloworld-分布式计算相关推荐
- 可能大家都能跑通的ignite的HelloWorld
ignite的开发,说难很难,说简单也很简单,开头第一个小程序,一定是HelloWorld. 下面开始HelloWorld 开发环境的配置,我是使用Spring+SpringMVC+MyBatis+M ...
- Ignite 的helloworld第二弹!(附源码!下载即用)
Ignite是个好玩意儿,在同一局域网下,运行相同的配置文件的话,就会自动拓扑发现.于是,就相当于,一个程序运行在一个主机上(但是这个主机是分成若干个小主机)这台主机的配置是由组成这个主机的节点之和, ...
- Apache Ignite——集合分布式缓存、计算、存储的分布式框架
Apache Ignite内存数据组织平台是一个高性能.集成化.混合式的企业级分布式架构解决方案,核心价值在于可以帮助我们实现分布式架构透明化,开发人员根本不知道分布式技术的存在,可以使分布式缓存.计 ...
- Apache Ignite(五):Ignite的集群部署
Ignite具有非常先进的集群能力,本文针对和集群有关的技术点做一个简短的介绍,然后针对实际应用的可能部署形式做了说明和对比,从中我们可以发现,Ignite平台在部署的灵活性上,具有很大的优势.\ 1 ...
- apache ignite_Apache Ignite变得简单:第一个Java应用程序
apache ignite 在本文中,我们将更进一步,让您完成第一个Ignite应用程序的创建,以从分布式缓存中进行读写操作. 作为第一个示例,我们将尽可能简单地向您展示如何用Java编写用于处理Ap ...
- Apache Ignite变得简单:第一个Java应用程序
在本文中,我们将更进一步,让您完成第一个Ignite应用程序的创建,以从分布式缓存中进行读写操作. 作为第一个示例,我们将尽可能简单地向您展示如何用Java编写用于处理Apache Ignite集群数 ...
- Ignite 数据网格快速学习(一)
PS:所有的代码使用的是Ignite2.0 #1.数据的基本存储操作 public static void main(String[] args) {//Ignition.start(...)启动一个 ...
- Ignite GridTaskWorker 执行分析
分析入口 ignite.compute().call(IgniteCallable<R> job) 1, IgniteComputeImpl.callAsync0 2, GridClosu ...
- 全面对比,深度解析 Ignite 与 Spark
经常有人拿 Ignite 和 Spark 进行比较,然后搞不清两者的区别和联系.Ignite 和 Spark,如果笼统归类,都可以归于内存计算平台,然而两者功能上虽然有交集,并且 Ignite 也会对 ...
最新文章
- SQL Server各种日期计算方法
- iOS 命令行自动打包 (archive)
- 怎么使用CorelDRAW 中的默认调色板
- java如何使用elasticsearch
- JAVA中方法的类型转换_Java中几种常用数据类型之间转换的方法
- 下列哪个不是目前python里的内置模块-python引入模块的五种方式与内置模块
- EASYHOOK逆向寒假生涯(20/100)
- 针对接口编程,不要针对实现编程
- java成员变量的初始化
- linux 文件系统路径,Linux编程 1 (文件系统路径说明, 目录结构说明)
- 刷新按钮_处理数据透视表的隐藏选项(四):固定报表刷新前后的列宽和格式...
- Centos 虚拟机克隆后eth0网卡打不开
- 关于前端样式定位的一些自己的看法
- 计算机操作系统笔记(四)
- Usability Testing Demystified
- Node.js简介及安装
- 计算机平面设计教材,《计算机平面设计软件应用——全国中等职业技术学校计算机教材》低价购书_教材教辅考试_孔网...
- css实现html透明效果
- Java String 的最大长度
- PAP和CHAP认证方式
热门文章
- global.asax不执行原因
- 安装node.js,CoffeeScript,Express.js,mysql,jade
- Android Logcat 报错:Could not create the view: For input string:
- RTS与CTS的含义
- 面试官系统精讲Java源码及大厂真题 - 11 HashSet、TreeSet 源码解析
- CryptoJS DES加密示例
- Centos7.4系统下安装httpd,mariadb,php7.1环境运行禅道
- Top20的OpenSSH服务器最佳安全实践--SSHD_CONFIG配置文件详细解读
- 【TencentOS tiny】深度源码分析(4)——消息队列
- idea中git分支、合并与使用