做过weblogic集群环境的人应该都清楚,要想实现session同步,必须满足两个条件:第一,在weblogic.xml里面增加session同步相关的代码;第二,所有放入session的类都要序列化。

但是,我终于还是栽了。两个条件明明都满足了,但就是“有些”对象就是不能同步。

我以前使用session是这样的:

这样写完以后,在单机环境下,session中的变量customer的name属性就被更改了。然而在集群环境下,仅仅这样做是不能触发session同步机制的。必须要把customer变量在重新放入session中,即:

session.setAttribute(KEY_REYO, customer);  

后来我从weblogic的售后工程师那里也确认了这一点。一点点经验,和大家分享。

在weblogic的文档中又翻出了以下内容,要是之前认真看了就好了:

Use setAttribute to Change Session State:
In an HTTP servlet that
implements javax.servlet.http.HttpSession, use HttpSession.setAttribute
(which replaces the deprecated putValue) to change attributes in a
session object. If you set attributes in a session object with
setAttribute, the object and its attributes are replicated in a cluster
using in-memory replication. If you use other set methods to change
objects within a session, WebLogic Server does not replicate those
changes. Every time a change is made to an object that is in the
session, setAttribute() should be called to update that object across
the cluster.

下面是补充说明:

在Weblogic中,HttpSession Replication的方式是通过在weblogic.xml中的session- descriptor的定义persistent-store-type来实现的. persistent-store-type可选的属性包括memory, replicated, replicated_if_clustered, async-replicated, async-replicated-if-clustered, file, async-jdbc, jdbc, cookie, coherence-web.

  • memory—Disables persistent session storage.
  • replicated—Same as memory, but session data is replicated across the clustered servers.
  • replicated_if_clustered—If the Web application is deployed on a clustered server, the in-effect persistent-store-type will be replicated. Otherwise, memory is the default.
  • async-replicated—Enables asynchronous session replication in an application or Web application. See "Asynchronous HTTP Session Replication" in Performance and Tuning for Oracle WebLogic Server.
  • async-replicated-if-clustered—Enables asynchronous session replication in an application or Web application when deployed to a cluster environment. If deployed to a single server environment, then the session persistence/replication defaults to in-memory. This allows testing on a single server without deployment errors.
  • file—Uses file-based persistence (See also session-descriptor).
  • async-jdbc—Enables asynchronous JDBC persistence for HTTP sessions in an application or Web application. See Configuring Session Persistence.
  • jdbc—Uses a database to store persistent sessions. (see also session-descriptor).
  • cookie—All session data is stored in a cookie in the user's browser.
  • Coherence*-web For more information, see User's Guide for Oracle Coherence*Web.

Replicated,async-replicated只用部置集群在集群上,而replicated_if_clustered,async-replicated-if-clustered也可以部署在独立实例上。都不能只部署在集群的部分实例中上。

参考:http://docs.oracle.com/cd/E23943_01/web.1111/e13712/weblogic_xml.htm#i1071981

例如:

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

<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">

<session-descriptor>

<!-- <persistent-store-type>replicated</persistent-store-type> -->

<persistent-store-type>replicated_if_clustered</persistent-store-type>

<!--<persistent-store-type>memory</persistent-store-type>   -->

<timeout-secs>60</timeout-secs>

</session-descriptor>

</weblogic-web-app>

  1. Load Blanace和Session Affinity

由于这里的机制是主从备份, 所以集群中只有两个实例会有同一HTTP Session的数据. 当集群里的实例多于2个以上时,为了确保后续的HTTP请求能访问到Session数据, 必须要求前置分发请求的load balancer支持session affinity(sticky session/seamless session). Session Affinity就是能够把特定Session的所有请求都路由到第一次创建Session的同一物理机器上;否则后续的请求就有可能不能够访问 Session数据了.

如果设置成非Replication方式即memory模式, 生成的JSESSIONID类似:

gGMWQy2LcSTHTSyLdyLpqYGskYpXPpRJkc2VB618mSKSQC9rgsCv!-1274119771!1353236040031

