Ignite是一个基于java的应用程序。本章将重点介绍不同的安装类型和配置。首先,我们将在一台机器上安装Ignite,然后再构建一个多节点集群,以运行HelloWorld示例。
环境准备
Apache spark团队正式支持Oracle JDK 7版本及以上版本。但是,我们在一些项目中使用IBM J9和OpenJDK 7安装了Apache Ignite。
- JDK Oracle JDK 7 and above
- OS Linux, MacOS (10.6 and above), Windows XP and above, Windows Server (2008 and
above)
*RAM Default value 1 GB ram for development environment
安装
- 下载地址:https://ignite.apache.org/download.cgi#binaries
- 解压安装包:unzip apache-ignite-fabric-1.6.0-bin.zip
- 设置环境变量:
export IGNITE_HOME=YOUR_IGNITE_HOME_PATH - 拷贝ignite安装目录下的/libs/optional/ignite-rest-http/ 文件目录中的jar包到$IGNITE_HOME/libs/目录下
- 运行启动命令:ignite.sh $IGNITE_HOME/examples/config/example-cache.xml
- 在浏览器中访问URL:http://localhost:8080/ignite?cmd=version
获得以下响应:{"error":"","response":"1.6.0","sessionToken":"","successStatus":0}
在单台主机上运行多实例
通过多播自动发现机制,Ignite允许集群成员使用多播通信来发现彼此。它还允许在一台主机上运行几个Ignite实例。必须在不同的shell中运行以下命令,才能在单个主机上运行几个Ignite实例。
ignite.sh IGNITE_HOME/examples/config/example-cache.xml ![image.png](https://img.haomeiwen.com/i2657590/95a0960f7c031d2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 您可以在ignite中更改默认配置。sh文件位于IGNITE_HOME/bin目录。在ignite中找到bash脚本的以下片段。sh文件:
if [ -z "$JVM_OPTS" ] ; then
if [[ `"$JAVA" -version 2>&1 | egrep "1\.[7]\."` ]]; then
JVM_OPTS="-Xms1g -Xmx1g -server -XX:+AggressiveOpts -XX:MaxPermSize=256m"
else
JVM_OPTS="-Xms1g -Xmx1g -server -XX:+AggressiveOpts -XX:MaxMetaspaceSize=256m"
fi
fi
例如,将-Xms1g -Xmx1g参数替换为-Xms512m -Xmx512m
在不同的主机上配置多节点集群
这种方法非常类似于单主机集群。首先,在每台主机上,我们必须在iptables文件中打开几个端口。通过向iptables文件添加以下命令,它将打开主机上所有必要的端口。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 47500:47509 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 47400 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 47100 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 47101 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 48100 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 48101 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 31100 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 31101 -j ACCEPT
然后重启iptables服务
/etc/init.d/iptables restart
然后更改配置文件 example-cache.xml的IP地址:
<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
<property name="addresses">
<list>
<value>HOST_IP_ADDRESS:47500..47509</value>
</list>
</property>
</bean>
现在,我们可以在单独的主机上启动Apache Ignite的每个实例,每个实例都应该相互发现。
使用Rest client对Apache ignite进行操作
现在,我们可以使用Ignite rest API来操纵Apache Ignite缓存。Ignite提供了一个开箱即用的HTTP REST客户端,它使您能够使用REST API通过HTTP和HTTPS协议与Ignite网格进行通信。
http://127.0.0.1:8080/ignite?cmd=getorcreate&cacheName=testCache
获得的响应:
{"error":"","response":null,"sessionToken":"","successStatus":0}
一旦ignite节点或集群启动并运行,您可以使用命令行工具ignitevisor来显示集群中节点的各种统计信息。打开shell或终端并运行以下命令。
$ignitevisorcmd.sh
image.png
ignitevisor是一个CLI(命令行)工具,用于管理和监视ignite集群。在ignitevisor控制台,可以查找缓存。
使用rest API放置一些缓存元素(条目)。在浏览器中键入以下URL:
http://127.0.0.1:8080/ignite?cmd=put&key=moscow&val=777&cacheName=testCache
现在ignitevisorcm命令行中输入'open'命令,选择启动加载是使用的配置文件。使用以下命令查看缓存信息:
visor> cache 'testCache'
显示如下信息:
Time of the snapshot: 2018-07-23 15:20:34
+==================================================================================================================+
| Name(@) | Mode | Nodes | Entries (Heap / Off-heap) | Hits | Misses | Reads | Writes |
+==================================================================================================================+
| default(@c0) | PARTITIONED | 1 | min: 0 (0 / 0) | min: 0 | min: 0 | min: 0 | min: 0 |
| | | | avg: 0.00 (0.00 / 0.00) | avg: 0.00 | avg: 0.00 | avg: 0.00 | avg: 0.00 |
| | | | max: 0 (0 / 0) | max: 0 | max: 0 | max: 0 | max: 0 |
+----------------+-------------+-------+---------------------------+-----------+-----------+-----------+-----------+
| testCache(@c1) | PARTITIONED | 1 | min: 1 (0 / 1) | min: 0 | min: 0 | min: 0 | min: 0 |
| | | | avg: 1.00 (0.00 / 1.00) | avg: 0.00 | avg: 0.00 | avg: 0.00 | avg: 0.00 |
| | | | max: 1 (0 / 1) | max: 0 | max: 0 | max: 0 | max: 0 |
+------------------------------------------------------------------------------------------------------------------+
Use "-a" flag to see detailed statistics.
现在,缓存大小是1,因为我们只在其中放入一个元素。如果您将在上面的rest API中更改密钥和缓存的值并再次调用URL,那么它将在缓存中再放入一个元素,那么缓存的大小应该会增加。要从ignite缓存中检索元素,请在浏览器中输入以下URL:
http://localhost:8080/ignite?cmd=get&key=moscow&cacheName=testCache
获得以下响应:
{"affinityNodeId":"4ca9af1c-7404-4f30-a92d-2c2ae08b8b85","error":"","response":"777","sess\
ionToken":"","successStatus":0}
也可以通过一个rest API调用从缓存中获取一些值。下面是一个示例:
http://localhost:8080/ignite?cmd=getall&k1=moscow&k2=vladimir&k3=tver&cacheName=testCache
获得以下响应:
{"successStatus":0,"affinityNodeId":null,"error":null,"sessionToken":null,"response":{"moscow":"777"}}
Java 客户端
在本节中,我们将介绍如何创建第一个Ignite应用程序,以便从分布式缓存中进行读写(put/get)。
项目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>com.mycookcode.bigData.ignite</groupId>
<artifactId>ignite-java</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ignite-java</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ignite.version>2.6.0</ignite.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j</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-slf4j</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.code.morphia</groupId>
<artifactId>morphia</artifactId>
<version>RELEASE</version>
</dependency>
<!--
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
<artifactId>jersey-guava</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<id>build-query</id>
<configuration>
<mainClass>com.mycookcode.bigData.ignite.client.HelloIgnite</mainClass>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
<filename>HelloIgnite-runnable.jar</filename>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
项目代码:
package com.mycookcode.bigData.ignite.client;
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.multicast.TcpDiscoveryMulticastIpFinder;
import java.util.Arrays;
/**
* Created by zhaolu on 2018/7/23.
*/
public class HelloIgnite {
public static void main(String[] args)
{
System.out.println("Hello ignite");
//创建一个TCP Discovery SPI实例
TcpDiscoverySpi spi = new TcpDiscoverySpi();
//创建一个tcp discovery multicast ip finder实例
TcpDiscoveryMulticastIpFinder tcMP = new TcpDiscoveryMulticastIpFinder();
tcMP.setAddresses(Arrays.asList("localhost"));//添加主机地址
spi.setIpFinder(tcMP);
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setClientMode(false);
cfg.setDiscoverySpi(spi);
Ignite ignite = Ignition.start(cfg);
IgniteCache<Integer,String> cache = ignite.getOrCreateCache("myCacheName");
for(int i = 0;i <= 100;i++)
{
cache.put(i,Integer.toString(i));
}
for(int i = 0;i <= 100;i++)
{
System.out.println("Cache get:"+ cache.get(i));
}
ignite.close();
}
}
执行maven命令:
mvn clean install
执行编译后的jar文件:
java -jar .\target\HelloIgnite-runnable.jar
SQL客户端
Apache Ignite在缓存中提供SQL查询执行,SQL语法是ANSI-99兼容的。Apache Ignite开箱即用的是JDBC驱动程序,它允许您使用标准SQL查询连接到Ignite缓存并从缓存中检索分布式数据。
网友评论