栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring boot

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot

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


    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@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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号