可以看出这个session被二个!分隔成三部分。第一部分应该是真正的sessionid, -1274119771是实例标识。而1353236040031为session创建时间。

一旦配置成Replicated模式,Weblogic会生成的SessionID类似:

sHkLQyQTnJQQ217Js7SmQL2x9hBb0JQ5hFm7n4QpNkZL7wMnLbPn!-9326295!959096067!1353236595093

这里出现三个!,第二,三部分为主备实例的标识。

SessionID格式的: sessionid!primary_server_id[!secondary_server_id]!creationTime

2.配置weblogic Load Blanace

配置方式参考: http://guojuanjun.blog.51cto.com/277646/748768

1)       通过http://localhost/Cluster/cluster.jsp访问,页面显示:

session Id:

KSW2QyJFzVcnFxQTWpSLJLhJTTQsCzLGqlM1ShnCvSyKm2r4k29h!-1458785082!2113129367!1353238917906

session CreateTime :1353238917906

current instance :Server1

可以看到该session的primary_server_id为-1458785082,即Server1。(每个server的id是启动时生成的,所以也是变化,所以你的测试可能与我不一样。) secondary_server_id为2113129367,即server3. 即server3是Server1的备点。

2)       停止Server1,再次访问, 页面显示:

session Id:

KSW2QyJFzVcnFxQTWpSLJLhJTTQsCzLGqlM1ShnCvSyKm2r4k29h!2113129367!-481865348!1353238917906

session CreateTime :1353238917906

current instance :Server3

可以看到sessionId没有变化,而该session的primary_server_id为2113129367, 即Server3。secondary_server_id为-481865348,即server0.即Server0是Server3的备点。

3)       停止Server3,再次访问, 页面显示:

session Id:

KSW2QyJFzVcnFxQTWpSLJLhJTTQsCzLGqlM1ShnCvSyKm2r4k29h!-481865348!NONE!1353238917906

session CreateTime :1353238917906

current instance :Server0

可以看到sessionId没有变化,该session的primary_server_id为-481865348, 即Server0。secondary_server_id为NONE,即该session没有备点.

通过测试我们大致可以猜出weblogic session复制的基本思路:

1)       每个实例都有两份Session数据。主数据和备份数据。

2) 当请求的sessionId的primary_server_id为当前实例时,从主数据里获取session响应请求,否则进行3).

3) 当请求的sessionId的secondary_server_id为当前实例时,从备份数据里取session响应请求。并修正该session的primary_server_id/secondary_server_id为自已及其的备点。

3. Weblogic支持的负载均衡

Weblogic支持两种机制的负载均衡

1)       Proxy plug-ins

Weblogic内置插件,即http://guojuanjun.blog.51cto.com/277646/748768中提到的mod_wl.

如果一个实例失败,plug-in会定位该session的secondary_server,将请求发给它。

2)       Hardware load balancers

Hardware load balancers,比如F5. 这些第三方产品并不能按weblogic的意愿,定位session的secondary_server。他会随机选机选择一个可用实例发给他。然后该实例通过session id里的secondary_server_id,像secondary_server获取数据。

虽然weblogic允许这种请求的随机转发,但并不建议使用会话不亲和方式,因为这将带来数据并发和一致性问题。

参考文献:

    1. http://blog.csdn.net/mobicents/article/details/7067957
    2. http://docs.oracle.com/cd/E23943_01/wls.htm
    3. http://stackoverflow.com/questions/6429990/weblogic-jsessionid

