前言
剛進入到Java 開發的世界,對於小白Java的我來說,使用Maven + SpringBoot 的項目下啟動redis;
第一步 本地安裝Redis 服務
關於redis的教程鏈接 點擊這里:https://www.runoob.com/redis/redis-install.html
由於我是Mac 上開發因此安裝如下:
1. 下載redis 安裝包
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz $ tar xzf redis-2.8.17.tar.gz $ cd redis-2.8.17 $ make
備注:上面的下載后可以解壓到你自己的人以目錄
2. 啟動redis 服務
make完后 redis-2.8.17目錄下會出現編譯后的redis服務程序redis-server,還有用於測試的客戶端程序redis-cli,兩個程序位於安裝目錄 src 目錄下:
下面啟動redis服務.
$ cd src $ ./redis-server
3. 啟動服務后進行客戶端鏈接測試
$ cd src $ ./redis-cli 127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> get foo "bar"
這里可以看到本地的地址和端口分別為: 127.0.0.1 和 6379 基本端口是不會變的。
到這里為子,我們的redis 本地服務算是安裝完成。
第二步 idea 配置以及代碼處理
1. pom.xml 文件中引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2. application.yml(或applicationx.xml)之中 配置redis服務信息
由於我的工程是使用yml 那么這里就以yml 為主:
# Redis 配置 redis: database: 0 #數據庫索引(默認為0) host: 127.0.0.1 port: 6379 #默認鏈接端口 password: #默認為空 lettuce: pool: max-active: 8 #最大鏈接池 max-wait: -1 #最大阻賽等待時間(使用負值沒有限制)默認為-1 max-idle: 8 #連接池中的最大空閑連接 默認 8 min-idle: 0
3. 編寫簡單代碼測試

package com.king.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/redis") @ResponseBody public class RedisStringController { @Autowired private StringRedisTemplate stringRedisTemplate; @PutMapping("/string/put") public void put(String key , @RequestParam(required = false,defaultValue = "default") String value){ stringRedisTemplate.opsForValue().set(key, value); } @GetMapping("/string/get") public Object get(String key){ return stringRedisTemplate.opsForValue().get(key); } }
上面測試代碼分別寫了 一個 put 提交 和一個get請求
4. 運行測試(Run)
我這里代碼是PUT 方式,所以不能直接在瀏覽器上進行訪問,這里我就使用最簡單的curl命令進行請求。
打開自己的終端,執行以下命令:
執行put 數據存儲操作
$ curl -X PUT --data 'key=kingbo&value=ok' http://127.0.0.1:8080/redis/string/put
執行get 獲取操做
$ curl http://127.0.0.1:8080/redis/string/get\?key\=kingbo ok
第三部 實現redis進行Java對象模型存儲
在上述使用中,是無法存儲對象的,存儲對象的話需要使用RedisTemplate並且要使用響應的序列化機制,下面我們就來測試下:
1. 引入序列化的jar包,這里我們是使用jackson
在pom.xml 文件中,添加依賴
<!-- redis 對象存儲相關配置--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>
2. 在Java工程中添加 RedisConfig文件
1 package com.king.config; 2 /* 3 * 文檔地址 4 * http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html 5 * 6 * 7 * */ 8 9 10 import org.springframework.cache.annotation.EnableCaching; 11 import org.springframework.context.annotation.Bean; 12 import org.springframework.context.annotation.Configuration; 13 import org.springframework.data.redis.connection.RedisConnectionFactory; 14 import org.springframework.data.redis.core.RedisTemplate; 15 import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; 16 import org.springframework.data.redis.serializer.StringRedisSerializer; 17 18 19 @Configuration 20 //@EnableCaching 21 public class RedisConfig { 22 @Bean 23 public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ 24 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); 25 26 //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值 27 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); 28 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); 29 30 //使用StringRedisSerializer來序列化和反序列化redis的ke 31 redisTemplate.setKeySerializer(new StringRedisSerializer()); 32 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); 33 34 //開啟事務 35 redisTemplate.setEnableTransactionSupport(true); 36 37 redisTemplate.setConnectionFactory(redisConnectionFactory); 38 return redisTemplate; 39 } 40 41 }
3. 添加測試controller
1 package com.king.controller; 2 3 import com.king.pojo.User; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.data.redis.core.RedisTemplate; 6 import org.springframework.web.bind.annotation.*; 7 8 @RequestMapping("/redis/object") 9 @RestController 10 public class RedisObjectController { 11 @Autowired 12 private RedisTemplate redisTemplate; 13 14 @GetMapping("/get/{username}") 15 public Object get(@PathVariable("username") String username){ 16 return redisTemplate.opsForValue().get(username); 17 } 18 19 @PutMapping("/put") 20 public void put(String username,Integer age,Integer id){ 21 User user= new User(); 22 user.setId(id); 23 user.setAge(age); 24 user.setUsername(username); 25 redisTemplate.opsForValue().set(username,user); 26 } 27 28 }
其中我這里的User 模型代碼如下

1 package com.king.pojo; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private Integer id; 7 private String username; 8 private Integer age; 9 10 public Integer getId() { 11 return id; 12 } 13 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 18 public String getUsername() { 19 return username; 20 } 21 22 public void setUsername(String username) { 23 this.username = username; 24 } 25 26 public Integer getAge() { 27 return age; 28 } 29 30 public void setAge(Integer age) { 31 this.age = age; 32 } 33 }
4. 運行測試
數據上傳
$ curl -X PUT --data 'username=jinlingbo&age=18&id=1' http://127.0.0.1:8080/redis/object/put
數據查詢(get 操作可以直接在瀏覽器訪問)
$ curl http://127.0.0.1:8080/redis/object/get/jinlingbo {"id":1,"username":"jinlingbo","age":18}%
已經打印出結果
參考文獻
https://segmentfault.com/a/1190000017805065
https://www.runoob.com/redis/redis-install.html