springboot整合windows版本redis详解
前言 实际生产中redis已经成为每个项目的标配,这篇文章就是本人在工作中搭建springboot项目整合windows版redis的步骤详解
目录
一、redis的安装
1、下载redis
关于redis的windows版本,redis的官网上没有windows版本的下载口(起码目前没有),所以windows版的redis都是在github上
Github下载地址:https://github.com/tporadowski/redis/releases
CSDN下载地址:https://download.csdn.net/download/weixin_49845828/86226075git可以直接下,但是之能下载redis
CSDN下载下来是个压缩包,里面有redis和可视化工具两个安装包,解压密码123456
2、安装redis
按步骤走就行
3、运行redis
安装完去任务管理器的服务里就可以看到redis的服务了
二、redis的设置
1、设置密码
1、通过安装路径找到redis的文件夹,打开redis.windows.conf和redis.windows-service.conf文件
搜索requirepass,在后面输入你的密码!
2、设置IP访问
还是上面的文件,搜索bind,将下图红框中的一行注释掉
三、redis可视化工具 redis_desktop
1、下载redis_desktop
从CSDN直接下,或者网上搜一大把一大把的
CSDN下载地址:https://download.csdn.net/download/weixin_49845828/86226075
里面有redis和可视化工具两个安装包,解压密码123456
2、安装redis_desktop
安装就按步骤往下点就行
3、连接本地redis
打开 redis-desktop-manager软件,点击下面的Connect to redis server按钮
输入信息连接redis
点击Test Connection 测试连接,通过后点击OK就连上了
四、springboot项目整合redis
1、maven依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、application.yml的配置
spring:
redis:
database: 0
host: ***.***.*.***
port: ****
password: ********
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 4
connect-timeout: 1000
3、redisconfig类
package com.sojn.choose_room.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.net.UnknownHostException;
import java.time.Duration;
@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;
}
}
4、测试
在测试类中引入RedisTemplate
import org.springframework.data.redis.core.RedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
在方法中添加代码,插入一条数据
redisTemplate.opsForValue().set("123", "123");
运行测试方法,看到redis中有了数据
至此windows版的redis就配置完成了