找回密码
 立即注册
首页 业界区 业界 AEM6.5集成Redis详细步骤(附代码)

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

髡芯 2025-6-17 15:00:50
一、环境准备


  • Redis 安装
    1. # Ubuntu/Debian系统
    2. sudo apt update
    3. sudo apt install redis-server
    4. # 启动Redis
    5. sudo systemctl start redis-server
    6. # 验证运行状态
    7. sudo systemctl status redis-server
    复制代码
     
   2.配置 Redis 远程访问
  1. # 编辑配置文件
  2. sudo nano /etc/redis/redis.conf
  3. # 找到bind 127.0.0.1 ::1并修改为(生产环境建议设置具体IP)
  4. bind 0.0.0.0
  5. # 启用密码认证(推荐)
  6. requirepass your_redis_password
  7. # 重启Redis
  8. sudo systemctl restart redis-server
复制代码
 
二、AEM 端配置

  1.添加 Redis 客户端依赖

  在 AEM 项目的pom.xml中添加:
  1. <dependency>
  2.     <groupId>redis.clients</groupId>
  3.     jedis</artifactId>
  4.     <version>4.4.3</version>
  5. </dependency>
复制代码
  2.创建 Redis 连接工厂
  1. import redis.clients.jedis.Jedis;
  2. import redis.clients.jedis.JedisPool;
  3. import redis.clients.jedis.JedisPoolConfig;
  4. public class RedisConnectionFactory {
  5.     private static JedisPool jedisPool;
  6.    
  7.     static {
  8.         JedisPoolConfig poolConfig = new JedisPoolConfig();
  9.         poolConfig.setMaxTotal(100);
  10.         poolConfig.setMaxIdle(10);
  11.         poolConfig.setMinIdle(5);
  12.         poolConfig.setTestOnBorrow(true);
  13.         poolConfig.setTestOnReturn(true);
  14.         
  15.         // 替换为实际的Redis服务器信息
  16.         jedisPool = new JedisPool(
  17.             poolConfig,
  18.             "redis-server-ip",
  19.             6379,
  20.             5000,
  21.             "your_redis_password"
  22.         );
  23.     }
  24.    
  25.     public static Jedis getConnection() {
  26.         return jedisPool.getResource();
  27.     }
  28.    
  29.     public static void closeConnection(Jedis jedis) {
  30.         if (jedis != null) {
  31.             jedis.close();
  32.         }
  33.     }
  34. }
复制代码
三、缓存实现示例


  • 创建 Redis 缓存服务
  1. import org.osgi.service.component.annotations.Component;
  2. import redis.clients.jedis.Jedis;
  3. @Component(service = CacheService.class)
  4. public class RedisCacheServiceImpl implements CacheService {
  5.    
  6.     @Override
  7.     public void put(String key, String value) {
  8.         Jedis jedis = null;
  9.         try {
  10.             jedis = RedisConnectionFactory.getConnection();
  11.             jedis.set(key, value);
  12.         } catch (Exception e) {
  13.             // 记录异常日志
  14.             e.printStackTrace();
  15.         } finally {
  16.             RedisConnectionFactory.closeConnection(jedis);
  17.         }
  18.     }
  19.    
  20.     @Override
  21.     public String get(String key) {
  22.         Jedis jedis = null;
  23.         try {
  24.             jedis = RedisConnectionFactory.getConnection();
  25.             return jedis.get(key);
  26.         } catch (Exception e) {
  27.             // 记录异常日志
  28.             e.printStackTrace();
  29.             return null;
  30.         } finally {
  31.             RedisConnectionFactory.closeConnection(jedis);
  32.         }
  33.     }
  34.    
  35.     @Override
  36.     public void delete(String key) {
  37.         Jedis jedis = null;
  38.         try {
  39.             jedis = RedisConnectionFactory.getConnection();
  40.             jedis.del(key);
  41.         } catch (Exception e) {
  42.             // 记录异常日志
  43.             e.printStackTrace();
  44.         } finally {
  45.             RedisConnectionFactory.closeConnection(jedis);
  46.         }
  47.     }
  48. }
