添加依賴
本文使用spring data redis訪問和操作redis,pom文件中加入以下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml配置
application.yml中添加以下配置:
redis:
host: 127.0.0.1 # 服務地址
port: 6379 # 服務端口
password: gusy1234 # 服務密碼
database: 0 # 數(shù)據(jù)庫索引,默認0
# 連接池配置,使用lettuce,可選jedis
lettuce:
pool:
max-active: 8 # 連接池最大連接數(shù)
max-idle: 8 # 連接池最大空閑連接數(shù)
max-wait: -1 # 連接池最大阻塞等待時間,負值表示沒有限制
min-idle: 0 # 連接池最小空閑連接數(shù)
timeout: 1000 # 連接超時時間(毫秒)
注:spring data redis中,可以配置lettuce或jedis作為客戶端的連接池。springBoot 2.x默認使用的是lettuce。如果你使用jedis作為連接池,需定義一個JedisConnectionFactory注入到RedisTemplate中,但是這種方式在springBoot 2.x中已經(jīng)過時,不建議使用。
測試用例
使用RedisTemplate操作redis,以下是測試用例:
@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查看保存結(jié)果:
可以看到value顯示為二進制,這是由于redisTemplate默認使用jdk序列化導致的,這種方式生成的數(shù)據(jù)較大,性能較低,且只有Java應用能夠反序列化。本文將用fastjson2作為序列化方式。
fastjson2作為序列化方式
pom文件中引入fastjson2:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.45</version>
</dependency>
?增加RedisConfig配置類:
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 的序列化規(guī)則和 key 的序列化規(guī)則
template.setValueSerializer(redisSerializer);
template.setHashValueSerializer(redisSerializer);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
log.info("redis自定義配置啟動成功!");
return template;
}
}
再次執(zhí)行測試用例,進入redis desktop manage查看結(jié)果:
好了,配置完成。文章來源:http://www.zghlxwxcb.cn/news/detail-811605.html
拓展:文章來源地址http://www.zghlxwxcb.cn/news/detail-811605.html
RedisTemplat常用序列化方式:
1、JdkSerializationRedisSerializer:使用Java的序列化功能。這是默認的序列化機制,它使用標準的Java序列化方式。
2、StringRedisSerializer:字符串序列化,用于鍵(key)和值(value)都是字符串的情況。
3、Jackson2JsonRedisSerializer:使用Jackson庫將對象序列化為JSON字符串。同fastjson序列化方式。
4、GenericJackson2JsonRedisSerializer:是Jackson2JsonRedisSerializer的泛型版本,可以方便地序列化多種類型,而不需要為每種類型提供類型信息。
5、GenericToStringSerializer:可以將對象序列化為字符串。
到了這里,關于【項目搭建三】SpringBoot引入redis的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!