ignite的Web Session會話集羣化

我參考文檔弄了下,今天記錄下過程備忘。html

https://ignite.apache.org/java

https://apacheignite.readme.io/docs/web-session-clusteringnode

一、添加Ignite的jar包web

個人基於Maven的工程,應用的pom.xml 主要包含下面幾個jar 包,因爲我是基於zookeeper 來發現集羣的,因此還用了ignite-zookeeperspring

  • ignite.jar
  • ignite-web.jar
  • ignite-log4j.jar
  • ignite-spring.jar
  • ignite-zookeeper.jar
<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-core</artifactId>
    <version> ${ignite.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-web</artifactId>
    <version> ${ignite.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-log4j</artifactId>
    <version>${ignite.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-spring</artifactId>
    <version>${ignite.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-zookeeper</artifactId>
    <version>${ignite.version}</version>
</dependency>

web.xml 中主要是配置ignite 的監聽, 主要聲明一個ContextListener和一個WebSessionsFilter 。chrome

<listener>
  <listener-class>org.apache.ignite.startup.servlet.ServletContextListenerStartup</listener-class>
</listener>
<filter>
  <filter-name>IgniteWebSessionsFilter</filter-name>
  <filter-class>org.apache.ignite.cache.websession.WebSessionFilter</filter-class>
</filter>
<!-- You can also specify a custom URL pattern. -->
<filter-mapping>
  <filter-name>IgniteWebSessionsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Specify Ignite configuration (relative to META-INF folder or Ignite_HOME). -->
<context-param>
  <param-name>IgniteConfigurationFilePath</param-name>
  <param-value>config/default-config.xml </param-value>
</context-param>
<!-- Specify the name of Ignite cache for web sessions. -->
<context-param>
  <param-name>IgniteWebSessionsCacheName</param-name>
  <param-value>partitioned</param-value>
</context-param>

打開 WebSessionFilter源碼,找到init方法,咱們發現IgniteWebSessionsCacheName參數的設置分別在ServletConfigServletContext調用了getInitParameter方法進行獲取(U.firstNotNull內部邏輯是取第一個不爲空的爲準)。express

這兩個方法的都能從web.xml中獲取參數 ,因此無論你在全局仍是 WebSessionFilter裏配置參數都是能夠的,我這裏參考官方提供的在servletContext裏配置。固然,咱們能夠嘗試apache

<filter>
  <filter-name>IgniteWebSessionsFilter</filter-name>
  <filter-class>org.apache.ignite.cache.websession.WebSessionFilter</filter-class>
  <init-param>
      <param-name>IgniteWebSessionsCacheName</param-name>
      <param-value>partitioned</param-value>
  </init-param> 
</filter>

 

Ignite配置文件的路徑(相對於META-INF文件夾或者IGNITE_HOME) ,我這裏基於META-INF ,並在該目錄下建立了config目錄,而後新建default-config.xml 文件,因爲基於zookeeper(能夠參考https://www.zybuluo.com/liyuj/note/482684#259%E5%9F%BA%E4%BA%8Ezookeeper%E7%9A%84%E5%8F%91%E7%8E%B0),配置內容以下。api

<?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>
        <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. -->
        <property name="discoverySpi">
            <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
                <property name="ipFinder">
                    <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.zk.TcpDiscoveryZookeeperIpFinder">
                        <property name="zkConnectionString" value="127.0.0.1:2181"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

我本地啓動了個zookeeper-3.3.6 來測試,按默認的用2181端口。tomcat

Ignite 官方測試過的servlet 容器有:

  • Apache Tomcat 7
  • Eclipse Jetty 9
  • Apache Tomcat 6
  • Oracle WebLogic >= 10.3.4

我用的是tomcat 6,用的測試文件以下:

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html>
<head>
    <title>Cluster App Test</title>
</head>
<body>
Server Info: <%  out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
    out.println("<br> ID " + session.getId()+"<br>");   // 若是有新的 Session 屬性設置
    String dataName = request.getParameter("dataName");
    if (dataName != null && dataName.length() > 0) {
        String dataValue = request.getParameter("dataValue");
        session.setAttribute(dataName, dataValue);
    }
    out.print("<b>Session 列表</b>");
    Enumeration e = session.getAttributeNames();
    while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        String value = session.getAttribute(name).toString();
        out.println( name + " = " + value+"<br>");
        System.out.println( name + " = " + value);
    }
%>
<form action="index.jsp" method="POST">
    名稱:<input type=text size=20 name="dataName"> <br>
    值:<input type=text size=20 name="dataValue"> <br>
    <input type=submit>
</form>
</body>
</html>

在chrome 打開兩個服務器

http://localhost:8080/techpark/index.jsp 給session設置 name1 val1

http://localhost:8082/techpark/index.jsp 給session設置 name2 val2

刷新界面,能夠看到內容是同步的,也就說明會話實現了共享。

若是你還想觀察session的變化,能夠使用 HttpSessionListener  和 HttpSessionAttributeListener 作監聽。

參數文檔

https://apacheignite.readme.io/docs/web-session-clustering

https://www.zybuluo.com/liyuj/note/486177#318web%E4%BC%9A%E8%AF%9D%E9%9B%86%E7%BE%A4%E5%8C%96 

https://www.oschina.net/question/2861257_2203943

http://www.cnblogs.com/javawebsoa/archive/2013/07/31/3228858.html

http://momoxiaoxiong.iteye.com/blog/1214238  

相關文章
相關標籤/搜索