簡介
Apache Ignite 記憶體資料組織架構是一個高性能、內建化和分布式的記憶體計算和事務平台,用于大規模的資料集處理,比傳統的基于磁盤或閃存的技術具有更高的性能,同時他還為應用和不同的資料源之間提供高性能、分布式記憶體中資料組織管理的功能。
安裝
從 https://ignite.apache.org/download.cgi#binaries 下載下傳最新的安裝包,這裡我下載下傳的是 apache-ignite-fabric-2.3.0-bin.zip 包。下載下傳後解壓就可以直接使用了。
運作
進入到 ${IGNITE_HOME}/bin 目錄,然後運作
./ignite.sh
...
[00:24:04] To start Console Management & Monitoring run ignitevisorcmd.{sh|bat}
[00:24:04]
[00:24:04] Ignite node started OK (id=01af1a02)
[00:24:04] Topology snapshot [ver=1, servers=1, clients=0, CPUs=2, heap=1.0GB]
...
複制
其中:
- servers=1 表示目前 Ignite 叢集中隻有一個節點。
- clients=0 表示目前沒有用戶端連接配接到此叢集。
此時,我們可以在另外一台機器上運作同樣的指令來再啟動一個 Ignite,此時我們就可以看到
...
[00:41:21] Topology snapshot [ver=2, servers=2, clients=0, CPUs=2, ...
...
複制
可以看到 servers=2,說明有一個新節點加入了叢集。
測試
Ignite 叢集已經有了,下面我們來看看怎樣使用 Ignite 作為分布式緩存系統使用。
首先建立一個 Maven 工程,pom.xml 檔案内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.ignitestudy</groupId>
<artifactId>ignitestudy</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ignitestudy</name>
<url>http://maven.apache.org</url>
<properties>
<ignite.version>2.3.0</ignite.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<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>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
複制
下面是 Java 中怎樣使用 Cache 的例子
package my.ignitestudy.datagrid;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
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.vm.TcpDiscoveryVmIpFinder;
import org.apache.ignite.transactions.Transaction;
import java.util.Arrays;
public class Test {
public static void main( String[] args ) throws Exception {
Ignite ignite = getIgnite();
testGetPut(ignite);
testAtomOperation(ignite);
}
private static Ignite getIgnite() {
TcpDiscoverySpi spi = new TcpDiscoverySpi();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
ipFinder.setAddresses(Arrays.asList("192.168.0.192:47500..47509"));
spi.setIpFinder(ipFinder);
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setDiscoverySpi(spi);
cfg.setClientMode(true);
Ignite ignite = Ignition.start(cfg);
return ignite;
}
private static void testGetPut(Ignite ignite) {
IgniteCache<String, String> cache = ignite.getOrCreateCache("myCache");
for (int i = 0; i < 10; i++) {
cache.put("mykey_" + i, "myvalue_" + i);
}
for (int i = 0; i < 10; i++) {
String key = "mykey_" + i;
System.out.println("Got [key=" + key + ", val=" + cache.get(key) + ']');
}
}
private static void testAtomOperation(Ignite ignite) {
IgniteCache<String, Integer> cache = ignite.getOrCreateCache("myCache");
Integer oldValue = cache.getAndPutIfAbsent("MyKey", 11);
System.out.println("MyKey: " + oldValue);
boolean success = cache.putIfAbsent("MyKey", 22);
System.out.println("MyKey: " + success);
oldValue = cache.getAndReplace("MyKey", 11);
System.out.println("MyKey replace: " + oldValue);
success = cache.replace("MyKey", 22);
System.out.println("MyKey replace: " + success);
success = cache.replace("MyKey", 2, 22);
System.out.println("MyKey replace: " + success);
success = cache.remove("MyKey", 1);
System.out.println("MyKey remove: " + success);
}
}
複制
上面 Java 代碼中我們是使用程式設計的方式來連接配接叢集,如下:
TcpDiscoverySpi spi = new TcpDiscoverySpi();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
ipFinder.setAddresses(Arrays.asList("192.168.0.192:47500..47509"));
spi.setIpFinder(ipFinder);
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setDiscoverySpi(spi);
cfg.setClientMode(true);
Ignite ignite = Ignition.start(cfg);
複制
也可以使用指定的配置檔案來擷取到叢集的連接配接,比如:
Ignite ignite = Ignition.start("... config file path ...");
複制