ignite是分布式内存网格的一种实现,其基于java平台,具备可持久化,分布式事务,分布式计算等特色,此外还支持丰富的键值存储以及SQL语法(基于h2引擎),能够当作是一个分布式内存数据库。java
与ignite相似的产品有gemfire(12306目前正在使用),其开源版为geode。与gemfire相比,ignite对sql的支持比较完善,提供了数据并置来提高性能,还有对分布式事物的支持以及对spring的集成都比较友好,很方便进行嵌入式集成进应用服务。node
ignite有两种使用方式: 一种是从官网下载release版本程序,解压运行部署,另一种是经过嵌入式集成进现有应用程序。git
下载地址:https://ignite.apache.org/download.cgigithub
下载后获得apache-ignite-fabric-2.3.0-bin.zip
压缩包,解压后进入bin路径:spring
主要用到两个脚本: ignite.bat
启动脚本, ignitevisorcmd.bat
监控脚本sql
执行ignite.bat
脚本便可启动一个ignite服务数据库
执行ignitevisorcmd.bat
能够进入监控命令界面:apache
输入open
命令选择配置文件,这里选择默认的0 | config\default-config.xml
输入数字0
便可缓存
经常使用命令以下:bash
命令 | 功能 |
---|---|
top | 查看集群网络拓扑图 |
cache | 查看总体缓存状况 |
config | 查看节点配置 |
open | 打开一个配置文件链接集群 |
close | 关闭该链接 |
更多详细命令能够经过输入help命令查看命令帮助(输入help回车)。
经过JAVA服务使用已启动的ignite集群,JAVA服务可使用客户端模式(Client),应用端不存储数据,或者使用服务端模式(Server)变成一个节点加入现有ignite集群,则应用端会缓存部分数据。若是是使用服务端模式的话,整个集群其实均可以使用应用节点组成集群,也就是上面所说的嵌入式集成。这样能够对节点进行定制化处理,更为灵活。
这里使用Client模式演示一下简单使用:
1) 添加相关依赖
<dependency> <groupId>org.apache.ignite</groupId> <artifactId>ignite-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.apache.ignite</groupId> <artifactId>ignite-spring</artifactId> <version>2.3.0</version> </dependency>
2) 定义配置文件
default-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="igniteCfg" class="org.apache.ignite.configuration.IgniteConfiguration"> <property name="clientMode" value="true"/> <property name="discoverySpi"> <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> <property name="localPort" value="48500"/> <property name="localPortRange" value="20"/> <property name="ipFinder"> <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder"> <property name="addresses"> <list> <value>127.0.0.1:48500..48520</value> </list> </property> </bean> </property> </bean> </property> <property name="communicationSpi"> <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi"> <property name="localPort" value="48100"/> </bean> </property> </bean> </beans>
3) 启动ignite客户端并实现简单数据存取
ClientStartApplication.java
@SpringBootApplication @ImportResource(locations={"classpath:default-config.xml"}) //ignite配置文件路径 public class ClientStartApplication implements CommandLineRunner { @Autowired private IgniteConfiguration igniteCfg; public static void main(String[] args) { SpringApplication.run(ClientStartApplication.class,args); } /**启动完成以后执行初始化*/ @Override public void run(String... strings) { //启动ignite服务 Ignite ignite = Ignition.start(igniteCfg); //建立cache IgniteCache<String, String> cache = ignite.getOrCreateCache("test"); //存入数据 cache.put("cord", "hello"); //查询数据 System.out.format("key[%s]->value[%s]\n", "cord", cache.get("cord")); } }
执行结果以下:
[15:46:44] Ignite node started OK (id=48cfd9ce) [15:46:44] Topology snapshot [ver=30, servers=1, clients=1, CPUs=4, heap=2.7GB] key[cord]->value[hello]
经过ignitevisorcmd.bat
查看当前集群状态与缓存状况:
visor> cache (wrn) <visor>: No caches found. (wrn) <visor>: Type 'help cache' to see how to use this command.
结果发现没有数据,这是由于默认的config\default-config.xml
其实配置是空的,执行ignite.bat
启动服务虽然也是用这个文件,可是由于有默认值,因此不影响,可是监控程序ignitevisorcmd.bat
必需要根据配置文件才能链接访问集群信息,所以按以下所示修改config\default-config.xml
:
(其实就是在上面的default-config.xml
中去掉了<property name="clientMode" value="true"/>
这一项)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> <property name="discoverySpi"> <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> <property name="localPort" value="48500"/> <property name="localPortRange" value="20"/> <property name="ipFinder"> <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder"> <property name="addresses"> <list> <value>127.0.0.1:48500..48520</value> </list> </property> </bean> </property> </bean> </property> <property name="communicationSpi"> <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi"> <property name="localPort" value="48100"/> </bean> </property> </bean> </beans>
再从新启动ignitevisorcmd.bat
并open
修改后的config\default-config.xml
:
执行top
命令,能够看到两个节点的类型是不一样的:
visor> top Hosts: 1 +================================================= | Int./Ext. IPs | Node ID8(@) | Node Type | +================================================= | 0:0:0:0:0:0:0:1 | 1: 875F3FCF(@n0) | Server | | 10.118.144.74 | 2: 48CFD9CE(@n1) | Client | | 127.0.0.1 | | | +-------------------------------------------------
执行cache
命令,能够看到刚代码中建立的名为test
的cache的信息:
visor> cache Time of the snapshot: 08/03/18, 16:20:35 +============================================================== | Name(@) | Mode | Nodes | Entries (Heap / Off-heap) | +============================================================== | test(@c0) | PARTITIONED | 2 | min: 0 (0 / 0) | | | | | avg: 0.50 (0.00 / 0.50) | | | | | max: 1 (0 / 1) | +--------------------------------------------------------------
只需将java项目中的配置文件default-config.xml
中的<property name="clientMode" value="true"/>
改成
<property name="clientMode" value="false"/>
即变为服务节点模式,这样该节点也能够存储数据。
启动以后java服务输出以下:
[00:08:45] Topology snapshot [ver=7, servers=2, clients=0, CPUs=4, heap=2.8GB]
可见servers数量有增长,说明服务节点启动成功,至此ignite简介结束。
完整的示例代码请参考:
https://github.com/cording/ignite-example