代码位置
https://github.com/lilihongjava/ignite_examples/tree/main/ignite-01
安装
下载ignite安装包,apache-ignite-2.11.0-bin.zip
选2台服务器,解压文件,在bin目录下(如/root/ignite/apache-ignite-2.11.0-bin/bin)执行
./ignite.sh ../examples/config/example-ignite.xml
日志如下:
[15:56:53] Ignite node started OK (id=235e6170)
[15:56:53] Topology snapshot [ver=1, locNode=235e6170, servers=1, clients=0, state=ACTIVE, CPUs=8, offheap=3.9GB, heap=4.3GB]
[15:56:53] ^-- Baseline [id=0, size=1, online=1, offline=0]
[15:56:58] Joining node doesn't have stored group keys [node=acd7e430-d4b3-443c-816f-d3a3f6c8fa1f]
[15:56:58] Topology snapshot [ver=2, locNode=235e6170, servers=2, clients=0, state=ACTIVE, CPUs=16, offheap=7.8GB, heap=8.6GB]
[15:56:58] ^-- Baseline [id=0, size=2, online=2, offline=0]
Ignite node started OK,则表示节点启动成功,此例中在2台服务器启动,所以 online=2。
helloworld程序
创建maven项目,添加包依赖
<properties>
<ignite.version>2.11.0</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>
</dependencies>
创建HelloWorld.java 文件
cfg.setClientMode(true),作为客户端节点启动。
采用tcp发现方式(TcpDiscoveryMulticastIpFinder)查找服务节点
Ignition.start(cfg), 启动客户端节点
IgniteConfiguration cfg = new IgniteConfiguration();
// The node will be started as a client node.
cfg.setClientMode(true);
// 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("10.1.12.215:47500..47509"));
cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
// Starting the node
Ignite ignite = Ignition.start(cfg);
客户端创建cache(myCache)
// Create an IgniteCache and put some values in it.
IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");
cache.put(1, "Hello");
cache.put(2, "World!");
System.out.println(">> Created the cache and add the values.");
向服务器发起计算任务
ignite.compute(ignite.cluster().forServers()).broadcast(new RemoteTask());
System.out.println(">> Compute task is executed, check for output on the server nodes.");
RemoteTask 任务如下
打印myCache内容
private static class RemoteTask implements IgniteRunnable {
@IgniteInstanceResource
Ignite ignite;
@Override public void run() {
System.out.println(">> Executing the compute task");
System.out.println(
" Node ID: " + ignite.cluster().localNode().id() + "\n" +
" OS: " + System.getProperty("os.name") +
" JRE: " + System.getProperty("java.runtime.name"));
IgniteCache<Integer, String> cache = ignite.cache("myCache");
System.out.println(">> " + cache.get(1) + " " + cache.get(2));
}
}
结果
客户端日志
Topology snapshot […clients=1… 表示成功加入集群
[16:09:05] Ignite node started OK (id=72ba4f53)
[16:09:05] Topology snapshot [ver=3, locNode=72ba4f53, servers=2, clients=1, state=ACTIVE, CPUs=24, offheap=7.8GB, heap=12.0GB]
[16:09:05] ^-- Baseline [id=0, size=2, online=2, offline=0]
>> Created the cache and add the values.
>> Compute task is executed, check for output on the server nodes.
[16:09:08] Ignite node stopped OK [uptime=00:00:02.482]
服务端日志
打印>> Hello World!, 即myCache的内容
[16:08:23] Topology snapshot [ver=3, locNode=235e6170, servers=2, clients=1, state=ACTIVE, CPUs=24, offheap=7.8GB, heap=12.0GB]
[16:08:23] ^-- Baseline [id=0, size=2, online=2, offline=0]
>> Executing the compute task
Node ID: 235e6170-d2f7-4ea6-a7b3-a67cfa50238c
OS: Linux JRE: OpenJDK Runtime Environment
>> Hello World!
[16:08:55] Topology snapshot [ver=4, locNode=235e6170, servers=2, clients=0, state=ACTIVE, CPUs=16, offheap=7.8GB, heap=8.6GB]
[16:08:55] ^-- Baseline [id=0, size=2, online=2, offline=0]