我参考文档弄了下,今天记录下过程备忘。html
https://ignite.apache.org/java
https://apacheignite.readme.io/docs/web-session-clusteringnode
一、添加Ignite的jar包web
个人基于Maven的工程,应用的pom.xml 主要包含下面几个jar 包,因为我是基于zookeeper 来发现集群的,因此还用了ignite-zookeeperspring
<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参数的设置分别在ServletConfig和ServletContext调用了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 容器有:
我用的是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