apache ignite系列(一): 簡介

apache-ignite簡介(一)

1,簡介

​ ignite是分佈式內存網格的一種實現,其基於java平臺,具備可持久化,分佈式事務,分佈式計算等特色,此外還支持豐富的鍵值存儲以及SQL語法(基於h2引擎),能夠當作是一個分佈式內存數據庫。java

與ignite相似的產品有gemfire(12306目前正在使用),其開源版爲geode。與gemfire相比,ignite對sql的支持比較完善,提供了數據並置來提高性能,還有對分佈式事物的支持以及對spring的集成都比較友好,很方便進行嵌入式集成進應用服務。node

2,基本使用

​ ignite有兩種使用方式: 一種是從官網下載release版本程序,解壓運行部署,另一種是經過嵌入式集成進現有應用程序。git

2.1,官網二進制release版本的使用

下載地址: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回車)。

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.batopen修改後的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

相關文章
相關標籤/搜索