欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成Lettuce客戶端操作Redis的實(shí)現(xiàn)

 更新時(shí)間:2023年11月10日 14:50:15   作者:kerwin_code  
本文主要介紹了SpringBoot集成Lettuce客戶端操作Redis的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、前言

spring-boot-starter-data-redis有兩種實(shí)現(xiàn) lettuce 和 jedis,spring boot 2的spring-boot-starter-data-redis中,默認(rèn)使用的是lettuce作為redis客戶端,也推薦使用lettuce,它與jedis的主要區(qū)別如下

  • Jedis
    • Jedis是同步的,不支持異步,Jedis客戶端實(shí)例不是線程安全的,需要每個(gè)線程一個(gè)Jedis實(shí)例,所以一般通過連接池來使用Jedis.
    • 優(yōu)點(diǎn)
      • 提供了比較全面的 Redis 操作特性的 API
      • API 基本與 Redis 的指令一一對(duì)應(yīng),使用簡(jiǎn)單易理解
    • 缺點(diǎn)
      • 同步阻塞 IO
      • 不支持異步
      • 線程不安全
  • Lettuce
    • Lettuce是基于Netty框架的事件驅(qū)動(dòng)的Redis客戶端,其方法調(diào)用是異步的,Lettuce的API也是線程安全的,所以多個(gè)線程可以操作單個(gè)Lettuce連接來完成各種操作,同時(shí)Lettuce也支持連接池.
    • 優(yōu)點(diǎn)
      • 線程安全
      • 基于 Netty 框架的事件驅(qū)動(dòng)的通信,支持異步和響應(yīng)式編程
      • 適用于分布式緩存
      • 支持集群,Sentinel,管道和編碼器等等功能
    • 缺點(diǎn)
      • API 更抽象,學(xué)習(xí)使用成本高,不過我們基本都使用的RedisTemplate來操作Redis,它抽象了Jedis或者Lettuce客戶端,底層實(shí)現(xiàn)我們可以不用關(guān)心

二、基礎(chǔ)集成配置(redis單節(jié)點(diǎn))

工程結(jié)構(gòu)

2.1、POM

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--springboot中的redis依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- lettuce pool 緩存連接池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!-- 使用jackson作為redis數(shù)據(jù)序列化 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.4</version>
        </dependency>
		<!-- SpringBoot測(cè)試包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.2、添加配置文件application.yml

因?yàn)槲覀冇玫膕pring-boot-starter-data-redis包會(huì)自動(dòng)配置redis連接,在配置文件中添加對(duì)應(yīng)配置即可

spring:
  #redis配置信息
  redis:
    ## Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0)
    database: 0
    ## Redis服務(wù)器地址
    host: 172.16.8.169
    ## Redis服務(wù)器連接端口
    port: 6379
    ## Redis服務(wù)器連接密碼(默認(rèn)為空)
    password: 123456
    ## 連接超時(shí)時(shí)間(毫秒)
    timeout: 1200
    lettuce:
      pool:
        ## 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
        max-active: 8
        ## 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
        max-wait: -1
        ## 連接池中的最大空閑連接
        max-idle: 8
        ## 連接池中的最小空閑連接
        min-idle: 1

2.3、編寫配置文件

