第一步下载Redis:
下载地址:http://redis.io/download,下载最新文档版本。
第二步上传到linux并进行解压编译:
$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make
-2.8.17.tar.gz
$ cd redis-2.8.17
$ make
第三步通过vi编辑器修改访问IP允许其他设备访问:
$ vi redis.conf
将:bind 127.0.0.1 改为:bind 0.0.0.0
第四步启动redis:(记得加载配置文件)
./redis-server ../redis.conf
第五步:spring boot整合引入依赖包 pom.xm:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第六步进行application.properties相关配置:
#################redis基础配置#################
spring.redis.database=5
spring.redis.host=10.0.0.224
#spring.redis.password=admin.1231
spring.redis.port=6379
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=3000
#################redis线程池设置#################
# 连接池中的最大空闲连接,默认值也是8。
spring.redis.pool.max-idle=500
#连接池中的最小空闲连接,默认值也是0。
spring.redis.pool.min-idle=50
# 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
spring.redis.pool.max-active=2000
# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
spring.redis.pool.max-wait=1000
第七步进行代码测试:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.*; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/redis") public class Test { /** * redis对象工厂 */ @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping public String test(){ //string类型操作对象 ValueOperations valueOperations = redisTemplate.opsForValue(); //哈希类型操作对象 HashOperations hashOperations = redisTemplate.opsForHash(); //List类型操作对象 ListOperations listOperations = redisTemplate.opsForList(); //集合类型操作对象 SetOperations setOperations = redisTemplate.opsForSet(); //有序集合操作对象 ZSetOperations zSetOperations = redisTemplate.opsForZSet(); /* Meteorological meteorological=new Meteorological(); meteorological.setName("光照"); meteorological.setValue("205"); meteorological.setTime("2018-9-29"); Map<String,Object> map=new HashMap<>(); map.put("1",meteorological); hashOperations.putAll("nb",map); Object o = hashOperations.get("nb", "1"); List list = hashOperations.values("nb"); for (Object o1 : list) { } Meteorological meteorological1=(Meteorological) o; System.out.println(meteorological1.getName()+meteorological1.getTime());*/ List<Integer> list=new ArrayList<>(); for (int i = 0; i <10; i++) { list.add(i); } listOperations.leftPushAll("list",list); return null; } }
总结:在进行对象存储时需要将对象实体进行序列化,继承Serializable接口,否则会报DefaultSerializer requires a Serializable payload but received an object of type[xxx]异常.