apache ignite系列(一): 簡介
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> 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) |
+--------------------------------------------------------------
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系列(一): 簡介