這里需要添加一個(gè)RedisTemplate的bean,設(shè)置這個(gè)RedisTemplate序列化方式為jackson

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig{
    /**
     * retemplate相關(guān)配置
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置連接工廠
        template.setConnectionFactory(factory);
        //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,比如String,Integer等會(huì)跑出異常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);
        // 值采用json序列化
        template.setValueSerializer(jacksonSeial);
        //使用StringRedisSerializer來序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        // 設(shè)置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();
        return template;
    }
}

2.4、編寫啟動(dòng)類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LettuceApplication {
    public static void main(String[] args) {
        SpringApplication.run(LettuceApplication.class);
    }
}

2.5、編寫測(cè)試類測(cè)試是否連接成功

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes =  LettuceApplication.class)
public class LettuceTest {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Test
    public void t1(){
        String key = "key1";
        System.out.println("插入數(shù)據(jù)到redis");
        redisTemplate.opsForValue().set(key,"value1");
        Object value = redisTemplate.opsForValue().get(key);
        System.out.println("從redis中獲取到值為 "+value);
        Boolean delete = redisTemplate.delete(key);
        System.out.println("刪除redis中值 "+delete);
    }
}

到此這篇關(guān)于SpringBoot集成Lettuce客戶端操作Redis的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Lettuce操作Redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mybatis的dtd約束文件及配置文件xml自動(dòng)提示操作

    mybatis的dtd約束文件及配置文件xml自動(dòng)提示操作

    這篇文章主要介紹了mybatis的dtd約束文件及配置文件xml自動(dòng)提示操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 使用Spring Data Redis實(shí)現(xiàn)數(shù)據(jù)緩存的方法

    使用Spring Data Redis實(shí)現(xiàn)數(shù)據(jù)緩存的方法

    目前在系統(tǒng)架構(gòu)設(shè)計(jì)中使用Redis實(shí)現(xiàn)緩存,這篇文章主要介紹了使用Spring Data Redis實(shí)現(xiàn)數(shù)據(jù)緩存的方法,具有一定的參考價(jià)值,需要的朋友可以參考下
    2018-11-11
  • Mybatis傳遞多個(gè)參數(shù)進(jìn)行SQL查詢的用法

    Mybatis傳遞多個(gè)參數(shù)進(jìn)行SQL查詢的用法

    本文給大家介紹Mybatis傳遞多個(gè)參數(shù)進(jìn)行SQL查詢的用法的相關(guān)知識(shí),本文還給大家介紹了mybatis通過Map傳遞多個(gè)參數(shù)和JavaBean傳遞多個(gè)參數(shù),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-06-06
  • mybatis源碼解讀之executor包語(yǔ)句處理功能

    mybatis源碼解讀之executor包語(yǔ)句處理功能

    這篇文章主要介紹了executor包語(yǔ)句處理功能,mybatis中支持三種語(yǔ)句類型,不同語(yǔ)句類型支持的變量符號(hào)不同,下文詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-02-02
  • Java使用POI-TL和JFreeChart動(dòng)態(tài)生成Word報(bào)告

    Java使用POI-TL和JFreeChart動(dòng)態(tài)生成Word報(bào)告

    本文介紹了使用POI-TL和JFreeChart生成包含動(dòng)態(tài)數(shù)據(jù)和圖表的Word報(bào)告的方法,并分享了實(shí)際開發(fā)中的踩坑經(jīng)驗(yàn),通過代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2025-02-02
  • 淺析Java中XPath和JsonPath以及SpEL的用法與對(duì)比

    淺析Java中XPath和JsonPath以及SpEL的用法與對(duì)比

    XPath,即XML路徑語(yǔ)言,是一種用于在XML文檔中查找信息的語(yǔ)言,JsonPath是從XPath中發(fā)展而來的,專門用于JSON數(shù)據(jù)格式,本文主要來講講他們的用法與區(qū)別,需要的可以參考下
    2023-11-11
  • Java?C++題解leetcode672燈泡開關(guān)示例

    Java?C++題解leetcode672燈泡開關(guān)示例

    這篇文章主要為大家介紹了Java?C++題解leetcode672燈泡開關(guān)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 在Spring 中使用@Aspect 控制自定義注解的操作

    在Spring 中使用@Aspect 控制自定義注解的操作

    這篇文章主要介紹了在Spring 中使用@Aspect 控制自定義注解的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 徹底搞懂Java多線程(二)

    徹底搞懂Java多線程(二)

    這篇文章主要給大家介紹了關(guān)于Java面試題之多線程和高并發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • ElasticSearch學(xué)習(xí)之文檔API相關(guān)操作

    ElasticSearch學(xué)習(xí)之文檔API相關(guān)操作

    這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之文檔API相關(guān)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01

最新評(píng)論