Springsession+redis實現session共享

huxd發表於2017-10-26

Springsession+redis實現session共享
為何突然寫這個文章,因為我花費了一天的時間才把它配置好,其中遇到的坑,是到目前為止遇到最多的,有人在想現在網上這麼多的教程,為啥你這個小菜雞博主要寫這個,因為,原專案中用的快取用的是memcached,有人說,memcached+tomcat也可以實現快取共享呀!有能耐,你把這個包給我,我是基本上下遍了CSDN上面的包,沒一個能用的,有人說maven,對,我也下了一遍,最後我放棄了。對就是這麼任性的放棄了。老子要搞redis,老子要搞兩個快取,我就要高大上的!(心裡話:小菜雞搞不定了,認慫了,只能這麼搞了!)。

你想輸入的替代文字
你想輸入的替代文字

首先:準備工具,redis+jar包,包括了windows版本的redis以及連線redis工具
地址:download.csdn.net/download/li…
準備好了:上乾貨
首先:你要在你的web.xml中寫這些,攔截你的請求,儲存你的session

<!-- 配置session共享 注意位置:網上一大堆!自己去看 www.baidu.com 記得搜尋-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>這裡換成你的配置檔案的地址,別就複製過去,啥也不看。</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>複製程式碼

主要的來了,你要配置一個redis.xml檔案的名字自己起,最好具有代表性,比如我的。

你想輸入的替代文字
你想輸入的替代文字

這樣就清晰了!然後,配置檔案中寫,直接複製了:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="org.springframework.web.filter.DelegatingFilterProxy"/>

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" p:maxIdle="300" p:maxWaitMillis="1000" p:testOnBorrow="true"></bean>

    <!-- 新增RedisHttpSessionConfiguration用於session共享 -->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
                p:hostName="你的redisIP" 
                p:port="你的redis的埠號" 
                p:password="你的redis設定的密碼"
                p:poolConfig-ref="poolConfig" 
                p:usePool="true" p:database="1"
                p:timeout="180000" />
                <!-- 讓Spring Session不再執行config命令 -->
    <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
</beans>複製程式碼

注意這裡;

你想輸入的替代文字
你想輸入的替代文字

對就是這個版本好,一定要比你的Spring版本小,哪怕一致也可以,這個是隱藏的坑這個版本向下相容,不對上相容。(坑一)
然後你就可以開始連線了,對吧!如果你的redis安裝在本地,那麼!沒有問題,可以了!如果你的redis在虛擬機器或者測試伺服器中,你就會發現一個問題。

16:45:15,105 ERROR ContextLoader:319 - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool複製程式碼

居然報錯了!為啥,我都配置對了啊!為啥報錯啊!我就開始在網上尋找啊!有個部落格寫的挺好,告訴我配置這個

    <!-- 讓Spring Session不再執行config命令 -->
    <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>複製程式碼

我配置了啊!為啥報錯了!還不對,難道是jar包衝突了!不是的!記住,我一直都沒有說過一個事情,就是redis的配置與安裝,這個才是重點。一般的部落格對這個都沒有說。
redis安裝很簡單的:開啟網址:redis.io/ 然後點選這裡

你想輸入的替代文字
你想輸入的替代文字
按照下面的操作就可以了!這個是Linux的操作,如果windows的,自己百度,一堆的。注意:==redis預設只允許本地訪問,要使redis可以遠端訪問可以修改redis.conf
將bind 127.0.0.1 改成了bind 0.0.0.0。然後使用工具連線,即可。到此!坑填完了!
你想輸入的替代文字
你想輸入的替代文字

一隻努力前行的小菜雞。

---------------未經允許,不可轉載---------------

相關文章