前面一篇文章已經寫了如何搭建一個單機版redis服務, 那么我們應該怎么在現有的系統中集成進來呢? 由于筆者使用的編程語言是java, 所以本篇文章主要描述springboot如何集成單redis節點完成數據的增刪改查.
springboot環境
快速搭建一個springboot工程
進入 https://start.spring.io
網站, 使用該網站初始化一個springboot工程
添加相關依賴
因為使用spring initializer已經幫我們把redis的依賴建立好了; 但是由于我們要使用jedis客戶端訪問redis, 所以還需要添加jedis的依賴;
1
2
3
4
5
|
<dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version> 2.9 . 0 </version> //版本號可以放在properties中作為屬性, 這邊用${jedis.version}來依賴 </dependency> |
配置redis節點信息
打開application.properties文件, 初始化的文件是空的; 我們將spring redis最基本的信息加入進去
1
2
|
spring.redis.host=localhost spring.redis.port= 6379 |
將redis信息讀入到程序中
新建一個java類命名為standaloneredisconfig.java
, 放在com.xxx.example.config
包下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.terrylmay.redis.example.config; import org.springframework.boot.autoconfigure.condition.conditionalonproperty; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.context.annotation.configuration; @configuration @configurationproperties (prefix = "spring.redis" ) @conditionalonproperty (name = { "spring.redis.host" }) public class standaloneredisconfig { string host; int port; public string gethost() { return host; } public void sethost(string host) { this .host = host; } public int getport() { return port; } public void setport( int port) { this .port = port; } } |
上面配置中的@conditionalonproperty(name = {"spring.redis.host"})
如果只是單機的redis則不需要添加該屬性; 但是為了后面一套代碼兼容多個redis部署模式, 使用該屬性作為是否創建bean的條件; 如果是集群模式那么就不會使用spring.redis.host
來作為連接字符串了;
配置jedis的連接池
將redis連接對象放入到spring容器中進行管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.terrylmay.redis.example; import com.terrylmay.redis.example.config.standaloneredisconfig; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.autoconfigure.condition.conditionalonbean; import org.springframework.context.annotation.bean; import org.springframework.data.redis.connection.redisconnectionfactory; import org.springframework.data.redis.connection.redisstandaloneconfiguration; import org.springframework.data.redis.connection.jedis.jedisconnectionfactory; import org.springframework.data.redis.core.stringredistemplate; @springbootapplication (scanbasepackages = { "com.terrylmay.redis.example" }) public class redisexampleapplication { public static void main(string[] args) { springapplication.run(redisexampleapplication. class , args); } @autowired standaloneredisconfig standaloneredisconfig; @autowired redisconnectionfactory redisconnectionfactory; @bean @conditionalonbean (value = {standaloneredisconfig. class }) public redisconnectionfactory standaloneredisconnectionfactory() { jedisconnectionfactory factory = new jedisconnectionfactory( new redisstandaloneconfiguration(standaloneredisconfig.gethost(), standaloneredisconfig.getport())); return factory; } @bean public stringredistemplate stringredistemplate() { return new stringredistemplate(redisconnectionfactory); } } |
這里的@conditionalonbean(value = {standaloneredisconfig.class})
與上面的conditionalonproperty
是一個道理
這里的scanbasepackages = {"com.terrylmay.redis.example"}
是為了以后將redis的客戶端獨立出一個工程而做的, 當然獨立出來的工程base包名還要是這個才可以;
因為還沒有看redis支持的數據結構, 那么現在只是把redis字符串模板類放到spring 容器中, 后續再增加其他數據類型的支持;
創建操作redis的接口 以及實現
創建icacheprovider.java
接口:
1
2
3
4
5
6
7
|
package com.terrylmay.redis.example.provider; public interface icacheprovider { void setstring(string key, string value); string getstring(string key); } |
jedis版本的實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.terrylmay.redis.example.provider.impl; import com.terrylmay.redis.example.provider.icacheprovider; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.redis.core.stringredistemplate; import org.springframework.stereotype.component; @component public class jediscacheprovider implements icacheprovider { @autowired stringredistemplate stringredistemplate; @override public void setstring(string key, string value) { stringredistemplate.opsforvalue().set(key, value); } @override public string getstring(string key) { return stringredistemplate.opsforvalue().get(key); } } |
這樣基本上一個可以操作redis的java程序就已經就緒了; 那么我們需要驗證一下, 當然如果在主工程中寫一個類去驗證也是沒有問題的, 比如創建一個bean, 并且放到被postcontruct
注解的方法里面;
但是更加專業的做法是寫一個測試程序來測試, 下面看一下該測試程序應該怎么寫
ut測試程序可用性
因為創建工程的時候, 就已經有一個測試類在test目錄下面了, 我們增加我們想要的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.terrylmay.redis.example; import com.terrylmay.redis.example.provider.icacheprovider; import org.junit. assert ; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springjunit4classrunner; @runwith (springjunit4classrunner. class ) @springboottest (classes = {redisexampleapplication. class }) public class redisexampleapplicationtests { @autowired icacheprovider jediscacheprovider; @test public void contextloads() { jediscacheprovider.setstring( "name" , "terrylmay" ); system.out.println(jediscacheprovider.getstring( "name" )); assert .assertequals( "terrylmay" , jediscacheprovider.getstring( "name" )); } } |
注: 程序中不要有打印, 使用logger或者直接斷言來處理
(本來想用markdown語法來標紅的, 但是發現簡書竟然不支持html的寫法; 沒辦法只能用``來搞定了)
開發過程中遇到的問題
一、在寫好所有的程序之后, 跑測試用例, 但是始終都是報nosuchbeanexception
caused by: org.springframework.beans.factory.nosuchbeandefinitionexception:no qualifying bean of type 'com.terrylmay.redis.example.config.standaloneredisconfig' available: expected at least 1 bean which qualifies as autowire candidate. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
原因共有三點:
1、寫了scanbasepackages
來掃描包下面的bean, 掃描的包與類所在的包不一樣, 只有一個字符之差 com.terrylmay.redis.example
與 com.terrlmay.redis.example
, 當然這時候idea
會報錯, 只是我不認識那個錯而已; idea報錯如圖所示:
2、按照網上的application.properties屬性的讀取方式, 只使用了一個注解:
@configurationproperties(prefix = "spring.redis")
但是點進該注解里面看, 它其實并沒有component注解的功能; 所以增加了@configuration
注解
3、第三個原因不仔細斷然不會發現這個錯誤
我理解的是只要工程里面父工程是spring-boot-starter-parent
, 那么就不應該存在這類jar包沒有依賴的問題, 打開文檔
依賴可粘貼版:
1
2
3
4
5
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> <optional> true </optional> </dependency> |
到這里, 一個能夠使用redis的java工程就已經就緒了; 最終的代碼全部在github的spring-redis-example倉庫下, 歡迎star與pr
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000017151786