Springboot2整合Redis - Go语言中文社区

Springboot2整合Redis


Springboot2整合Redis

新的改变

Spring Boot 1.0 默认使用的是 Jedis 客户端,2.0 替换成 Lettuce
Lettuce 和 Jedis 的都是连接Redis Server的客户端程序。
Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接。
Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

01:导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

02:配置

#redis配置 Lettuce 是一个可伸缩线程安全的 Redis 客户端,多个线程可以共享同一个 RedisConnection,它利用优秀 netty NIO 框架来高效地管理多个连接
spring.redis.host=192.168.0.172
spring.redis.port=6379
spring.redis.password=
# 连接超时时间(毫秒)
spring.redis.timeout=36000ms
# Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1ms
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0

03:查看源码:RedisAutoConfiguration自动配置类

spring-boot-starter-data-redis启动器已经为我们提供了两种Template,无需任何配置,直接使用即可。

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean
	public StringRedisTemplate stringRedisTemplate(
			RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

}

04:直接使用

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

方法有
opsForValue :对应 String(字符串)
opsForZSet:对应 ZSet(有序集合)
opsForHash:对应 Hash(哈希)
opsForList:对应 List(列表)
opsForSet:对应 Set(集合)
opsForGeo:对应 GEO(地理位置)


05:修改默认的序列化方法

在实际使用中,我们大多不会直接使用RedisTemplate<Object,Object>,而是会对key,value进行序列化,所以我们还需要新增一个配置类
RedisSerializer类下查看我们可用的序列化器。
在这里插入图片描述

接下来配置所需要的 ,一般使用:Jackson2JsonRedisSerializer

@Configuration
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate<String, Object> redisCacheTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        template.setConnectionFactory(factory);
        return template;
    }
}

06:测试


 @Autowired
 RedisTemplate<String, Object> redisTemplate;
 
 @RequestMapping(value = "/core/redis/set02", method = RequestMethod.GET)
    public String redis03(@RequestParam Integer id) throws Exception {
        User user = mapper.getOne(id);
        redisTemplate.opsForValue().set("user", user);
        return "ok";
    }

** Redis Desktop Manager进行结果展示 **
没自定义序列化器Jackson2JsonRedisSerializer 之前的:
在这里插入图片描述

使用后的

在这里插入图片描述


END

个人微信公众号:fbzl95


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/victoylin/article/details/91906875
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