<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 193,  comments - 520,  trackbacks - 0

    系統要集群,使用SNA方案。
    一、 緩存的處理
    緩存要使用統一的緩存服務器,集中式緩存。
    原先的實現采用ehcache。
    在spring里的配置,以資源緩存為例:

    <!-- EhCache Manager -->
        
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            
    <property name="configLocation">
                
    <value>classpath:ehcache.xml</value>
            
    </property>
    </bean>

    <bean id="resourceCacheBackend"
              class
    ="org.springframework.cache.ehcache.EhCacheFactoryBean">
            
    <property name="cacheManager" ref="cacheManager"/>
            
    <property name="cacheName" value="resourceCache"/>
        
    </bean>

        
    <bean id="resourceCache"
              class
    ="com.framework.extcomponent.security.authentication.services.acegi.cache.EhCacheBasedResourceCache"
              autowire
    ="byName">
            
    <property name="cache" ref="resourceCacheBackend"/>
        
    </bean>

    
    

    cacheManager負責對ehcache進行管理,初始化、啟動、停止。
    resourceCacheBackend負責實際執行緩存操作,put 、get、remove。
    resourceCache實現具有業務語義的業務應用層面的緩存操作,內部調用resourceCacheBackend操作。

    現在采用memcached。
    關于客戶端,采用文初封裝的客戶端,地址在http://code.google.com/p/memcache-client-forjava/
    使用spring的FactoryBean進行二次封裝。同理:
    memcachedManager負責對memcached進行管理,初始化、啟動、停止。
    代碼:

    /**
    * User: ronghao
    * Date: 2008-10-14
    * Time: 10:36:30
    * 管理Memcached 的CacheManager
    */
    public class MemcachedCacheManagerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {

        
    protected final Log logger = LogFactory.getLog(getClass());

        
    private ICacheManager<IMemcachedCache> cacheManager;

        
    public Object getObject() throws Exception {
            
    return cacheManager;
        }

        
    public Class getObjectType() {
            
    return this.cacheManager.getClass();
        }

        
    public boolean isSingleton() {
            
    return true;
        }

        
    public void afterPropertiesSet() throws Exception {
            logger.info(
    "Initializing Memcached CacheManager");
            cacheManager 
    = CacheUtil.getCacheManager(IMemcachedCache.class,
                    MemcachedCacheManager.
    class.getName());
            cacheManager.start();
        }

        
    public void destroy() throws Exception {
            logger.info(
    "Shutting down Memcached CacheManager");
            cacheManager.stop();
        }
    }
     

    配置:


    <bean id="memcachedManager"
              class
    ="com.framework.extcomponent.cache.MemcachedCacheManagerFactoryBean"/>
    
     
    

    resourceCacheBackend負責實際執行緩存操作,put 、get、remove。
    代碼:

    /**
    * User: ronghao
    * Date: 2008-10-14
    * Time: 10:37:16
    * 返回  MemcachedCache
    */
    public class MemcachedCacheFactoryBean implements FactoryBean, BeanNameAware, InitializingBean {

        
    protected final Log logger = LogFactory.getLog(getClass());

        
    private ICacheManager<IMemcachedCache> cacheManager;
        
    private String cacheName;
        
    private String beanName;
        
    private IMemcachedCache cache;

        
    public void setCacheManager(ICacheManager<IMemcachedCache> cacheManager) {
            
    this.cacheManager = cacheManager;
        }

        
    public void setCacheName(String cacheName) {
            
    this.cacheName = cacheName;
        }

        
    public Object getObject() throws Exception {
            
    return cache;
        }

        
    public Class getObjectType() {
            
    return this.cache.getClass();
        }

        
    public boolean isSingleton() {
            
    return true
        }

        
    public void setBeanName(String name) {
            
    this.beanName=name;
        }

        
    public void afterPropertiesSet() throws Exception {
            
    // If no cache name given, use bean name as cache name.
           if (this.cacheName == null) {
            
    this.cacheName = this.beanName;
        }
            cache 
    = cacheManager.getCache(cacheName);
        }
    }

    配置:

    <bean id="resourceCacheBackend"
              class
    ="com.framework.extcomponent.cache.MemcachedCacheFactoryBean">
            
    <property name="cacheManager" ref="memcachedManager"/>
            
    <property name="cacheName" value="memcache"/>
        
    </bean>
      resourceCache同上,替換新的實現類MemcachedBasedResourceCache即可。

    二、 Session失效的處理
    采用memcached作為httpsession的存儲,并不直接保存httpsession對象,自定義SessionMap,SessionMap直接繼承HashMap,保存SessionMap。

    會話膠粘:未失敗轉發的情況下沒必要在memcached保存的SessionMap和httpsession之間復制來復制去,眉來眼去。

    利用memcached計數器保存在線人數。

    系統權限采用了acegi,在acegi的攔截器鏈里配置snaFilter

    <bean id="filterChainProxy"
              class
    ="org.acegisecurity.util.FilterChainProxy">
            
    <property name="filterInvocationDefinitionSource">
                
    <value>
                    CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                    PATTERN_TYPE_APACHE_ANT
                    /**=snaFilter,httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,basicProcessingFilter,securityContextHolderAwareRequestFilter,exceptionTranslationFilter,filterInvocationInterceptor
                
    </value>
            
    </property>
    </bean>


    注意需要配置在第一個。
    snaFilter的職責:
    1、 沒有HttpSession時,創建HttpSession;
    2、 創建Cookie保存HttpSession id;
    3、 如果Cookie保存的HttpSession id與當前HttpSession id一致,說明是正常請求;
    4、 如果Cookie保存的HttpSession id與當前HttpSession id不一致,說明是失敗轉發;失敗轉發的處理:
         4.1、根據Cookie保存的HttpSession id從memcached獲取SessionMap;
         4.2、SessionMap屬性復制到當前HttpSession;
         4.3、memcached刪除SessionMap。
    5、 判斷當前請求url是否是登出url,是則刪除SessionMap,在線人數減1.

    代碼:

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                             FilterChain filterChain) 
    throws IOException, ServletException {
            
    final HttpServletRequest hrequest = (HttpServletRequest) servletRequest;
            
    final HttpServletResponse hresponse = (HttpServletResponse) servletResponse;
            String uri 
    = hrequest.getRequestURI();
            logger.debug(
    "開始SNA攔截-----------------" + uri);
            HttpSession httpSession 
    = hrequest.getSession();
            String sessionId 
    = httpSession.getId();
            
    //如果是登出,則直接干掉sessionMap
            if (uri.equals(logoutUrl)) {
                logger.debug(
    "remove sessionmap:" + sessionId);
                
    //在線人數減1
                getCache().addOrDecr("userCount",1);
                getCache().remove(sessionId);
            } 
    else {
                String cookiesessionid 
    = getSessionIdFromCookie(hrequest, hresponse);
                
    if (!sessionId.equals(cookiesessionid)) {
                    createCookie(sessionId, hresponse);
                    SessionMap sessionMap 
    = getSessionMap(cookiesessionid);
                    
    if (sessionMap != null) {
                        logger.debug(
    "fail over--------sessionid:" + sessionId + "cookiesessionid:" + cookiesessionid);
                        initialHttpSession(sessionMap, httpSession);
                        cache.remove(cookiesessionid);
                    }
                }
            }
            filterChain.doFilter(hrequest, hresponse);
    }

    利用HttpSessionAttributeListener監聽httpsession的屬性變化,同步到memecached中的sessionmap。
    public void attributeAdded(HttpSessionBindingEvent event) {
            HttpSession httpSession 
    = event.getSession();
            String attrName 
    = event.getName();
            Object attrValue 
    = event.getValue();
            String sessionId 
    = httpSession.getId();
            logger.debug(
    "attributeAdded sessionId:" + sessionId + "name:" + attrName + ",value:" + attrValue);
            SessionMap sessionMap 
    = getSessionMap(sessionId);
            
    if (sessionMap == null){
                
    //在線人數加1
                getCache().addOrIncr("userCount",1);
                sessionMap 
    = new SessionMap();
            }
            logger.debug(
    "name:" + attrName + ",value:" + attrValue);
            sessionMap.put(attrName, attrValue);
            getCache().put(sessionId, sessionMap);
        }

        
    public void attributeRemoved(HttpSessionBindingEvent event) {
            HttpSession httpSession 
    = event.getSession();

            String attrName 
    = event.getName();
            String sessionId 
    = httpSession.getId();
            logger.debug(
    "attributeRemoved sessionId:" + sessionId + "name:" + attrName);
            SessionMap sessionMap 
    = getSessionMap(sessionId);
            
    if (sessionMap != null) {
                logger.debug(
    "remove:" + attrName);
                sessionMap.remove(attrName);
                getCache().put(sessionId, sessionMap);
            }
        }

        
    public void attributeReplaced(HttpSessionBindingEvent event) {
            attributeAdded(event);
        }
    
     
    利用HttpSessionListener,sessionDestroyed事件時根據sessionid刪除memcached里的sessionMap(如果存在)。不再擔心httpsession的過期問題。
    
    public void sessionDestroyed(HttpSessionEvent event) {
            HttpSession httpSession 
    = event.getSession();
            String sessionId 
    = httpSession.getId();
            logger.debug(
    "session Removed sessionId:" + sessionId);
            SessionMap sessionMap 
    = getSessionMap(sessionId);
            
    if (sessionMap != null) {
                logger.debug(
    "remove sessionmap:" + sessionId);
                
    //在線人數減1
                getCache().addOrDecr("userCount",1);
                getCache().remove(sessionId);
            }
        }

      三、 文件保存的處理

    和緩存類似,采用集中式的文件服務。對于linux,采用nfs。參考文檔http://linux.vbird.org/linux_server/0330nfs.php#What_NFS_perm。關鍵在于對權限的分配。
    應用程序本身不用修改。




    http://www.tkk7.com/ronghao 榮浩原創,轉載請注明出處:)
    posted on 2008-10-28 20:41 ronghao 閱讀(2504) 評論(3)  編輯  收藏 所屬分類: 工作日志

    FeedBack:
    # re: 基于memcached的SNA實現
    2008-10-29 09:15 | 岑文初
    ^_^,不錯  回復  更多評論
      
    # re: 基于memcached的SNA實現
    2008-10-29 09:18 | 岑文初
    對了,最近修復了幾個邊界問題,最新版本為2.4  回復  更多評論
      
    # re: 基于memcached的SNA實現[未登錄]
    2008-10-30 09:11 | ronghao
    @岑文初
    呵呵,謝謝。要多向你學習:)  回復  更多評論
      
    <2008年10月>
    2829301234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    關注工作流和企業業務流程改進。現就職于ThoughtWorks。新浪微博:http://weibo.com/ronghao100

    常用鏈接

    留言簿(38)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    常去的網站

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲人成在线播放| 在线播放免费播放av片| 综合偷自拍亚洲乱中文字幕| 久久精品国产亚洲香蕉| xvideos亚洲永久网址| 成人au免费视频影院| 亚洲免费在线观看视频| 免费网站看av片| 国产精品视频全国免费观看| 亚洲精品色在线网站| 亚洲精品二三区伊人久久| 色婷婷六月亚洲婷婷丁香| 国产午夜亚洲精品午夜鲁丝片| 日韩视频在线免费| 成人超污免费网站在线看| 亚洲最大免费视频网| 久久国产精品萌白酱免费| 9久热这里只有精品免费| 日韩一级片免费观看| 国产亚洲精品精品精品| 亚洲AV成人片无码网站| 亚洲欧美黑人猛交群| 亚洲一区二区三区丝袜| 久久亚洲国产成人影院| 久久精品国产亚洲av麻豆蜜芽| 亚洲性69影院在线观看| 亚洲精品福利在线观看| 亚洲国产精品热久久| 久久久久亚洲AV无码专区首| 亚洲AV日韩AV鸥美在线观看| 亚洲AV无码不卡在线播放| 亚洲人成电影在线天堂| 67pao强力打造67194在线午夜亚洲 | 免费无码作爱视频| 国精产品一区一区三区免费视频| 久久久久久噜噜精品免费直播 | 国产aa免费视频| mm1313亚洲精品无码又大又粗| 哒哒哒免费视频观看在线www| 人人狠狠综合久久亚洲高清| 亚洲精品黄色视频在线观看免费资源 |