Spring boot + redis 實現session 共用管理

來源:互聯網
上載者:User
一、準備工作

版本

更新或下載URL

Windows 10

64位

IntelliJ IDEA

2017.1.5

http://www.jetbrains.com/idea/

JDK8

64位

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

redis

3.2.100

https://github.com/MSOpenTech/redis/releases

Spring-data-redis

Jar包

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

jedis

jar包

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

範例程式碼地址:

Spring boot session + redis

 

https://github.com/Cavan2477/SpringBoot-Session-Redis

 



二、搭建步驟

1. 安裝redis 1) 下載Windows redis

        下載地址見準備工作。


2) 解壓redis至自訂目錄



3) 設定redis需要訪問密碼
  a) 找到redis所在目錄的redis.windows.conf檔案的requirepass欄位,去掉注釋,修改你需要的密碼
  b) 設定永久登入要求輸入密碼串連
 


4) 啟動redis



 
2. 建立Spring boot項目 1) 建立Spring boot項目


2) Maven依賴、JDK、Package設定  


3) 項目依賴項選擇(pom.xml檔案內均可查看)
 


4) 建立項目成功,依據pom.xml檔案自動更新jar 本地倉庫預設路徑:${user.home}/.m2/repository,也可以自行設定
 


5) 運行Spring boot 應用是否能啟動
 
未添加相關的redis支援項時無法正常啟動。

添加相關配置,步驟如下:
a) application.properties
 


b) pom.xml
 

c) CodeCaptionApplication.java
 


6) 重新啟動Spring boot應用,並訪問session測試路徑 訪問路徑:

http://localhost:8080/sessionRedis/test


訪問結果:



此步驟需要添加的controller源碼見附錄。

至此已能正常訪問整合session+redis的Spring boot應用。源碼下載地址見準備工作。


三、 FAQ 1. Spring-boot-redis與jedis版本不一致 1) 應用無法正常啟動;

2) Jedis對應jar包版本大於2.4.2,詳細情況請在Maven官網進行查詢。


2. Redis服務端設定臨時密碼方式 1) 開啟cmd命令列
2) 進入redis所在目錄
3) 使用startup.bat指令碼啟動redis服務
4) 輸入命令redis-cli –h 127.0.0.1 –p 6379 登入至客服端
5) 設定臨時密碼:config set requirepass <你的密碼>
6) 查看密碼設定情況:config get requirepass
7) 結果如下:(密碼設定後需要重新登入)
 


四、 附錄 1. Maven本地倉庫設定檔案settings.xml下載路徑 已添加阿里雲倉庫更新地址(位於測試工程源碼下載地址的xml檔案夾下)。

2. Maven倉庫 查詢地址:
http://mvnrepository.com/

3. Redis startup.bat指令碼
內容
redis-server.exe redis.windows.conf

4. com.example.controller.TestController.java源碼

package com.example.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpSession;@SuppressWarnings("unchecked")@RestControllerpublic class TestController{    private Logger logger = LoggerFactory.getLogger(this.getClass());    @RequestMapping("/sessionRedis/test")    public ResponseEntity<?> httpSessionRedisTest(HttpSession httpSession)    {        if (httpSession.isNew()) {            logger.info("Successfully creates a session ,the id of session :" + httpSession.getId());            httpSession.setAttribute("key", "hello");        } else {            logger.info("session already exists in the server, the id of session :" + httpSession.getId());            logger.info(httpSession.getAttribute("key").toString());        }        ResponseEntity<?> entity = new ResponseEntity<Object>("Hello world, session id:" + httpSession.getId(), HttpStatus.OK);        return entity;    }}

效果圖:


有任何問題請給我留言或直接聯絡我,歡迎批評指正。

 

連絡方式

QQ:247706624

Email:liuyeying1103@163.com/code_captain@163.com

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.