欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法

 更新時(shí)間:2017年05月04日 11:52:49   作者:fengzp  
本篇文章主要介紹了學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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,在底層就會(huì)通過Spring Session得到的,并且會(huì)存儲(chǔ)到Redis或其他你所選擇的數(shù)據(jù)源中。

這里是我寫的一個(gè)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:

第一個(gè)tomcat

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

第二個(gè)tomcat

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

測(cè)試

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

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

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

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

發(fā)現(xiàn)在負(fù)載的情況下讀取session沒問題,并且是同一個(gè)session,成功實(shí)現(xiàn)負(fù)載+session共享!以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Security整合CAS的示例代碼

    Spring Security整合CAS的示例代碼

    本篇文章主要介紹了Spring Security整合CAS的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • 由@NotNull注解引出的關(guān)于Java空指針的控制

    由@NotNull注解引出的關(guān)于Java空指針的控制

    這是一些很容易學(xué)會(huì)的簡(jiǎn)單技術(shù),但是對(duì)于代碼質(zhì)量和健壯性來說確實(shí)很重要。以我的經(jīng)驗(yàn),僅是第一個(gè)小技巧就已經(jīng)對(duì)改進(jìn)代碼質(zhì)量具有很大的作用了
    2016-09-09
  • 使用FileReader采用的默認(rèn)編碼

    使用FileReader采用的默認(rèn)編碼

    這篇文章主要介紹了使用FileReader采用的默認(rèn)編碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組

    java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組

    這篇文章主要介紹了java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 解決Swagger修改請(qǐng)求對(duì)象字段文檔不更新問題

    解決Swagger修改請(qǐng)求對(duì)象字段文檔不更新問題

    這篇文章主要為大家介紹了解決Swagger修改請(qǐng)求對(duì)象字段文檔不更新的問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 分析那些不講武德的SDK(構(gòu)造使用規(guī)范)

    分析那些不講武德的SDK(構(gòu)造使用規(guī)范)

    這篇文章主要為大家介紹了盤點(diǎn)分析那些不講武德的SDK(構(gòu)造規(guī)范)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • 基于OpenCv與JVM實(shí)現(xiàn)加載保存圖像功能(JAVA?圖像處理)

    基于OpenCv與JVM實(shí)現(xiàn)加載保存圖像功能(JAVA?圖像處理)

    openCv有一個(gè)名imread的簡(jiǎn)單函數(shù),用于從文件中讀取圖像,本文給大家介紹JAVA?圖像處理基于OpenCv與JVM實(shí)現(xiàn)加載保存圖像功能,感興趣的朋友一起看看吧
    2022-01-01
  • 詳解@Autowired(required=false)注入注意的問題

    詳解@Autowired(required=false)注入注意的問題

    這篇文章主要介紹了@Autowired(required=false)注入注意的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • jvm垃圾回收之GC調(diào)優(yōu)工具分析詳解

    jvm垃圾回收之GC調(diào)優(yōu)工具分析詳解

    這篇文章主要為大家介紹了jvm垃圾回收之GC調(diào)優(yōu)工具的分析詳解,在進(jìn)行JVM?GC性能調(diào)優(yōu)之前,需要使用某些工具獲取到當(dāng)前應(yīng)用的狀態(tài)信息
    2022-01-01
  • Java進(jìn)階學(xué)習(xí):jar打包詳解

    Java進(jìn)階學(xué)習(xí):jar打包詳解

    Java進(jìn)階學(xué)習(xí):jar打包詳解...
    2006-12-12

最新評(píng)論