springboot+redis简单学习记录
Redis
什么是redis?
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
Redis 的功能:
用来充当临时数据库,或者是中间件,暂时没有用的其他功能
第一步:在windows10 安装Redis
下载安装包:https://github.com/microsoftarchive/redis/releases
选择zip压缩包下载
下载完成后将压缩包解压在D:\Program Files\
将根目录重命名为Redis
进入dos界面,切换根目录下,执行:
redis-server.exe redis.windows.conf
出现如下图成功:
新开dos界面,切换目录到rides下,执行:
redis-cli.exe -h 127.0.0.1 -p 6379
设置键值对:
set myKey abc
获取键值:
get mykey
搭建springboot+rides:
创建maven项目:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>rides</artifactId> <version>0.0.1-SNAPSHOT</version> <name>rides</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建yml:
server: port: 8080 # Redis数据库索引(默认为0) # Redis服务器地址 # Redis服务器连接端口 # 连接池最大连接数(使用负值表示没有限制) # 连接池最大阻塞等待时间(使用负值表示没有限制) # 连接池中的最大空闲连接 # 连接池中的最小空闲连接 # 连接超时时间(毫秒) spring: redis: database: 0 host: 127.0.0.1 port: 6379 jedis: pool: max-active: 20 max-wait: -1 max-idle: 10 min-idle: 0 timeout: 1000
创建 RedisService.java
package com.example.rides.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; /** * @author: * @date: 2020/1/6 * @description: **/ @Service public class RidesService { @Autowired private RedisTemplate redisTemplate; @Autowired(required = false) public void setRedisTemplate(RedisTemplate redisTemplate) { RedisSerializer stringSerializer = new StringRedisSerializer();//序列化为String Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);//序列化为Json redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); this.redisTemplate = redisTemplate; } public String setMsg(String key,String msg) { redisTemplate.opsForValue().set(key,msg); return "success"; } public String getMsg(String key) { return (String) redisTemplate.opsForValue().get(key); } /** * 设置写入的有效时间 */ public String setMsgTime(String key,String msg,Long expiredTime){ redisTemplate.opsForValue().set(key,msg,expiredTime,TimeUnit.SECONDS);// 这里设置的时间单位为秒 return "success"; } }
创建controller:Redishandier.java
package com.example.rides.controller; import com.example.rides.service.RidesService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @author: * @date: 2020/1/6 * @description: **/ @RestController @RequestMapping("/msg") public class RidesHandler { @Autowired RidesService ridesService; @GetMapping("/set") public String setMsg(@RequestParam(value = "key") String key, @RequestParam(value = "msg") String msg){ return ridesService.setMsg(key,msg); } @GetMapping("/setMsgTime") public String setMsg(@RequestParam(value = "key") String key, @RequestParam(value = "msg") String msg,@RequestParam(value = "expiredTime") Long expiredTime){ return ridesService.setMsgTime(key,msg,expiredTime); } @GetMapping("/get") public String getMsg(@RequestParam(value = "key") String key){ return ridesService.getMsg(key); } }
启动项目,然后用postMan请求set,请求成功后,到redis 查看,或者get一下。
这里我使用的是redis Desktop Manager 用来连接redis.
下面是连接成功的示例