最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Boot整合?NoSQL?數(shù)據(jù)庫?Redis詳解

 更新時間:2022年09月13日 10:51:17   作者:歐子有話說  
這篇文章主要為大家介紹了Spring?Boot整合?NoSQL?數(shù)據(jù)庫?Redis詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

引言

在日常的開發(fā)中,除了使用 Spring Boot 這個企業(yè)級快速構(gòu)建項目的框架之外,隨著業(yè)務(wù)數(shù)據(jù)量的大幅度增加,對元數(shù)據(jù)庫造成的壓力成倍劇增。在此背景下, Redis 這個 NoSQL 數(shù)據(jù)庫已然整個項目架構(gòu)中的不可或缺的一部分,懂得如何 Spring Boot 整合 Redis ,是當(dāng)今開發(fā)人員必備的一項技能,接下來對整合步驟進(jìn)行詳細(xì)說明。

一、環(huán)境準(zhǔn)備

在開始開發(fā)之前,我們需要準(zhǔn)備一些環(huán)境配置:

  • jdk 1.8 或其他更高版本
  • 開發(fā)工具 IDEA
  • 管理依賴 Maven
  • Redis環(huán)境,推薦linux系統(tǒng)中搭建redis環(huán)境

二、構(gòu)建Spring Boot項目

打開 idea -> file -> Nwe -> Project ,如圖,勾選填寫相關(guān)的配置信息:

勾選一些初始化的依賴配置:

Spring Boot項目初始化完成。

三、引入Redis依賴

構(gòu)建完成Spring Boot項目工程之后,需要在 pom.xml 文件中引入 redis 相關(guān)依賴

<!-- redis -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
	<version>2.6.0</version>
</dependency>

四、Reds相關(guān)配置

將redis相關(guān)的依賴引入到項目中之后,需要對redis進(jìn)行一些配置,在 application.properties配置redis:

# Redis服務(wù)器地址
spring.redis.host=自己搭建的redis服務(wù)器的 IP
# Redis服務(wù)器連接端口
spring.redis.port=6379
# Redis數(shù)據(jù)庫索引(默認(rèn)為0)
spring.redis.database= 0
# 連接超時時間(毫秒)
spring.redis.timeout=1800000
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.lettuce.pool.max-active=20
# 最大阻塞等待時間(負(fù)數(shù)表示沒限制)
spring.redis.lettuce.pool.max-wait=-1
# 連接池中的最大空閑連接
spring.redis.lettuce.pool.max-idle=5
# 連接池中的最小空閑連接
spring.redis.lettuce.pool.min-idle=0

五、添加Redis配置類

對Redis相關(guān)配置完成后,添加Redis配置類,(拿來即用):

package com.zhao.demo.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
 * @author xiaoZhao
 * @date 2022/9/6
 * @describe
 */
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(factory);
        //key序列化方式
        template.setKeySerializer(redisSerializer);
        //value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //解決查詢緩存轉(zhuǎn)換異常的問題
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 配置序列化(解決亂碼的問題),過期時間600秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
}

六、測試一下

將所有的環(huán)境依賴和配置搭建完成之后,進(jìn)行測試一把。

① 首先,確保安裝 Redis 的服務(wù)器已經(jīng)啟動Redis服務(wù)

② 編寫 controller 類,前提需要引入 Spring Boot Web 的依賴:

package com.zhao.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author xiaoZhao
 * @date 2022/9/6
 * @describe
 */
@RestController
@RequestMapping("/redistest")
public class RedisTestController {
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping
    public String testRedis(){
        // 設(shè)置值到reids
        redisTemplate.opsForValue().set("name","jack");
        // 從redis中獲取值
        String name = (String)redisTemplate.opsForValue().get("name");
        return name;
    }
}

③ 啟動Spring Boot工程,在瀏覽器上向接口發(fā)送請求:

項目啟動成功,向 /redistest 接口發(fā)送請求

