Spring Boot整合 NoSQL 数据库 Redis_springboot大数据项目nosql依赖

org.apache.commons commons-pool2 2.6.0

![在这里插入图片描述](https://img-blog.csdnimg.cn/24a45d33b61f473099e91b1fc8f7a689.png)


## 四、Reds相关配置


将redis相关的依赖引入到项目中之后,需要对redis进行一些配置,在`application.properties`配置redis:



Redis服务器地址

spring.redis.host=自己搭建的redis服务器的 IP

Redis服务器连接端口

spring.redis.port=6379

Redis数据库索引(默认为0)

spring.redis.database= 0

连接超时时间(毫秒)

spring.redis.timeout=1800000

连接池最大连接数(使用负值表示没有限制)

spring.redis.lettuce.pool.max-active=20

最大阻塞等待时间(负数表示没限制)

spring.redis.lettuce.pool.max-wait=-1

连接池中的最大空闲连接

spring.redis.lettuce.pool.max-idle=5

连接池中的最小空闲连接

spring.redis.lettuce.pool.min-idle=0


![在这里插入图片描述](https://img-blog.csdnimg.cn/3e02a7b1c0964cecbaf25719f87a3e38.png)


## 五、添加Redis配置类


**对Redis相关配置完成后,添加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);
    //解决查询缓存转换异常的问题
    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;
}

}


![在这里插入图片描述](https://img-blog.csdnimg.cn/909dcf56dd9b4ddd935d5f154e89390e.png)


## 六、测试一下


将所有的环境依赖和配置搭建完成之后,进行测试一把。


① 首先,确保安装`Redis`的服务器已经启动Redis服务


![在这里插入图片描述](https://img-blog.csdnimg.cn/4b7f470b64934a3b9e3b424175ed4201.png)


② 编写`controller`类,前提需要引入`Spring Boot Web` 的依赖:


![在这里插入图片描述](https://img-blog.csdnimg.cn/8aefcd30f2054a44b910adde7d301462.png)



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(){
    // 设置值到reids
    redisTemplate.opsForValue().set("name","jack");
    // 从redis中获取值
    String name = (String)redisTemplate.opsForValue().get("name");

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值