springboot 整合 redis 需要注意依赖与版本问题

1、redis服务端

首先你得安装一个redis服务端:

1.1、windows下安装

下载地址:https://github.com/tporadowski/redis/releases。
cmd输入指令

# 启动服务端
redis-server.exe redis.windows.conf
# 启动客户端
redis-cli

1.2、linux下安装

下载地址:http://redis.io/download
使用指令:下载、安装、启动

wget http://download.redis.io/releases/redis-6.0.8.tar.gz
tar xzf redis-6.0.8.tar.gz
cd redis-6.0.8
make

cd src
./redis-server

cd src
./redis-cli

2、spring-redis的几种依赖与版本问题

在spring与redis整合时有下面几种:
1、spring-boot-starter-data-redis
2、spring-boot-starter-redis
3、spring-data-redis

1、2中都包含有3

springboot版本可使用依赖
1.5.0之后必须是 spring-boot-starter-data-redis
1.5.0-1.4.0之间用spring-boot-starter-data-redis和spring-boot-starter-redis都可以
1.4.0之前必须是 spring-boot-starter-redis

到目前(2021年4月)都已经更新到了 spring-boot 2.5以后了,所以基本都是使用 spring-boot-starter-data-redis

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.4.0</version>
</dependency>

3、redis的几种客户端实现

实现方式区别
Jedisjava实现,使用同步和阻塞IO的方式,提供比较全面的redis原生指令的支持,上层封装比较弱,集群特性支持度低
lettuce基于netty,异步,支持各种模式的redis连接和操作,在一些分布式缓存框架上使用比较多
Redisson基于netty,异步,支持各种模式的redis连接和操作,同时提供一大堆的实用功能
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是SpringBoot整合Redis的步骤: 1. 在pom.xml中引入Redis依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties或application.yml中配置Redis连接信息,例如: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 ``` 3. 创建RedisConfig类,配置RedisTemplate和StringRedisTemplate: ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(redisConnectionFactory); return stringRedisTemplate; } } ``` 4. 在需要使用Redis的地方注入RedisTemplate或StringRedisTemplate,并使用相应方法操作Redis,例如: ``` @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } ``` 以上就是SpringBoot整合Redis的基本步骤,希望能够帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值