請求發(fā)送成功,獲取到數(shù)據(jù),測試成功,至此Spring Boot整合 Redis所有步驟已經(jīng)完成,更多關(guān)于SpringBoot整合NoSQL Redis的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring Boot中使用Activiti的方法教程(二)

    Spring Boot中使用Activiti的方法教程(二)

    工作流(Workflow),就是“業(yè)務(wù)過程的部分或整體在計算機(jī)應(yīng)用環(huán)境下的自動化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • Java泛型與數(shù)據(jù)庫應(yīng)用實例詳解

    Java泛型與數(shù)據(jù)庫應(yīng)用實例詳解

    這篇文章主要介紹了Java泛型與數(shù)據(jù)庫應(yīng)用,結(jié)合實例形式詳細(xì)分析了java繼承泛型類實現(xiàn)增刪改查操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-08-08
  • Java JVM調(diào)優(yōu)五大技能詳解

    Java JVM調(diào)優(yōu)五大技能詳解

    這篇文章主要為大家介紹了JVM調(diào)優(yōu)的五大技能,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • java開發(fā)BeanUtils類解決實體對象間賦值

    java開發(fā)BeanUtils類解決實體對象間賦值

    這篇文章主要為大家介紹了java開發(fā)中使用BeanUtils類實現(xiàn)實體對象之間的賦值有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步學(xué)有所得
    2021-10-10
  • Java中常見的語法糖分享

    Java中常見的語法糖分享

    Java語法糖是指Java編譯器在編譯Java源代碼時所做的一些特殊處理,使得Java源代碼在編譯后生成的字節(jié)碼更加簡潔、易讀、易維護(hù),Java 中有許多常見的語法糖,本文給大家列舉了一些常見的例子,需要的朋友可以參考下
    2023-10-10
  • Java8中的LocalDateTime你會使用了嗎

    Java8中的LocalDateTime你會使用了嗎

    LocalDateTime?是?Java?8?中日期時間?API?提供的一個類,在日期和時間的表示上提供了更加豐富和靈活的支持,本文就來講講LocalDateTime的一些具體使用方法吧
    2023-05-05
  • IDEA創(chuàng)建maven項目時在tomcat運(yùn)行瀏覽器404的問題

    IDEA創(chuàng)建maven項目時在tomcat運(yùn)行瀏覽器404的問題

    這篇文章主要介紹了IDEA創(chuàng)建maven項目時在tomcat運(yùn)行瀏覽器404的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Java SpringMVC數(shù)據(jù)響應(yīng)超詳細(xì)講解

    Java SpringMVC數(shù)據(jù)響應(yīng)超詳細(xì)講解

    Spring?MVC?是?Spring?提供的一個基于?MVC?設(shè)計模式的輕量級?Web?開發(fā)框架,本質(zhì)上相當(dāng)于?Servlet,Spring?MVC?角色劃分清晰,分工明細(xì),本章來講解SpringMVC數(shù)據(jù)響應(yīng)
    2022-04-04
  • java線程池的四種創(chuàng)建方式詳細(xì)分析

    java線程池的四種創(chuàng)建方式詳細(xì)分析

    這篇文章主要介紹了java線程池的四種創(chuàng)建方式詳細(xì)分析,連接池是創(chuàng)建和管理一個連接的緩沖池的技術(shù),這些連接準(zhǔn)備好被任何需要它們的線程使用
    2022-07-07
  • Java項目實現(xiàn)尋找迷宮出路

    Java項目實現(xiàn)尋找迷宮出路

    這篇文章主要為大家詳細(xì)介紹了Java項目實現(xiàn)尋找迷宮出路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05

最新評論

兴化市| 锡林郭勒盟| 上思县| 治多县| 奇台县| 嘉黎县| 鄂托克前旗| 杭锦旗| 增城市| 昌宁县| 临泉县| 铁岭市| 辉县市| 长垣县| 虹口区| 南雄市| 来凤县| 三门县| 双流县| 荃湾区| 奉新县| 扬中市| 读书| 木里| 泽库县| 曲麻莱县| 瓦房店市| 通河县| 建始县| 肥东县| 怀柔区| 互助| 平谷区| 平定县| 怀来县| 会泽县| 黔江区| 交城县| 弥渡县| 呼图壁县| 保德县|