获得镜像
先pull一个稳定镜像
docker pull redis:7.2
配置文件路径
选择一个文件夹,配置配置文件
touch redis.conf
我选择的是**/root/redis**
以下是配置文件
# 绑定 IP,改为 0.0.0.0 以允许外部访问
bind 0.0.0.0
# 保护模式关闭,防止 Redis 拒绝外部连接
protected-mode no
# Redis 监听端口
port 6379
# 设置 Redis 密码,安全访问
requirepass Pssword
# 设置最大客户端连接数
maxclients 10000
# 启用 RDB 持久化(定期保存快照)
save 900 1
save 300 10
save 60 10000
# 启用 AOF(日志持久化)
appendonly yes
# AOF 同步写入策略
appendfsync everysec
# 设置最大内存限制(可选)
maxmemory 512mb
maxmemory-policy allkeys-lru
# 开启日志(可选)
logfile "/var/log/redis.log"
# 允许远程访问
daemonize no
启动命令
docker run -d \
--name my-redis \
-p 6379:6379 \
--restart always \
-v ~/redis/redis.conf:/etc/redis/redis.conf \
-v ~/redis/data:/data \
redis:7.2 \
redis-server /etc/redis/redis.conf
-d 以 后台 运行 Redis。
--name my-redis 给容器命名 my-redis。
-p 6379:6379 映射 Redis 端口(外部可访问)。
--restart always 容器崩溃或重启服务器时,自动重启 Redis。
-v ~/redis/redis.conf:/etc/redis/redis.conf 挂载 Redis 配置。
-v ~/redis/data:/data 持久化 Redis 数据(防止重启丢失)。
redis:latest redis-server /etc/redis/redis.conf 使用自定义配置启动 Redis。
失败情况
查看日志docker logs my-redis --tail 20
权限问题Can’t open the log file: Permission denied
解决方案:去给文件sudo chmod 777权限,再给sudo chown -R 1001:1001 ~/redis Redis 以默认的 Docker 内部用户运行
如果不行,修改下面部分配置文件重启
logfile ""
修改为空
然后重启docker restart id
可视化工具
Another Redis Desktop Manager
建议是github下载使用
https://github.com/qishibo/AnotherRedisDesktopManager/releases
Maven引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>3.2.3</version>
</dependency>
yaml配置文件
redis:
port: 6379
password: 填写自己的
host: 填写自己Ip
配置Redis的Config类
package com.youkill.aiserver.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
调用处代码测试
@Autowired
private RedisTemplate<String, Object> redisTemplate;
//加载对象
public void setDataToRedis(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object getDataFromRedis(String key) {
return redisTemplate.opsForValue().get(key);
}
//避免代码重复,get,set值
Redis比较重要一点
有必要情况下关闭Lua脚本或者对其进行限制
源于CVE-2024-46981
其次当前持久化是永久,对其进行设置的话去配置文件
809

被折叠的 条评论
为什么被折叠?



