最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot項(xiàng)目利用Redis實(shí)現(xiàn)session管理實(shí)例

 更新時(shí)間:2017年06月12日 09:09:49   作者:SkyeBeFreeman  
本篇文章主要介紹了Spring Boot項(xiàng)目利用Redis實(shí)現(xiàn)session管理實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在現(xiàn)代網(wǎng)絡(luò)服務(wù)中,session(會話)不得不說是非常重要也是一定要實(shí)現(xiàn)的概念,因此在web后臺開發(fā)中,對session的管理和維護(hù)是必須要實(shí)現(xiàn)的組件。這篇文章主要是介紹如何在Spring Boot項(xiàng)目中加入redis來實(shí)現(xiàn)對session的存儲與管理。

1. 利用Spring Initializr來新建一個(gè)spring boot項(xiàng)目

2. 在pom.xml中添加redis和session的相關(guān)依賴。項(xiàng)目生成的時(shí)候雖然也會自動(dòng)生成父依賴,但是1.5.3版本的spring boot的redis相關(guān)依賴有可能不能夠正常工作,筆者自行在maven repository找到了比較穩(wěn)定的版本如下方代碼所示

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>1.5.2.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-data-redis</artifactId>
  <version>1.3.0.RELEASE</version>
</dependency>

3. 在application.properties中添加redis數(shù)據(jù)庫的相關(guān)配置。這里為了演示使用方法只添加了相對簡單的配置,即設(shè)置了session存儲的數(shù)據(jù)庫類型、使用的數(shù)據(jù)庫號、數(shù)據(jù)庫地址和端口號。實(shí)戰(zhàn)中還會對使用非默認(rèn)數(shù)據(jù)庫、數(shù)據(jù)庫大小、數(shù)據(jù)庫最大連接數(shù)、生存時(shí)長、是否寫回磁盤等許多參數(shù)進(jìn)行配置

# Redis配置
spring.session.store-type=redis
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379

4. 編寫一個(gè)測試的controller來驗(yàn)證是否能夠正確地讀寫session。這里的controller中,我判斷了當(dāng)前獲取的session是否是新生成的。如果是,則輸出成功創(chuàng)建一個(gè)session對象,并返回session的id,然后在session中添加一個(gè)字段。如果session不是新生成的,即是已經(jīng)存在的session,則輸出session是已經(jīng)存在的并返回session的id,然后再輸出session中初次創(chuàng)建session保存的key所對應(yīng)的value

@SpringBootApplication
@EnableRedisHttpSession
@RestController
public class DemoApplication {

  private Logger logger = LoggerFactory.getLogger(this.getClass());

  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(DemoApplication.class);
    app.setWebEnvironment(true);
    app.run(args);
  }

  @GetMapping("/hello")
  public ResponseEntity<?> hello(HttpSession session) {
    if (session.isNew()) {
      logger.info("Successfully creates a session ,the id of session :" + session.getId());
      session.setAttribute("key", "hello");
    } else {
      logger.info("session already exists in the server, the id of session :"+ session.getId());
      logger.info(session.getAttribute("key").toString());
    }
    return new ResponseEntity<>("Hello World", HttpStatus.OK);
  }

}

5. 測試代碼

首先運(yùn)行Redis客戶端

redis-cli

查看當(dāng)前數(shù)據(jù)庫內(nèi)容

127.0.0.1:6379> keys * 
(empty list or set)

運(yùn)行spring boot項(xiàng)目

第一次瀏覽器訪問localhost:8080/hello,如下圖所示則成功運(yùn)行

查看log可以看到

2017-06-12 00:26:12.601 INFO 9580 — [nio-8080-exec-1] 
ication$$EnhancerBySpringCGLIB$$de942542 : Successfully creates a session ,the id of session :4368a535-9bfa-406b-975c-e58c2bca1e75

再次訪問localhost:8080/hello時(shí),查看log可以看到能夠正確地從redis中取出存放在session中的某個(gè)key對應(yīng)的值

2017-06-12 00:30:43.533 INFO 9580 — [nio-8080-exec-5] 
ication$$EnhancerBySpringCGLIB$$de942542 : session already exists in the server, the id of session :4368a535-9bfa-406b-975c-e58c2bca1e75 
2017-06-12 00:30:43.533 INFO 9580 — [nio-8080-exec-5] 
ication$$EnhancerBySpringCGLIB$$de942542 : hello

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

英超| 开化县| 谢通门县| 昌吉市| 舟山市| 将乐县| 思南县| 镇远县| 黄陵县| 卓尼县| 新疆| 安阳市| 凭祥市| 南丹县| 滕州市| 慈溪市| 闻喜县| 万山特区| 伊金霍洛旗| 肃北| 若尔盖县| 通城县| 全椒县| 凤庆县| 常州市| 大荔县| 九台市| 报价| 宣城市| 建水县| 固阳县| 翁源县| 苏尼特右旗| 瓦房店市| 丘北县| 汽车| 保德县| 如皋市| 新乡县| 沙坪坝区| 化隆|