对于刚接触Spring boot data-redis 的同学,与jedis的关系很容易搞混,这里对data-redis于jedis做一个简单的说明,并提供整合方式
1. 区别与关系
jedis
spring-data-redis
它依赖jedis或Lettuce,实际上是对jedis这些客户端的封装,提供一套与客户端无关的api供应用使用,从而你在从一个redis客户端切换为另一个客户端,不需要修改业务代码。
2. spring boot 整合data redis (默认依赖Lettuce)
spring-boot-data-redis 内部实现了对Lettuce和jedis两个客户端的封装,默认使用的是Lettuce
pom.xml
<?xml version="1.0" encoding="UTF-8"?><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.0</modelVersion> <groupId>com.heichong</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </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;@Servicepublic 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;@SpringBootApplicationpublic 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@62417a162018-08-09 09:24:14.839 INFO 7108 --- [ main] io.lettuce.core.EpollProvider : Starting without optional epoll library2018-08-09 09:24:14.840 INFO 7108 --- [ main] io.lettuce.core.KqueueProvider : Starting without optional kqueue library2018-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
<?xml version="1.0" encoding="UTF-8"?><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.0</modelVersion> <groupId>com.heichong</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!-- 排除lettuce包,使用jedis代替--> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <!-- jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </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@60d1a32f2018-08-09 09:34:29.553 INFO 7131 --- [ main] com.heichong.demo.redis.SimpleService : 从redis中获取key=user的值为:张三
从输出的工厂类为
JedisConnectionFactory
我们就可以看出,redis客户端已经变为jedis
作者:heichong
链接:https://www.jianshu.com/p/c7b4cd47ad65