SpringBoot2.X集成Redis3.2 Jedis

概述

        前兩三年使用Redis,後面工作中也一直使用。這次由於搭建公司框架,在SpringBoot的基礎上集成Redis。

 

 

win10下安裝Redis3.2

 

        下載Redis3.2

        首先下載Redis3.2,目前來講,3.2是相對穩定且比較新的版本了。下載地址:Redis-64(V3.2)

        安裝Redis3.2

        安裝很簡單,將下載的文件進行解壓即可。

        

        

        啓動測試Redis

        啓動時,進入Redis的安裝目錄,在硬盤的路徑一欄輸入“cmd”,然後回車,即可直接cmd命令窗口,且目錄已經是Redis的安裝目錄。

                

        然後在cmd窗口中輸入命令:redis-server redis.windows.conf,如下圖所示,說明啓動成功。

        

        

        安裝Redis Desktop Manager

      Redis Desktop Manager是Redis的桌面管理工具,可以說是Redis的可視化窗口工具。下載地址:Redis Desktop Manager

      

        下載後,安裝基本上下一步,很簡單。安裝完成時,提醒查看Redis Desktop Manager的快速入門:快速入門

 

        Redis Desktop Manager連接Redis服務

       安裝好Redis Desktop Manager後,就可以連接之前啓動的Redis服務了。因爲之前沒有配置Redis服務密碼,所以下面不需輸入密碼,只要輸入連接名、地址、端口,然後連接就可以了。     

         連接成功後,如下圖所示。之所以會的db0到db15,是因爲Redis默認會給出16個db,且是從0開始的。 Redis Desktop Manager的具體使用,可以參考上面給出的快速入門連接,網上也有很多資料。  

 

SpringBoot集成Redis3.2

        引入Redis的依賴

       這裏要特別說明一下,因爲我使用的SpringBoot是2.X,Redis是3.2,SpringBoot2.X默認採用lettuce,而1.5默認採用的是jdeis,本文描述的是使用jedis,所以在依賴裏要排除lettuce。否則會報錯:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: io/netty/handler/ssl/SslProvider

<!-- Redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<!-- 1.5的版本默認採用的連接池技術是jedis,2.0以上版本默認連接池是lettuce, 因爲此次是採用jedis,所以需要排除lettuce的jar -->
			<exclusions>
				<exclusion>
					<groupId>redis.clients</groupId>
					<artifactId>jedis</artifactId>
				</exclusion>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- jedis客戶端 -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>

		<!-- spring2.X集成redis所需common-pool2,使用jedis必須依賴它-->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.5.0</version>
		</dependency>
		<!--<dependency>-->
			<!--<groupId>org.springframework.session</groupId>-->
			<!--<artifactId>spring-session-data-redis</artifactId>-->
		<!--</dependency>-->

        Redis配置文件

        當引入依賴成功後,在書寫配置信息時,會自動給出提示,如下圖所示。

        

        具體配置內容如下所示。寫在application-dev.properties中。

#redis
#redis數據庫索引,默認爲0
spring.redis.database=0
#服務地址
spring.redis.host=localhost
#端口
spring.redis.port=6379
#密碼,默認爲空,本地沒有設置密碼,所以這裏不填寫
spring.redis.password=
#連接池最大的連接數,若使用負值表示沒有限制
spring.redis.jedis.pool.max-active=8
#連接池最大阻塞等待時間,若使用負值表示沒有限制
spring.redis.jedis.pool.max-wait=-1
#連接池中的最大空閒連接
spring.redis.jedis.pool.max-idle=8
#連接池中的最小空閒連接
spring.redis.jedis.pool.min-idle=0
#連接超時時間(單位爲毫秒)
spring.redis.timeout=0

          因爲我們存儲的數據有可能是字符串、對象等不同的內容,也會有中文等不同編碼的數據,所以這裏先自定義下Redis的序列化器。

 

          SpringBoot與Redis集成測試

 測試代碼如下:            

       到這裏,SpringBoot2.x與Redis3.2的集成已經成功了,至於在項目中怎麼使用,主要還是看團隊的情況了。

       

Redis簡要說明

        下面對本次集成過程中一些點進行簡要說明。

      首先是在啓動Redis的時候,輸入的命令是:redis-server redis.windows.conf,其中redis-server對應是redis-server.exe,即Redis的服務程序,redis.windows.conf對應的是windows系統下Redis的配置文件,可以配置Redis的端口、密碼等。

       在集成過程中,一定要注意SpringBoot和Redis不同版本的情況。此次使用的SpringBoot版本是2.X,Redis版本是3.2,SpringBoot2.X默認採用lettuce,而1.5默認採用的是jdeis,本文描述的是使用jedis,所以在引入依賴裏要排除lettuce。更大的區別是SpringBoot2.X集成Redis的時候,引入的依賴spring-boot-starter-redis,而SpringBoot2.X使用的依賴是spring-boot-starter-data-redis,它們的RedisCacheManager就不一樣了,在寫Redis的配置類時就會遇到問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章