springboot整合redis的两种连接方式

 

jedis和lettuce比较:

jedis:多线程下,非线程安全,所以使用连接池(不支持异步操作),适用springboot1.x

lettuce:多线程下,线程安全,基于Netty支持异步操作,适用springboot2.x

redis五大类型操作如下:

redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

redis的所有Serializer如下:

1、GenericToStringSerializer: 可以将任何对象泛化为字符串并序列化

2、StringRedisSerializer: 简单的字符串序列化 

3、Jackson2JsonRedisSerializer: 序列化object对象为json字符串
如:"{\"userName\":\"user\",\"age\":10}"

4、JdkSerializationRedisSerializer: 序列化java对象(被序列化的对象必须实现Serializable接口),无法转义成对象。
如:\x01\x03\x02\x11t\x00

5、GenericJackson2JsonRedisSerializer:类似第三条,但使用时构造函数不用特定的类参考以上序列化,自定义序列化类。(推荐)

 

一、使用lettuce

1、引入pom

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

<!--redis连接池-->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
	<version>2.8.0</version>
</dependency>

2、application配置文件添加

#redis配置
#数据库索引,默认0
spring.redis.database=0
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
#连接超时时间毫秒(类型为Duration,添加秒)
spring.redis.timeout=10s

# Lettuce
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=1
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=3
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
spring.redis.lettuce.pool.max-wait=5s
#空闲链接检测线程检测周期毫秒(负值表示不检测)(类型为Duration,添加秒)
spring.redis.lettuce.pool.time-between-eviction-runs=60s
#关闭超时时间
spring.redis.lettuce.shutdown-timeout=100

3、如果需要修改Serializer、打开事务等,在启动类添加(选填)

	@Bean
	public RedisTemplate redisTemplate(LettuceConnectionFactory factory){
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(factory);
		//配置序列化方式
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

		//设置redis支持数据库的事务
		redisTemplate.setEnableTransactionSupport(true);

		return redisTemplate;
	}

4、注入RedisTemplate,使用redis操作即可,如:opsForValue(),expire()(设置过期时间)

 

二、使用jedis

1、引入pom(spring-boot-starter-data-redis 默认是lettuce 需要先排除)

		<!--spring-boot-starter-data-redis 默认是lettuce 需要先排除-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<version>2.2.4.RELEASE</version>
			<exclusions>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!--redis连接池-->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.8.0</version>
		</dependency>

		<!--jedis-->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>3.2.0</version>
		</dependency>

2、application配置文件添加

#redis配置
#数据库索引,默认0
spring.redis.database=0
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
#连接超时时间毫秒(类型为Duration,添加秒)
spring.redis.timeout=10s
#连接池最大连接数
spring.redis.jedis.pool.max-active=5
#连接池最小空闲连接
spring.redis.jedis.pool.min-idle=1
#连接池最大空闲连接
spring.redis.jedis.pool.max-idle=3
#连接池最大阻塞等待时间(负值表示没有限制)(类型为Duration,添加秒)
spring.redis.jedis.pool.max-wait=5s
#空闲链接检测线程检测周期毫秒(负值表示不检测)(类型为Duration,添加秒)
spring.redis.jedis.pool.time-between-eviction-runs=60s

3、如果需要修改Serializer、打开事务等,在启动类添加(选填)

	@Bean
	public RedisTemplate redisTemplate(JedisConnectionFactory factory){
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(factory);
		//配置序列化方式
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

		//设置redis支持数据库的事务
		redisTemplate.setEnableTransactionSupport(true);

		return redisTemplate;
	}

4、注入RedisTemplate,使用redis操作即可,如:opsForValue(),expire()(设置过期时间)

 

构造CacheManager时springboot版本比较: 

springboot1.x时用:CacheManager cacheManager=new RedisCacheManager(redisTemplate)

springboot2.x时用:RedisCacheManager redisCacheManager = RedisCacheManager.create(redisConnectionFactory)

(实现类是JedisConnectionFactory或LettuceConnectionFactory)

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot 2.0中,默认使用Lettuce客户端来连接Redis服务。默认情况下,不使用连接池,只有在配置`redis.lettuce.pool`属性后才能使用Redis连接池。 使用连接池可以提升性能。例如,在插入1万条数据时,如果配置了连接池,执行速度会比没有配置连接池快很多[2]。 在Spring Boot中,使用Lettuce连接池时,可以配置连接池的大小。比如,如果当前连接池中已经有3个连接,再加上当前查询窗口的连接,一共就是4个socket连接。通过配置连接池大小,可以控制同时处理的连接数,以及减少与Redis服务器的重复建立和断开连接的开销。 综上所述,Spring Boot整合Redis连接池可以通过配置`redis.lettuce.pool`属性来启用,并且可以提升性能和优化连接管理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot 配置 Redis 连接池](https://blog.csdn.net/web18334137065/article/details/126114299)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [springboot2整合redis使用lettuce连接池(解决lettuce连接池无效问题)](https://blog.csdn.net/qq_41921994/article/details/109627736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值