apache-ignite简介(一)
1,简介
? ignite是分布式内存网格的一种实现,其基于java平台,具有可持久化,分布式事务,分布式计算等特点,此外还支持丰富的键值存储以及SQL语法(基于h2引擎),可以看成是一个分布式内存数据库。
与ignite类似的产品有gemfire(12306目前正在使用),其开源版为geode。与gemfire相比,ignite对sql的支持比较完善,提供了数据并置来提升性能,还有对分布式事物的支持以及对spring的集成都比较友好,很方便进行嵌入式集成进应用服务。
2,基本使用
? ignite有两种使用方式: 一种是从官网下载release版本程序,解压运行部署,另外一种是通过嵌入式集成进现有应用程序。
2.1,官网二进制release版本的使用
下载地址:https://ignite.apache.org/download.cgi
下载后得到apache-ignite-fabric-2.3.0-bin.zip
压缩包,解压后进入bin路径:
主要用到两个脚本: ignite.bat
启动脚本, ignitevisorcmd.bat
监控脚本
执行ignite.bat
脚本即可启动一个ignite服务
执行ignitevisorcmd.bat
可以进入监控命令界面:
输入open
命令选择配置文件,这里选择默认的0 ?| config\default-config.xml
输入数字0
即可
常用命令如下:
命令 | 功能 |
---|---|
top | 查看集群网络拓扑图 |
cache | 查看整体缓存情况 |
config | 查看节点配置 |
open | 打开一个配置文件连接集群 |
close | 关闭该连接 |
更多详细命令可以通过输入help命令查看命令帮助(输入help回车)。
2.2,java服务使用ignite客户端访问ignite集群
? 通过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> topHosts: 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> cacheTime 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) ???????????|+--------------------------------------------------------------
2.3, java服务集成ignite作为服务节点
只需将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
apache ignite系列(一): 简介
原文地址:https://www.cnblogs.com/cord/p/9397616.html