AEM6.5集成Redis详细步骤(附代码)

一、环境准备

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"); } }

六、生产环境建议

  1. 安全加固

    • 禁用 Redis 默认端口 (6379)
    • 配置防火墙限制访问
    • 启用 TLS 加密通信
    • 定期备份 Redis 数据
  2. 性能优化

    • 配置 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 地址,无需包含端口号。

相关推荐
花落人散处34 分钟前
SpringAI——接入高德MCP服务
java·后端
超浪的晨34 分钟前
Java 代理机制详解:从静态代理到动态代理,彻底掌握代理模式的原理与实战
java·开发语言·后端·学习·代理模式·个人开发
天天摸鱼的java工程师35 分钟前
🧠 MySQL 索引结构有哪些?优缺点是什么?【原理 + 场景实战】
java·后端·面试
骑着蜗牛闯宇宙40 分钟前
Thinkphp8 Redis队列与消息队列Queue
redis·php
java叶新东老师1 小时前
idea提交时忽略.class、.iml文件和文件夹或目录的方法
java·开发语言
飞翔的佩奇1 小时前
Java项目:基于SSM框架实现的社区团购管理系统【ssm+B/S架构+源码+数据库+毕业论文+答辩PPT+远程部署】
java·数据库·vue.js·毕业设计·mybatis·答辩ppt·社区团购
TDengine (老段)1 小时前
TDengine 转化函数 TO_TIMESTAMP 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Warren981 小时前
Java Collections工具类
java·开发语言·笔记·python·学习·oracle·硬件工程
java叶新东老师2 小时前
CMakelists.txt 实现多级目录编译
java·服务器·数据库
_风不会停息2 小时前
JDK1.8升级 JDK21 实践踩坑
java