在Spring-Session使用Redis如何實(shí)現(xiàn)共享session

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)在Spring-Session使用redis如何實(shí)現(xiàn)共享session,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

成都創(chuàng)新互聯(lián)專注于海湖新企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),電子商務(wù)商城網(wǎng)站建設(shè)。海湖新網(wǎng)站建設(shè)公司,為海湖新等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

1、添加依賴

<dependency>
 <groupId>org.springframework.session</groupId>
 <artifactId>spring-session-data-redis</artifactId>
 <version>1.2.1.RELEASE</version>
</dependency>
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.8.1</version>
</dependency>

2、配置

spring-mvc.xml:

<bean id="redisHttpSessionConfiguration"
   class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
  <property name="maxInactiveIntervalInSeconds" value="600"/>
</bean>

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  <property name="maxTotal" value="100" />
  <property name="maxIdle" value="10" />
</bean>

<bean id="jedisConnectionFactory"
   class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
  <property name="hostName" value="${redis_hostname}"/>
  <property name="port" value="${redis_port}"/>
  <property name="password" value="${redis_pwd}" />
  <property name="timeout" value="3000"/>
  <property name="usePool" value="true"/>
  <property name="poolConfig" ref="jedisPoolConfig"/>
</bean>

web.xml添加攔截器:

<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>

3、使用spring-session

只要使用標(biāo)準(zhǔn)的servlet api調(diào)用session,在底層就會通過Spring Session得到的,并且會存儲到Redis或其他你所選擇的數(shù)據(jù)源中。

這里是我寫的一個demo:

/**
 * @author fengzp
 * @date 17/2/23下午3:19
 */
@Controller
@RequestMapping(value = "index")
public class IndexController {

  private final Gson gson = new GsonBuilder().setDateFormat("yyyyMMddHHmmss").create();

  @RequestMapping(value = "login")
  public String login(HttpServletRequest request, String username){

    request.getSession().setAttribute("user", gson.toJson(new User(username,"123456")));

    return "login";
  }

  @RequestMapping(value = "index")
  public String index(HttpServletRequest request, Model model){

    User user = gson.fromJson(request.getSession().getAttribute("user").toString(), User.class);

    model.addAttribute("user", user);

    return "index";
  }
}

index.jsp:

第一個tomcat

<html>
<body>
<h3>Hello World!</h3>
<p>${user.username}</p>
</body>
</html>

第二個tomcat

<html>
<body>
<h3>Hello World! i am the second!</h3>
<p>${user.username}</p>
</body>
</html>

測試

這里利用上一篇nginx負(fù)載配置的兩個tomcat來測試。

首先訪問 http://192.168.99.100/feng/index/login.htm&#63;username=nginx 來觸發(fā)生成session。

查看redis,發(fā)現(xiàn)session已經(jīng)保存到redis。

在Spring-Session使用Redis如何實(shí)現(xiàn)共享session

訪問 http://192.168.99.100/feng/index/index.htm 來讀取session, 并刷新多次。

在Spring-Session使用Redis如何實(shí)現(xiàn)共享session

在Spring-Session使用Redis如何實(shí)現(xiàn)共享session

上述就是小編為大家分享的在Spring-Session使用Redis如何實(shí)現(xiàn)共享session了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章題目:在Spring-Session使用Redis如何實(shí)現(xiàn)共享session
瀏覽地址:http://muchs.cn/article38/ihgppp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站策劃、App設(shè)計(jì)、軟件開發(fā)網(wǎng)站設(shè)計(jì)公司、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)