溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-10-14 20:57:34 來源:腳本之家 閱讀:143 作者:SkyeBeFreeman 欄目:編程語言

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

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

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

2. 在pom.xml中添加redis和session的相關(guān)依賴。項(xiàng)目生成的時(shí)候雖然也會(huì)自動(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存儲(chǔ)的數(shù)據(jù)庫類型、使用的數(shù)據(jù)庫號(hào)、數(shù)據(jù)庫地址和端口號(hào)。實(shí)戰(zhàn)中還會(huì)對使用非默認(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)行

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

查看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í)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI