【项目搭建三】SpringBoot引入redis

添加依赖

本文使用spring data redis访问和操作redis,pom文件中加入以下依赖:

XML 复制代码
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>

application.yml配置

application.yml中添加以下配置:

bash 复制代码
redis:
  host: 127.0.0.1        # 服务地址
  port: 6379             # 服务端口
  password: gusy1234     # 服务密码
  database: 0            # 数据库索引,默认0
  # 连接池配置,使用lettuce,可选jedis
  lettuce:
    pool:
      max-active: 8        # 连接池最大连接数
      max-idle: 8          # 连接池最大空闲连接数
      max-wait: -1         # 连接池最大阻塞等待时间,负值表示没有限制         
      min-idle: 0          # 连接池最小空闲连接数
  timeout: 1000        # 连接超时时间(毫秒)

**注:**spring data redis中,可以配置lettuce或jedis作为客户端的连接池。springBoot 2.x默认使用的是lettuce。如果你使用jedis作为连接池,需定义一个JedisConnectionFactory注入到RedisTemplate中,但是这种方式在springBoot 2.x中已经过时,不建议使用。

测试用例

使用RedisTemplate操作redis,以下是测试用例:

java 复制代码
@SpringBootTest
class AgedWebApplicationTests {

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @Test
    public void redisTest() {
        redisTemplate.boundValueOps("testKey").set("testValue");
        System.out.println(redisTemplate.boundValueOps("testKey").get());
    }
}

进入redis desktop manage查看保存结果:

可以看到value显示为二进制,这是由于redisTemplate默认使用jdk序列化导致的,这种方式生成的数据较大,性能较低,且只有Java应用能够反序列化。本文将用fastjson2作为序列化方式。

fastjson2作为序列化方式

pom文件中引入fastjson2:

XML 复制代码
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>2.0.45</version>
</dependency>

增加RedisConfig配置类:

java 复制代码
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置
 *
 * @Author: gusy
 */
@Slf4j
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 使用Fastjson2序列化器
        GenericFastJsonRedisSerializer redisSerializer = new GenericFastJsonRedisSerializer();
        // 设置 value 的序列化规则和 key 的序列化规则
        template.setValueSerializer(redisSerializer);
        template.setHashValueSerializer(redisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        log.info("redis自定义配置启动成功!");
        return template;
    }
}

再次执行测试用例,进入redis desktop manage查看结果:

好了,配置完成。

拓展:

java 复制代码
RedisTemplat常用序列化方式:
1、JdkSerializationRedisSerializer:使用Java的序列化功能。这是默认的序列化机制,它使用标准的Java序列化方式。
2、StringRedisSerializer:字符串序列化,用于键(key)和值(value)都是字符串的情况。
3、Jackson2JsonRedisSerializer:使用Jackson库将对象序列化为JSON字符串。同fastjson序列化方式。
4、GenericJackson2JsonRedisSerializer:是Jackson2JsonRedisSerializer的泛型版本,可以方便地序列化多种类型,而不需要为每种类型提供类型信息。
5、GenericToStringSerializer:可以将对象序列化为字符串。
相关推荐
苹果酱05678 分钟前
一文读懂SpringCLoud
java·开发语言·spring boot·后端·中间件
掐指一算乀缺钱29 分钟前
SpringBoot 数据库表结构文档生成
java·数据库·spring boot·后端·spring
晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑34 分钟前
苍穹外卖学习笔记(七)
java·windows·笔记·学习·mybatis
就这个java爽!39 分钟前
JAVA网络编程【基于TCP和UDP协议】超详细!!!
java·开发语言·网络·tcp/ip·udp·eclipse·idea
一叶飘零_sweeeet43 分钟前
为什么 Feign 要用 HTTP 而不是 RPC?
java·网络协议·http·spring cloud·rpc·feign
飞翔的佩奇1 小时前
xxl-job适配sqlite本地数据库及mysql数据库。可根据配置指定使用哪种数据库。
数据库·spring boot·mysql·sqlite·xxl-job·任务调度
懒洋洋大魔王1 小时前
7.Java高级编程 多线程
java·开发语言·jvm
芊言芊语1 小时前
分布式缓存服务Redis版解析与配置方式
redis·分布式·缓存
茶馆大橘1 小时前
【黑马点评】已解决java.lang.NullPointerException异常
java·开发语言
星辰@Sea1 小时前
服务注册中心对比及使用场景分析
java·云原生