SpringBoot2 集成 redis

SpringBoot-Redis

spring boot 整合 redis, 实现 redis 的基本存储以及订阅模式
源码位置: https://github.com/DongCarzy/springboot-redis

流程

  • clone code
git clone https://github.com/DongCarzy/springboot-redis.git
  • 安装 Redis
  • 启动 项目

说明文档

  • spring-boot 2.0.4.RELEASE
  • spring-boot-starter-data-redis
  • redis.clients

pom.xml

     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.0.4.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>

     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
     </properties>

     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>redis.clients</groupId>
             <artifactId>jedis</artifactId>
             <type>jar</type>
             <scope>compile</scope>
         </dependency>
     </dependencies>

自定义序列化方案

springBoot的redisTemplate默认采用的JDK的序列化 JdkSerializationRedisSerializer, 可在源码RedisTemplate.class 中看到 defaultSerializer 的默认是正是 JdkSerializationRedisSerializer ,占用空间较大,且识别度不好.因此我们自定义. 这里采用的是 GenericJackson2JsonRedisSerializer
可以认为它是jackson的升级版本,在序列化的过程种会将Object的类型一起存储起来.

默认 RedisTemplate 声明在 RedisAutoConfiguration.class 这个类中
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
   
    public 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值