Redis从入门到实践:Java开发者指南

一、Redis简介

1.1 什么是Redis?

Redis(Remote Dictionary Server)是一个开源的、基于内存的键值对存储系统,支持多种数据结构,提供持久化功能。因其高性能(10万+ QPS)和丰富特性,被广泛应用于缓存、队列、会话管理等场景。

1.2 核心特性

  • 支持String/Hash/List/Set/ZSet等数据结构

  • 数据持久化(RDB/AOF)

  • 主从复制与高可用(Sentinel)

  • 分布式集群(Cluster)

  • Lua脚本支持

  • 发布订阅模型

    二、环境搭建

    2.1 安装Redis(Linux示例)

    wget https://download.redis.io/releases/redis-7.0.12.tar.gz
    tar xzf redis-7.0.12.tar.gz
    cd redis-7.0.12
    make && make install
    redis-server --daemonize yes

    2.2 Java客户端选择

  • Jedis:同步阻塞式客户端

  • Lettuce:基于Netty的异步客户端

  • Redisson:分布式服务扩展

  • Spring Data Redis:Spring生态集成

    三、Java集成实战

    3.1 添加Maven依赖

    
    
        redis.clients
        jedis
        4.4.3
    
    
    
    
        org.springframework.boot
        spring-boot-starter-data-redis
    

    3.2 基础操作示例

    public class JedisDemo {
        public static void main(String[] args) {
            try (Jedis jedis = new Jedis("localhost", 6379)) {
                // String操作
                jedis.set("user:1001", "张三");
                System.out.println(jedis.get("user:1001")); // 输出:张三
                
                // Hash操作
                jedis.hset("product:2001", "name", "手机");
                jedis.hset("product:2001", "price", "3999");
                Map product = jedis.hgetAll("product:2001");
                System.out.println(product); // {name=手机, price=3999}
                
                // List操作
                jedis.lpush("task_queue", "task1", "task2");
                String task = jedis.rpop("task_queue");
                System.out.println(task); // 输出:task1
            }
        }
    }

    四、Spring Boot整合

    4.1 配置RedisTemplate

    # application.yml
    spring:
      redis:
        host: localhost
        port: 6379
        password: 
        database: 0
    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate template = new RedisTemplate<>();
            template.setConnectionFactory(factory);
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return template;
        }
    }

    4.2 缓存应用示例

    @Service
    public class UserService {
        
        @Autowired
        private RedisTemplate redisTemplate;
        
        private static final String USER_CACHE_PREFIX = "user:";
        
        public User getUserById(Long id) {
            String key = USER_CACHE_PREFIX + id;
            User user = (User) redisTemplate.opsForValue().get(key);
            if (user == null) {
                user = getUserFromDB(id); // 模拟数据库查询
                redisTemplate.opsForValue().set(key, user, 30, TimeUnit.MINUTES);
            }
            return user;
        }
        
        @Cacheable(value = "users", key = "#id")
        public User getUserWithCache(Long id) {
            return getUserFromDB(id);
        }
    }

    五、高级应用场景

    5.1 分布式锁实现

    public class RedisLock {
        private static final String LOCK_PREFIX = "lock:";
        private static final long EXPIRE_TIME = 30000; // 30秒
        
        public boolean tryLock(String key, String requestId) {
            Jedis jedis = new Jedis("localhost");
            try {
                String result = jedis.set(LOCK_PREFIX + key, requestId, 
                    SetParams.setParams().nx().px(EXPIRE_TIME));
                return "OK".equals(result);
            } finally {
                jedis.close();
            }
        }
        
        public boolean releaseLock(String key, String requestId) {
            String luaScript = "if redis.call('get', KEYS[1]) == ARGV[1] then " +
                               "return redis.call('del', KEYS[1]) else return 0 end";
            Jedis jedis = new Jedis("localhost");
            try {
                Object result = jedis.eval(luaScript, 
                    Collections.singletonList(LOCK_PREFIX + key),
                    Collections.singletonList(requestId));
                return result.equals(1L);
            } finally {
                jedis.close();
            }
        }
    }

    5.2 消息队列实现

    // 生产者
    public class MessageProducer {
        public void sendMessage(String queueName, String message) {
            redisTemplate.opsForList().leftPush(queueName, message);
        }
    }
    
    // 消费者(使用BRPOP阻塞获取)
    public class MessageConsumer implements Runnable {
        public void run() {
            while (true) {
                List messages = redisTemplate.execute(
                    (RedisCallback>) connection -> 
                        connection.bRPop(0, "message_queue".getBytes()));
                processMessage(messages.get(1));
            }
        }
    } 

    六、性能优化建议

    1. 连接池配置:合理设置maxTotal/maxIdle参数

      JedisPoolConfig config = new JedisPoolConfig();
      config.setMaxTotal(100);
      config.setMaxIdle(20);
      JedisPool jedisPool = new JedisPool(config, "localhost", 6379);

    2. 管道技术(Pipeline):批量操作提升性能

      Pipeline pipeline = jedis.pipelined();
      for (int i = 0; i < 1000; i++) {
          pipeline.set("key_" + i, "value_" + i);
      }
      pipeline.sync();

    3. Lua脚本:保证原子性操作

      String script = "local count = redis.call('get', KEYS[1]) " +
                     "if count and tonumber(count) > 0 then " +
                     "   redis.call('decr', KEYS[1]) " +
                     "   return 1 " +
                     "else return 0 end";
      Object result = jedis.eval(script, 1, "stock");

    七、运维监控

    7.1 常用监控命令

    • INFO:查看服务器信息

    • MONITOR:实时监控命令

    • SLOWLOG GET:查询慢查询日志

    7.2 Spring Boot Actuator集成

    management:
      endpoints:
        web:
          exposure:
            include: health,metrics,redis

    八、常见问题解决方案 (下期出详细方案)

    1. 缓存穿透:布隆过滤器+空值缓存

    2. 缓存雪崩:随机过期时间+熔断机制

    3. 大Key问题:拆分Key+数据结构优化

    4. 连接泄漏:确保使用try-with-resources关闭连接

    你可能感兴趣的:(redis,redis,java)