Redis的安装和使用
引言
Redis 是基于内存的、采用Key-Value结构化存储的NoSQL数据库,底层采用单线程和多路IO复用模型加快查询速度。
- 支持多种数据格式的存储;
- 支持持久化存储;
- 支持集群部署。
安装
Windows安装
Redis 官方不支持Windows的安装,通过启用windows自带的WSL2 ((Windows Subsystem for Linux)Linux子系统工具可以使用和安装。
Windows 版本要求大于10 。
具体安装流程和Linux安装一致。
Linux安装
大多数Linux发行版本提供了Redis的安装包,通过安装软件包命令可以从远程安装对应的工具。
Ubuntu/Debian 系统的安装流程如下:
- 添加Redis官方软件仓库源到Apt软件。
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis
- 前置要求:如果运行的像是docker这类最小发行版本需要先安装
lsb-release
。
sudo apt install lsb-release
Redis启动
Linux Ubuntu/Debian 系统启动命令如下:
sudo service redis-server start
Redis访问
通过官方的Cli工具访问:
redis-cli
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h <hostname> Server hostname (default: 127.0.0.1).
-p <port> Server port (default: 6379).
-s <socket> Server socket (overrides hostname and port).
-a <password> Password to use when connecting to the server.
You can also use the REDISCLI_AUTH environment
variable to pass this password more safely
(if both are used, this argument takes precedence).
Redis配置
详细配置例子查看 https://redis.io/docs/management/config-file/
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 60
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
requirepass redis1234
分布式部署
需要修改集群的相关配置,此处从略。 详情可参考网络或者 https://redis.io/docs/management/replication/
################################ REDIS CLUSTER ###############################
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes
Springboot集成redis
- 修改依赖加入Redis启动项目,此处以maven为例子说明。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 修改配置,指定redis启动。
spring:
# Redis 配置
redis:
# Redis 服务器地址
host: 127.0.0.1
# 连接端口号
port: 6379
# 数据库索引(0 - 15)
password: redis1234
database: 0
# 连接超时时间(毫秒)
timeout: 600000
# lettuce 参数
lettuce:
pool:
# 最大连接数(使用负值表示没有限制) 默认为 8
max-active: 10
# 最大阻塞等待时间(使用负值表示没有限制) 默认为 -1 ms
max-wait: -1
# 最大空闲连接 默认为 8
max-idle: 5
# 最小空闲连接 默认为 0
min-idle: 0
- 增加 RedisTemplate 序列化配置,以FastJSON2为例子说明。
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);//设置默认的Serialize,包含 keySerializer & valueSerializer
redisTemplate.setKeySerializer(fastJsonRedisSerializer);//单独设置keySerializer
redisTemplate.setValueSerializer(fastJsonRedisSerializer);//单独设置valueSerializer
return redisTemplate;
}
}
- 使用的demo,通过RedisTemplate访问。
@RestController
@Api(tags = "demo")
@RequestMapping("/v1/demo/")
@Slf4j
public class RedisDemo {
@Autowired
private RedisTemplate redisTemplate;
@ApiOperation(value = "get")
@PostMapping("get")
@ResponseBody
public Object get(String key) {
ValueOperations valueOperations = redisTemplate.opsForValue();
Object val = valueOperations.get(key);
return val;
}
@ApiOperation(value = "set")
@PostMapping("set")
@ResponseBody
public Boolean set(String key,String val) {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set(key,val);
return true;
}
@ApiOperation(value = "delete")
@PostMapping("delete")
@ResponseBody
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)