/hazelcast-documentation/src/main/docbook/manual/content/HttpSessionClustering.xml

https://bitbucket.org/gabral6_gmailcom/hazelcast · XML · 237 lines · 156 code · 8 blank · 73 comment · 0 complexity · 1819c663ec8f318c41837ab6b01e9b7f MD5 · raw file

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!--
  3. ~ Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  4. ~
  5. ~ Licensed under the Apache License, Version 2.0 (the "License");
  6. ~ you may not use this file except in compliance with the License.
  7. ~ You may obtain a copy of the License at
  8. ~
  9. ~ http://www.apache.org/licenses/LICENSE-2.0
  10. ~
  11. ~ Unless required by applicable law or agreed to in writing, software
  12. ~ distributed under the License is distributed on an "AS IS" BASIS,
  13. ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ~ See the License for the specific language governing permissions and
  15. ~ limitations under the License.
  16. -->
  17. <simplesect version='5.0' xmlns='http://docbook.org/ns/docbook'
  18. xmlns:xi="http://www.w3.org/2001/XInclude"
  19. xmlns:xlink="http://www.w3.org/1999/xlink"
  20. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd
  22. http://www.w3.org/1999/xlink http://www.w3.org/1999/xlink.xsd">
  23. <para>
  24. Say you have more than one web servers (A, B, C) with a load balancer in front of them. If server A goes down
  25. then your users on that server will be directed to one of the live servers (B or C) but their sessions will be lost!
  26. So we have to have all these sessions backed up somewhere if we don't want to lose the sessions upon server crashes.
  27. Hazelcast WM allows you to cluster user http sessions automatically. The following are required for enabling
  28. Hazelcast Session Clustering:
  29. <itemizedlist>
  30. <listitem>
  31. <para>Target application or web server should support Java 1.5+
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>Target application or web server should support Servlet 2.4+ spec
  36. </para>
  37. </listitem>
  38. <listitem>
  39. <para>Session objects that needs to be clustered have to be Serializable
  40. </para>
  41. </listitem>
  42. </itemizedlist>
  43. Here are the steps to setup Hazelcast Session Clustering:
  44. <orderedlist>
  45. <listitem>
  46. <para>Put the
  47. <literal>hazelcast</literal>
  48. and
  49. <literal>hazelcast-wm</literal>
  50. jars in your
  51. <literal>WEB-INF/lib</literal>
  52. directory. Optionally if you wish to connect to a cluster as a client add
  53. <literal>hazelcast-client</literal>
  54. as well.
  55. </para>
  56. </listitem>
  57. <listitem>
  58. <para>Put the following xml into
  59. <literal>web.xml</literal>
  60. file. Make sure Hazelcast filter is placed
  61. before all the other filters if any; put it at the top for example.
  62. </para>
  63. <para>
  64. <programlisting language="xml"><![CDATA[
  65. <filter>
  66. <filter-name>hazelcast-filter</filter-name>
  67. <filter-class>com.hazelcast.web.WebFilter</filter-class>
  68. <!--
  69. Name of the distributed map storing
  70. your web session objects
  71. -->
  72. <init-param>
  73. <param-name>map-name</param-name>
  74. <param-value>my-sessions</param-value>
  75. </init-param>
  76. <!--
  77. How is your load-balancer configured?
  78. stick-session means all requests of a session
  79. is routed to the node where the session is first created.
  80. This is excellent for performance.
  81. If sticky-session is set to false, when a session is updated
  82. on a node, entry for this session on all other nodes is invalidated.
  83. You have to know how your load-balancer is configured before
  84. setting this parameter. Default is true.
  85. -->
  86. <init-param>
  87. <param-name>sticky-session</param-name>
  88. <param-value>true</param-value>
  89. </init-param>
  90. <!--
  91. Name of session id cookie
  92. -->
  93. <init-param>
  94. <param-name>cookie-name</param-name>
  95. <param-value>hazelcast.sessionId</param-value>
  96. </init-param>
  97. <!--
  98. Domain of session id cookie. Default is based on incoming request.
  99. -->
  100. <init-param>
  101. <param-name>cookie-domain</param-name>
  102. <param-value>.mywebsite.com</param-value>
  103. </init-param>
  104. <!--
  105. Should cookie only be sent using a secure protocol? Default is false.
  106. -->
  107. <init-param>
  108. <param-name>cookie-secure</param-name>
  109. <param-value>false</param-value>
  110. </init-param>
  111. <!--
  112. Should HttpOnly attribute be set on cookie ? Default is false.
  113. -->
  114. <init-param>
  115. <param-name>cookie-http-only</param-name>
  116. <param-value>false</param-value>
  117. </init-param>
  118. <!--
  119. Are you debugging? Default is false.
  120. -->
  121. <init-param>
  122. <param-name>debug</param-name>
  123. <param-value>true</param-value>
  124. </init-param>
  125. <!--
  126. Configuration xml location;
  127. * as servlet resource OR
  128. * as classpath resource OR
  129. * as URL
  130. Default is one of hazelcast-default.xml
  131. or hazelcast.xml in classpath.
  132. -->
  133. <init-param>
  134. <param-name>config-location</param-name>
  135. <param-value>/WEB-INF/hazelcast.xml</param-value>
  136. </init-param>
  137. <!--
  138. Do you want to use an existing HazelcastInstance?
  139. Default is null.
  140. -->
  141. <init-param>
  142. <param-name>instance-name</param-name>
  143. <param-value>default</param-value>
  144. </init-param>
  145. ]]></programlisting>
  146. <!-- Splitting xml here, otherwise it won't into pdf page. -->
  147. <programlisting language="xml"><![CDATA[
  148. <!--
  149. Do you want to connect as a client to an existing cluster?
  150. Default is false.
  151. -->
  152. <init-param>
  153. <param-name>use-client</param-name>
  154. <param-value>false</param-value>
  155. </init-param>
  156. <!--
  157. Client configuration location;
  158. * as servlet resource OR
  159. * as classpath resource OR
  160. * as URL
  161. Default is null.
  162. -->
  163. <init-param>
  164. <param-name>client-config-location</param-name>
  165. <param-value>/WEB-INF/hazelcast-client.properties</param-value>
  166. </init-param>
  167. <!--
  168. Do you want to shutdown HazelcastInstance during
  169. web application undeploy process?
  170. Default is true.
  171. -->
  172. <init-param>
  173. <param-name>shutdown-on-destroy</param-name>
  174. <param-value>true</param-value>
  175. </init-param>
  176. </filter>
  177. <filter-mapping>
  178. <filter-name>hazelcast-filter</filter-name>
  179. <url-pattern>/*</url-pattern>
  180. <dispatcher>FORWARD</dispatcher>
  181. <dispatcher>INCLUDE</dispatcher>
  182. <dispatcher>REQUEST</dispatcher>
  183. </filter-mapping>
  184. <listener>
  185. <listener-class>com.hazelcast.web.SessionListener</listener-class>
  186. </listener>
  187. ]]></programlisting>
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>Package and deploy your war file as you would normally do.
  192. </para>
  193. </listitem>
  194. </orderedlist>
  195. It is that easy! All http requests will go through Hazelcast
  196. <literal>WebFilter</literal>
  197. and it will put the
  198. session objects into Hazelcast distributed map if needed.
  199. </para>
  200. <para>
  201. <emphasis role="bold">Info about sticky-sessions:</emphasis>
  202. </para>
  203. <para>
  204. Hazelcast holds whole session attributes in a distributed map and in local http session. Local session is required
  205. for fast access to data and distributed map is needed for fail-safety.
  206. <itemizedlist>
  207. <listitem>
  208. <para>
  209. <emphasis role="italic">If sticky-session is not used, whenever a session a attribute
  210. is updated in a node (in both node local session and clustered cache),
  211. that attribute should be invalidated in all other nodes' local sessions,
  212. because now they have dirty value. So when a request arrives one of those other nodes
  213. that attribute value is fetched from clustered cache.</emphasis>
  214. </para>
  215. </listitem>
  216. <listitem>
  217. <para>
  218. <emphasis role="italic">To overcome performance penalty of sending invalidation messages during updates,
  219. sticky-sessions can be used.
  220. If Hazelcast knows sessions are sticky, invalidation will not be send, because Hazelcast assumes there is
  221. no other local session at the moment. When a server is down, requests belonging to a session hold
  222. in that server will routed to other one and that server will fetch session data from clustered cache.
  223. That means using sticky-sessions, one will not suffer performance penalty of accessing clustered data
  224. and can benefit recover from a server failure.</emphasis>
  225. </para>
  226. </listitem>
  227. </itemizedlist>
  228. </para>
  229. </simplesect>