Ignite Compute helloworld-分布式计算
作者:互联网
目录
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 APIs
IgniteConfiguration 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
标签:Ignite,ignite,Compute,22,分布式计算,org,apache,import 来源: https://blog.csdn.net/keeppractice/article/details/123037611