前言
刚进入到Java 开发的世界,对于小白Java的我来说,使用Maven + SpringBoot 的项目下启动redis;
第一步 本地安装Redis 服务
由于我是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);
}
}
View Code
上面测试代码分别写了 一个 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 }
View Code
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}%
已经打印出结果