weblogic 12C集群环境下的session复制的更多相关文章

  1. 集群环境下,Session管理的几种手段

    集群环境下,Session管理的几种手段 1.Session复制 缺点:集群服务器间需要大量的通信进行Session复制,占用服务器和网络的大量资源. 由于所有用户的Session信息在每台服务器上都 ...

  2. Weblogic 12c 集群环境搭建

    本文是在windows7操作系统下配置的,jdk版本1.7 ,weblogic版本12.1.3.0.0. 搭建集群前的规划 其中AdminServer是总控制端,server1.server2.ser ...

  3. 集群环境下的Session管理

    1. 集群环境下的管理HTTPSSession所遇到的问题 一台服务器对应这个一个session对象,无法在另外一个服务器互通 解决方法: 1. Session 的 Replication(复制)将当 ...

  4. 【原创】Tomcat集群环境下对session进行外部缓存的方法(2)

    Session对象的持久化比较麻烦,虽然有序列化,但是并不确定Session对象中保存的其他信息是否可以序列化,这可能是网上很多解决方案摒弃此种做法的原因,网上的很多做法都是将Session中的att ...

  5. 【原创】Tomcat集群环境下对session进行外部缓存的方法(1)

    BJJC网改版, 计划将应用部署在tomcat集群上,集群的部署方案为Apache+Tomcat6,连接件为mod_jk,其中开启了session复制和粘性session.计划节点数为3个. 到这,或 ...

  6. 集群环境下的Session共享

    一.Cookie机制和Session机制回顾 1)定义:Session成为“会话”,具体是指一个终端用户与交互系统进行通信的时间间隔,通常指从注册进入系统到注销退出系统之间所经过的时间.Session ...

  7. 集群环境下Shiro Session的管理

    问题引入 紧接上篇连接 在多台tomcat集群中,shiro管理的session需要放在Redis中,我们只需要增加redisSessionDAO的配置就行 <!-- 定义会话管理器的操作 表示 ...

  8. redis内存分配管理与集群环境下Session管理

    ##################内存管理############### 1.Redis的内存管理 .与memcache不同,没有实现自己的内存池 .在2..4以前,默认使用标准的内存分配函数(li ...

  9. redis 与java的连接 和集群环境下Session管理

    redis 的安装与设置开机自启(https://www.cnblogs.com/zhulina-917/p/11746993.html)  第一步: a) 搭建环境 引入 jedis jar包 co ...

随机推荐

  1. hdu 6118度度熊的交易计划(费用流)

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. MyBatis Plus + Activiti 整合报错:org.springframework.beans.factory.UnsatisfiedDependencyException

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ind ...

  3. 优雅的将Map转为String工具类

    import com.alibaba.fastjson.JSONObject;import org.apache.commons.lang3.StringUtils; import java.lang ...

  4. 利用transform的bug使fixed相对于父级定位

    首先,大家都清楚,元素使用fixed之后,若不设置top与left则会相对于最近的使用定位的父元素,并位于父元素的原点位置设置top与left值时,则会相对于窗口定位.但无论如何,此时仍相对于窗口定位 ...

  5. Python 项目实践二(生成数据)第二篇

    接着上节继续学习,在本节中,我们将使用Python来生成随机漫步数据,再使用matplotlib以引人瞩目的方式将这些数据呈现出来.随机漫步是这样行走得到的路径:每次行走都完全是随机的,没有明确的方向 ...

  6. 八. Pandas的轴

    axis=0代表跨行(down),而axis=1代表跨列(across) 使用0值表示沿着每一列或行标签\索引值向下执行方法 使用1值表示沿着每一行或者列标签模向执行对应的方法 下图代表在DataFr ...

  7. 3143 二叉树的序遍历codevs

    题目描述 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 输入描述 Input Description 第一行一个整数n,表示这棵树的节点个数. 接下来n行每行2个整数L和R.第i ...

  8. NOI.AC NOIP模拟赛 第六场 游记

    NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...

  9. WorldFinal11 (2/11)

    WorldFinal 11 Trash Removal 题意 给你一个多边形,问这个多边形至少需要多宽的长度,才能把这个多边形放进去. 数据范围100 题解 数据范围只有100,暴力枚举两点,然后算最 ...

  10. Codeforces Round #281 (Div. 2) B. Vasya and Wrestling 水题

    B. Vasya and Wrestling 题目连接: http://codeforces.com/contest/493/problem/B Description Vasya has becom ...