文章出處

Apache Ignite初步認識

今年4月開始倒騰openfire,過程中經歷了許多,更學到了許多。特別是在集群方面有了很多的認識,真正開始認識到集群的概念及應用方法。

在openfire中使用的集群解決方案是代理+分布式內存。所謂代理便是通過一個入口轉發請求到多個服務實例。而分布式內存就是解決服務實例間數據共享問題。通過這兩步就可以搭建出一套水平擴展的集群系統。

openfire使用的分布式內存計算框架是hazelcast,并不了解它,大概只知道它是分布式網格內存計算框架。聽許多openfire開發者都吐槽hazelcast有許多問題,集群效果上不太好,也因此意外間的發現了Ignite。

Ignite是apache基金的一個開源項目,功能與hazelcast非常類似:

Apache Ignite內存數據組織是高性能的、集成化的以及分布式的內存平臺,他可以實時地在大數據集中執行事務和計算,和傳統的基于磁盤或者閃存的技術相比,性能有數量級的提升。

image

特性:
可以將Ignite視為一個獨立的、易于集成的內存組件的集合,目的是改進應用程序的性能和可擴展性,部分組件包括:

  • 高級的集群化
  • 數據網格(JCache)
  • 流計算和CEP
  • 計算網格
  • 服務網格
  • Ignite文件系統
  • 分布式數據結構
  • 分布式消息
  • 分布式事件
  • Hadoop加速器
  • Spark共享RDD

已經有國內的大神做了翻譯,可以看看這個鏈接:https://www.zybuluo.com/liyuj/note/481591
這里面有Ignite的手冊,介紹的還是比較清楚的。

啟動一個Ignite吧

只要少量的代碼我們就可以將Ignite應用到自己的系統中,比如我需要做一個緩存。

  • 在工程中通過Maven引入
    Ignte的最小引入包就是一個ignite-core.jar包依賴
<dependency>
      <groupId>org.apache.ignite</groupId>
      <artifactId>ignite-core</artifactId>
      <version>${ignite.version}</version>
    </dependency>

 

這樣就行啦。。

  • 啟動Ignite創建并使用緩存
    我們需要創建一個緩存,那么如何整呢?
Ignite ignite = Ignition.start();

這一句代碼就啟動了一個Ignite節點,整個過程不需要配置就這么簡單的跑起來了。

好了,再創建一個緩存用來存用戶的ID和姓名:

IgniteCache<Integer, String> cache = ignite.getOrCreateCache("userInfo");
cache.put(1, "小明");

這樣就over了,是不是感覺和使用hashmap差不多?但重要的是什么,如果有另外一個ignite節點起來了,它們會自動發現并組成集群,那么userInfo這個緩存就會自動的完成分布式存儲咯。

只不過有點問題,默認情況下緩存模式是分區模式,當然分區模式下需要設置緩存的備份數量backups,如果不設置的話緩存并不會在其他節點上做備份。

什么意思呢?就是說系統中有一個節點node1,這時候存了userInfo,此時node2啟動了,并且自動發現后node1和node2建立了集群,不過node1突然掛了,此時系統會訪問node2的數據,結果就失敗啦。也就是說默認配置下數據是不會自帶分布式存儲的。需要做一下緩存的配置才行。

可以在創建緩存的時候指定一下緩存的配置:

CacheConfiguration cfg = new CacheConfiguration();
cfg.setCacheMode(CacheMode.PARTITIONED);
cfg.setBackups(1);
cfg.setName("userInfo");
IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cfg);
cache.put(1, "小明");

 

Ignite提供了三種不同的緩存操作模式,分區、復制和本地。可以參考這里的介紹:https://www.zybuluo.com/liyuj/note/393469#33緩存模式

Ignite配置Tomcat WebSession練練手

我最開始拿Ignite的用處主要是做緩存使用,而且其支持JCache的特性和集群化做緩存非常合適,特別是未來部署成分布式也很平滑。但是目前我使用的還比較淺,估計入門都不算,這也可見Ignite使用多么簡單,基本上看看手冊就可以上手了。

