Spring boot - data-redis与jedis关系

对于刚接触Spring boot data-redis 的同学,与jedis的关系很容易搞混,这里对data-redis于jedis做一个简单的说明,并提供整合方式

1. 区别与关系


  • jedis

jedis是redis的java客户端,通过它可以对redis进行操作。与之功能相似的还包括:Lettuce等

  • spring-data-redis

它依赖jedis或Lettuce,实际上是对jedis这些客户端的封装,提供一套与客户端无关的api供应用使用,从而你在从一个redis客户端切换为另一个客户端,不需要修改业务代码。

2. spring boot 整合data redis (默认依赖Lettuce)

spring-boot-data-redis 内部实现了对Lettuce和jedis两个客户端的封装,默认使用的是Lettuce

  • pom.xml


    4.0.0

    com.heichong
    demo
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



  • application.yml
spring:
  redis:
    database: 0
    host: 192.168.1.191
    port: 6379

  • SimpleService redis测试类
package com.heichong.demo.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class SimpleService {
    static Logger logger = LoggerFactory.getLogger(SimpleService.class) ;

    @Autowired
    StringRedisTemplate stringRedisTemplate ;

    public void run(){
        logger.info("redis连接工厂:{}",stringRedisTemplate.getConnectionFactory());

        //添加key
        stringRedisTemplate.opsForValue().set("user","张三");
        //获取key
        logger.info("从redis中获取key=user的值为:{}",stringRedisTemplate.opsForValue().get("user"));

        //删除key
        stringRedisTemplate.delete("user");

    }
}

  • DemoApplication 启动类
package com.heichong.demo;

import com.heichong.demo.redis.SimpleService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);

        SimpleService simpleService = ctx.getBean(SimpleService.class) ;
        simpleService.run();
    }
}

  • 运行日志如下
2018-08-09 09:24:14.726  INFO 7108 --- [           main] com.heichong.demo.redis.SimpleService    : redis连接工厂:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory@62417a16
2018-08-09 09:24:14.839  INFO 7108 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2018-08-09 09:24:14.840  INFO 7108 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
2018-08-09 09:24:14.955  INFO 7108 --- [           main] com.heichong.demo.redis.SimpleService    : 从redis中获取key=user的值为:张三

从输出的工厂类为LettuceConnectionFactory我们就可以看出,默认使用的Lettuce

2. spring boot 整合data redis (jedis)

只需要修改pom,业务代码都不需要修改

  • pom.xml


    4.0.0

    com.heichong
    demo
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-redis
            
            
                
                    io.lettuce
                    lettuce-core
                
            
        
        
        
            redis.clients
            jedis
            2.9.0
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



  • 再次运行日志如下
2018-08-09 09:34:29.500  INFO 7131 --- [           main] com.heichong.demo.redis.SimpleService    : redis连接工厂:org.springframework.data.redis.connection.jedis.JedisConnectionFactory@60d1a32f
2018-08-09 09:34:29.553  INFO 7131 --- [           main] com.heichong.demo.redis.SimpleService    : 从redis中获取key=user的值为:张三

从输出的工厂类为JedisConnectionFactory我们就可以看出,redis客户端已经变为jedis

你可能感兴趣的:(Spring boot - data-redis与jedis关系)