Springdata redis与jedis关系

注:本文转自https://www.jianshu.com/p/c7b4cd47ad65

对于刚接触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


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.heichonggroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

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

3. spring boot 整合data redis (jedis)

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

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.heichonggroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
            
            <exclusions>
                <exclusion>
                    <groupId>io.lettucegroupId>
                    <artifactId>lettuce-coreartifactId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.0version>
        dependency>


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

再次运行日志如下

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

你可能感兴趣的:(JAVA)