這些天我還是想嘗試一下Ignite的WebSession的集群功能,為以后Web系統集群做一個基礎。之前的使用Redis的方案總覺得不是特別爽,雖然對代碼的侵入性低,但不是java系列的。目前Ignite官方給出了WebSession集群的指南:https://www.zybuluo.com/liyuj/note/393469#318web會話集群化

我根據這個指南做了嘗試,效果基本達到,但是對于像我這樣的初學者面對這個手冊會遇到一些問題。首先在配置后啟動系統后發現會報一個異常:

嚴重: Exception starting filter IgniteWebSessionsFilter
class org.apache.ignite.IgniteException: Cache for web sessions is not started (is it configured?): partitioned
    at org.apache.ignite.cache.websession.WebSessionFilter.initCache(WebSessionFilter.java:336)
    at org.apache.ignite.cache.websession.WebSessionFilter.init(WebSessionFilter.java:292)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4854)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5546)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

 

這個問題我查了蠻久,后來還是@李玉玨給了指導解決了。問題的原因是WebSessionFilter初始化時找不到對應的緩存。我查看了代碼:

@SuppressWarnings("unchecked")
    void initCache() {
        cache = webSesIgnite.cache(cacheName);
        binaryCache = webSesIgnite.cache(cacheName);

        if (cache == null)
            throw new IgniteException("Cache for web sessions is not started (is it configured?): " + cacheName);

 

報錯的代碼就是因為cache為null導致的。這里比較重要的是

cache = webSesIgnite.cache(cacheName);

在前面舉的例子中我們獲取一個緩存是用getOrCreateCache方法,這個方法會在緩存不存在的情況下自動創建一個緩存,但是cache方法卻不會。所以要解決這個問題就是要在Ignite啟動后先把緩存創建好。

但是整個過程我們并沒有手動顯式的去啟動Ignite,是WebSessionFilter在Init的時候獲取的,所以我們可以通過配置的方式將緩存首先創建。

如何指定Ignite XML文件加載?
需要創建一個XML文件,并將其放在Web目錄的META-INF目錄下面,比如創建一個default-config.xml文件,文件內容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<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 class="org.apache.ignite.configuration.IgniteConfiguration" >
        <property name="cacheConfiguration">
            <bean class="org.apache.ignite.configuration.CacheConfiguration">
                     <!-- Cache name. -->
                     <property name="name" value="partitioned"/>
                     <property name="cacheMode" value="PARTITIONED"/>
               <property name="backups" value="1"/>
                 </bean>
        </property>
    </bean>
</beans>

這里的配置意思是創建一個name為partitioned的緩存,使用分區模式,備份數為1.再次啟動web程序就可以啦。

結合Spring方式

對于使用Spring的應用是可以集成Ignite緩存的,配置方式需要通過一個緩存抽象類來完成org.apache.ignite.cache.spring.SpringCacheManager。

在自己項目的Spring里做如下配置:

<!-- Provide configuration bean. -->
  <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager">
      <property name="configuration">
          <bean class="org.apache.ignite.configuration.IgniteConfiguration">
            <property name="cacheConfiguration">
                  <bean class="org.apache.ignite.configuration.CacheConfiguration">
                       <!-- Cache name. -->
                       <property name="name" value="partitioned"/>
                       <property name="cacheMode" value="PARTITIONED"/>
                       <property name="backups" value="1"/>
                   </bean>
                </property>
          </bean>
      </property>
  </bean>

 

總結

可以看到Ignite的使用還是非常簡單的,特別是其配置的簡單性,很容易上手,輕松就搭建了一套分布式內存系統。而且對于Ignite來說還有更多的高級特性,參考"特性"部分。接下來還需要再繼續深入了解。

 另外對于作為緩存使用我在j2cache開源項目里有簡單的集成,代碼可以看:https://github.com/mini188/j2cache

注:此文章為原創,歡迎轉載,請在文章頁面明顯位置給出此文鏈接!
若您覺得這篇文章還不錯請點擊下右下角的推薦,非常感謝!
http://www.cnblogs.com/5207

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 AutoPoster 的頭像
    AutoPoster

    互聯網 - 大數據

    AutoPoster 發表在 痞客邦 留言(0) 人氣()