一、环境准备
Redis 安装
# Ubuntu/Debian系统
sudo apt update
sudo apt install redis-server
# 启动Redis
sudo systemctl start redis-server
# 验证运行状态
sudo systemctl status redis-server
2.配置 Redis 远程访问
# 编辑配置文件
sudo nano /etc/redis/redis.conf
# 找到bind 127.0.0.1 ::1并修改为(生产环境建议设置具体IP)
bind 0.0.0.0
# 启用密码认证(推荐)
requirepass your_redis_password
# 重启Redis
sudo systemctl restart redis-server
二、AEM 端配置
1.添加 Redis 客户端依赖
在 AEM 项目的pom.xml
中添加:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.3</version> <!-- 最新稳定版 -->
</dependency>
2.创建 Redis 连接工厂
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisConnectionFactory {
private static JedisPool jedisPool;
static { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(100); poolConfig.setMaxIdle(10); poolConfig.setMinIdle(5); poolConfig.setTestOnBorrow(true); poolConfig.setTestOnReturn(true); // 替换为实际的Redis服务器信息 jedisPool = new JedisPool( poolConfig, "redis-server-ip", 6379, 5000, "your_redis_password" ); } public static Jedis getConnection() { return jedisPool.getResource(); } public static void closeConnection(Jedis jedis) { if (jedis != null) { jedis.close(); } } }
三、缓存实现示例
创建 Redis 缓存服务
import org.osgi.service.component.annotations.Component;
import redis.clients.jedis.Jedis;
@Component(service = CacheService.class)
public class RedisCacheServiceImpl implements CacheService {
@Override
public void put(String key, String value) {
Jedis jedis = null; try { jedis = RedisConnectionFactory.getConnection(); jedis.set(key, value); } catch (Exception e) { // 记录异常日志 e.printStackTrace(); } finally { RedisConnectionFactory.closeConnection(jedis); } } @Override public String get(String key) { Jedis jedis = null; try { jedis = RedisConnectionFactory.getConnection(); return jedis.get(key); } catch (Exception e) { // 记录异常日志 e.printStackTrace(); return null; } finally { RedisConnectionFactory.closeConnection(jedis); } } @Override public void delete(String key) { Jedis jedis = null; try { jedis = RedisConnectionFactory.getConnection(); jedis.del(key); } catch (Exception e) { // 记录异常日志 e.printStackTrace(); } finally { RedisConnectionFactory.closeConnection(jedis); } } }
2.缓存服务接口
public interface CacheService {
void put(String key, String value);
String get(String key);
void delete(String key);
}
四、AEM Dispatcher 配置
在dispatcher.any
中添加 Redis 缓存规则:
/vanityurl {
/docroot "/content"
/handler "/libs/granite/dispatcher/content/vanity"
/invalidate {
/0001 { /type "path" /glob "/content/**" }
}
/cache {
/docroot "/content"
/rules {
/0001 { /type "allow" /glob "*" }
} /headers { /0001 { /type "allow" /name "Content-Type" } } /invalidate { /0001 { /type "path" /glob "/content/**" } } /redis { /host "redis-server-ip" /port 6379 /password "your_redis_password" /timeout 5000 } } }
五、验证集成
Redis CLI 测试
redis-cli -h redis-server-ip -a your_redis_password
ping # 应返回PONG
2.AEM 代码测试
public class CacheTest {
public static void main(String[] args) {
CacheService cacheService = new RedisCacheServiceImpl();
// 存入缓存
cacheService.put("testKey", "Hello Redis!");
// 获取缓存
String value = cacheService.get("testKey");
System.out.println("缓存值: " + value); // 删除缓存 cacheService.delete("testKey"); } }
六、生产环境建议
-
安全加固
- 禁用 Redis 默认端口 (6379)
- 配置防火墙限制访问
- 启用 TLS 加密通信
- 定期备份 Redis 数据
-
性能优化
- 配置 Redis 内存策略
# 在redis.conf中添加 maxmemory 256mb maxmemory-policy allkeys-lru
- 使用 Redis 集群提高可用性
3.监控与告警
○ 集成 Prometheus+Grafana 监控 Redis 性能
○ 设置内存使用率、连接数等告警阈值
通过以上步骤,你可以成功将 AEM 6.5 与最新版 Redis 集成,实现高性能缓存机制。
问题:
一、修改绑定地址这一步,需要端口号吗?
在 Redis 配置中修改绑定地址(bind
参数)时,不需要指定端口号。bind
参数仅用于配置 Redis 服务器监听的 IP 地址,端口号由单独的port
参数控制(默认是 6379)。
详细说明:
1.bind 参数作用
该参数用于限制 Redis 服务器只接受来自指定 IP 地址的连接请求。例如:
# 仅监听本地回环地址(默认值)
bind 127.0.0.1
# 监听所有可用网络接口(不推荐生产环境)
bind 0.0.0.0
# 监听特定IP地址(推荐生产环境)
bind 192.168.1.100
2。port 参数作用
该参数用于指定 Redis 服务器监听的端口号,默认是 6379。例如:
port 6379
3.配置示例
如果需要 Redis 监听特定 IP 和端口,应分别设置这两个参数:
# 监听特定IP地址
bind 192.168.1.100
# 使用非标准端口
port 6380
注意事项:
安全风险:bind 0.0.0.0会使 Redis 暴露在公网,存在安全风险,生产环境建议绑定具体内网 IP。
防火墙配置:如果修改了默认端口,需确保防火墙开放相应端口。
重启生效:修改配置后需重启 Redis 服务:
sudo systemctl restart redis-server
验证配置:
可以通过以下命令检查 Redis 监听的 IP 和端口:
# 查看Redis进程监听情况
sudo netstat -tulpn | grep redis
# 输出示例(显示监听127.0.0.1:6379)
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1234/redis-server 1
因此,在修改 Redis 绑定地址时,只需指定 IP 地址,无需包含端口号。