复制代码
    2.缓存服务接口
  1. public interface CacheService {
  2.     void put(String key, String value);
  3.     String get(String key);
  4.     void delete(String key);
  5. }
复制代码
四、AEM Dispatcher 配置

在dispatcher.any中添加 Redis 缓存规则:
  1. /vanityurl {
  2.   /docroot "/content"
  3.   /handler "/libs/granite/dispatcher/content/vanity"
  4.   /invalidate {
  5.     /0001 { /type "path" /glob "/content/**" }
  6.   }
  7.   /cache {
  8.     /docroot "/content"
  9.     /rules {
  10.       /0001 { /type "allow" /glob "*" }
  11.     }
  12.     /headers {
  13.       /0001 { /type "allow" /name "Content-Type" }
  14.     }
  15.     /invalidate {
  16.       /0001 { /type "path" /glob "/content/**" }
  17.     }
  18.     /redis {
  19.       /host "redis-server-ip"
  20.       /port 6379
  21.       /password "your_redis_password"
  22.       /timeout 5000
  23.     }
  24.   }
  25. }
复制代码
五、验证集成


  • Redis CLI 测试
  1. redis-cli -h redis-server-ip -a your_redis_password
  2. ping  # 应返回PONG
复制代码
    2.AEM 代码测试
  1. public class CacheTest {
  2.     public static void main(String[] args) {
  3.         CacheService cacheService = new RedisCacheServiceImpl();
  4.         
  5.         // 存入缓存
  6.         cacheService.put("testKey", "Hello Redis!");
  7.         
  8.         // 获取缓存
  9.         String value = cacheService.get("testKey");
  10.         System.out.println("缓存值: " + value);
  11.         
  12.         // 删除缓存
  13.         cacheService.delete("testKey");
  14.     }
  15. }
复制代码
六、生产环境建议


  • 安全加固

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

  • 性能优化

    • 配置 Redis 内存策略
      1. # 在redis.conf中添加
      2. maxmemory 256mb
      3. maxmemory-policy allkeys-lru
      复制代码
    • 使用 Redis 集群提高可用性

 
    3.监控与告警
     ○ 集成 Prometheus+Grafana 监控 Redis 性能
    ○ 设置内存使用率、连接数等告警阈值
  通过以上步骤,你可以成功将 AEM 6.5 与最新版 Redis 集成,实现高性能缓存机制。 
问题:

一、修改绑定地址这一步,需要端口号吗?

在 Redis 配置中修改绑定地址(bind参数)时,不需要指定端口号。bind参数仅用于配置 Redis 服务器监听的 IP 地址,端口号由单独的port参数控制(默认是 6379)。
详细说明:
1.bind 参数作用
该参数用于限制 Redis 服务器只接受来自指定 IP 地址的连接请求。例如:
  1. # 仅监听本地回环地址(默认值)
  2. bind 127.0.0.1
  3. # 监听所有可用网络接口(不推荐生产环境)
  4. bind 0.0.0.0
  5. # 监听特定IP地址(推荐生产环境)
  6. bind 192.168.1.100
复制代码
2。port 参数作用
该参数用于指定 Redis 服务器监听的端口号,默认是 6379。例如:
  1. port 6379
复制代码
3.配置示例
如果需要 Redis 监听特定 IP 和端口,应分别设置这两个参数:
  1. # 监听特定IP地址
  2. bind 192.168.1.100
  3. # 使用非标准端口
  4. port 6380
复制代码
注意事项:
安全风险:bind 0.0.0.0会使 Redis 暴露在公网,存在安全风险,生产环境建议绑定具体内网 IP。
防火墙配置:如果修改了默认端口,需确保防火墙开放相应端口。
重启生效:修改配置后需重启 Redis 服务:
  1. sudo systemctl restart redis-server
复制代码
验证配置:
可以通过以下命令检查 Redis 监听的 IP 和端口:
  1. # 查看Redis进程监听情况
  2. sudo netstat -tulpn | grep redis
  3. # 输出示例(显示监听127.0.0.1:6379)
  4. tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      1234/redis-server 1
复制代码
因此,在修改 Redis 绑定地址时,只需指定 IP 地址,无需包含端口号。
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册