springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問題)
lettuce客戶端
Lettuce 和 Jedis 的都是連接Redis Server的客戶端程序。Jedis在實現(xiàn)上是直連redis server,多線程環(huán)境下非線程安全(即多個線程對一個連接實例操作,是線程不安全的),除非使用連接池,為每個Jedis實例增加物理連接。Lettuce基于Netty的連接實例(StatefulRedisConnection),可以在多個線程間并發(fā)訪問,且線程安全,滿足多線程環(huán)境下的并發(fā)訪問(即多個線程公用一個連接實例,線程安全),同時它是可伸縮的設(shè)計,一個連接實例不夠的情況也可以按需增加連接實例。
添加依賴
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
//lettuce依賴commons-pool2
compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.6.2'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile('org.springframework.boot:spring-boot-starter-cache')
}
application.properties 添加redis連接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 server.port=8945 #redis數(shù)據(jù)庫默認使用db0 spring.redis.database=2 spring.redis.password= spring.redis.port=6379 spring.redis.host=127.0.0.1 # 連接超時時間 spring.redis.timeout=5000 # 連接池最大連接數(shù)(使用負值表示沒有限制) spring.redis.lettuce.pool.max-active=3 # 連接池中的最小空閑連接 spring.redis.lettuce.pool.min-idle=2 # 連接池中的最大空閑連接 spring.redis.lettuce.pool.max-idle=3 # 連接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.lettuce.pool.max-wait=-1 #在關(guān)閉客戶端連接之前等待任務(wù)處理完成的最長時間,在這之后,無論任務(wù)是否執(zhí)行完成,都會被執(zhí)行器關(guān)閉,默認100ms spring.redis.lettuce.shutdown-timeout=100 #是否緩存空值 spring.cache.redis.cache-null-values=false
編寫配置類
package org.example.base.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
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.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author l
* @date Created in 2020/11/3 10:51
*/
@Configuration
@EnableCaching
public class RedisConfig {
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private long timeout;
@Value("${spring.redis.lettuce.shutdown-timeout}")
private long shutDownTimeout;
@Value("${spring.redis.lettuce.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.lettuce.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.lettuce.pool.max-active}")
private int maxActive;
@Value("${spring.redis.lettuce.pool.max-wait}")
private long maxWait;
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxTotal(maxActive);
genericObjectPoolConfig.setMaxWaitMillis(maxWait);
genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(100);
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(database);
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(timeout))
.shutdownTimeout(Duration.ofMillis(shutDownTimeout))
.poolConfig(genericObjectPoolConfig)
.build();
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
// factory.setShareNativeConnection(true);
// factory.setValidateConnection(false);
return factory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(lettuceConnectionFactory);
//使用Jackson2JsonRedisSerializer替換默認的JdkSerializationRedisSerializer來序列化和反序列化redis的value值
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
jackson2JsonRedisSerializer.setObjectMapper(mapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
@Bean("redisCacheManager")
@Primary
public CacheManager cacheManager( LettuceConnectionFactory lettuceConnectionFactory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
//解決查詢緩存轉(zhuǎn)換異常的問題
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
jackson2JsonRedisSerializer.setObjectMapper(mapper);
// 配置1 ,
RedisCacheConfiguration config1 = RedisCacheConfiguration.defaultCacheConfig()
//緩存失效時間
.entryTtl(Duration.ofSeconds(30))
//key序列化方式
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
//value序列化方式
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
//不允許緩存null值
.disableCachingNullValues();
//配置2 ,
RedisCacheConfiguration config2 = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(1000))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
//設(shè)置一個初始化的緩存空間set集合
Set<String> cacheNames = new HashSet<>();
cacheNames.add("my-redis-cache1");
cacheNames.add("my-redis-cache2");
//對每個緩存空間應(yīng)用不同的配置
Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>(3);
configurationMap.put("my-redis-cache1", config1);
configurationMap.put("my-redis-cache2", config2);
return RedisCacheManager.builder(lettuceConnectionFactory)
//默認緩存配置
.cacheDefaults(config1)
//初始化緩存空間
.initialCacheNames(cacheNames)
//初始化緩存配置
.withInitialCacheConfigurations(configurationMap).build();
}
}
編寫service層
package org.example.base.service.impl;
import org.example.base.bean.Animal;
import org.example.base.service.AnimalService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
* @author l
* @date Created in 2020/11/4 17:33
*/
@Service
@CacheConfig(cacheNames = "my-redis-cache1", cacheManager = "redisCacheManager")
public class AnimalServiceImpl implements AnimalService {
@Override
@Cacheable(key = "#id",sync = true)
public Animal getAnimal(Integer id) {
System.out.println("操作數(shù)據(jù)庫,返回Animal");
return new Animal(110, "cat", "fish");
}
/**
* 使用@CachePut注解的方法,一定要有返回值,該注解聲明的方法緩存的是方法的返回結(jié)果。
* it always causes the
* method to be invoked and its result to be stored in the associated cache
**/
@Override
@CachePut(key = "#animal.getId()")
public Animal setAnimal(Animal animal) {
System.out.println("存入數(shù)據(jù)庫");
return animal;
}
@Override
@CacheEvict(key = "#id")
public void deleteAnimal(Integer id) {
System.out.println("刪除數(shù)據(jù)庫中animal");
}
@Override
@CachePut(key = "#animal.getId()")
public Animal updateAnimal(Animal animal) {
System.out.println("修改animal,并存入數(shù)據(jù)庫");
return animal;
}
}
編寫controller層
package org.example.base.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.example.base.bean.Animal;
import org.example.base.bean.User;
import org.example.base.service.AnimalService;
import org.example.base.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author l
* @date Created in 2020/10/23 15:46
*/
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
private AnimalService animalService;
@Autowired
public UserController(AnimalService animalService) {
this.animalService = animalService;
}
@GetMapping("/queryAnimal")
@ResponseBody
public Animal queryAnimal(@RequestParam(value = "ID") Integer ID) {
Animal animal = animalService.getAnimal(ID);
log.info("animal " + animal.toString());
return animal;
}
}
配置jemeter


啟動jemeter ,查看當(dāng)前redis的客戶端連接數(shù)

當(dāng)前springboot lettuce連接池中有三個連接 加上當(dāng)前查詢窗口的連接,共計4個socket連接。
jemeter 運行2分鐘后顯示,平均響應(yīng)時間為239,吞吐量為4158.2/sec

注釋掉 RedisConfig 類中
genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(100);
這行代碼,重新啟動項目,用jemeter進行壓測接口。
查看當(dāng)前redis的客戶端連接數(shù)

可以看出連接池沒有生效,經(jīng)過兩分鐘壓測后顯示平均響應(yīng)時間為241,吞吐量為4139.2/sec

,接口性能相比于使用lettuce連接池中多個連接,略有下降。 總結(jié) 要想使lettuce連接池生效,即使用多個redis物理連接。這行設(shè)置不能缺少
genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(100); 這個設(shè)置是,每隔多少毫秒,空閑線程驅(qū)逐器關(guān)閉多余的空閑連接,且保持最少空閑連接可用,這個值最好設(shè)置大一點,否者影響性能。同時 genericObjectPoolConfig.setMinIdle(minIdle); 中minldle值要大于0。
lettuce連接池屬性timeBetweenEvictionRunsMillis如果不設(shè)置 默認是 -1,當(dāng)該屬性值為負值時,lettuce連接池要維護的最小空閑連接數(shù)的目標(biāo)minIdle就不會生效 。源碼中的解釋如下:
/** * Target for the minimum number of idle connections to maintain in the pool. This * setting only has an effect if both it and time between eviction runs are * positive. */ private int minIdle = 0;
factory.setShareNativeConnection(true),shareNativeConnection 這個屬性默認是true,允許多個連接公用一個物理連接。如果設(shè)置false ,每一個連接的操作都會開啟和關(guān)閉socket連接。如果設(shè)置為false,會導(dǎo)致性能下降,本人測試過了。源碼中解釋如下:
/**
* Enables multiple {@link LettuceConnection}s to share a single native connection. If set to {@literal false}, every
* operation on {@link LettuceConnection} will open and close a socket.
*
* @param shareNativeConnection enable connection sharing.
*/
public void setShareNativeConnection(boolean shareNativeConnection) {
this.shareNativeConnection = shareNativeConnection;
}
-factory.setValidateConnection(false), validateConnection這個屬性是每次獲取連接時,校驗連接是否可用。默認false,不去校驗。默認情況下,lettuce開啟一個共享的物理連接,是一個長連接,所以默認情況下是不會校驗連接是否可用的。如果設(shè)置true,會導(dǎo)致性能下降。
到此這篇關(guān)于springboot2整合redis使用lettuce連接池(解決lettuce連接池?zé)o效問題)的文章就介紹到這了,更多相關(guān)springboot2使用lettuce連接池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java模擬http的Get/Post請求,并設(shè)置ip與port代理的方法
下面小編就為大家?guī)硪黄猨ava模擬http的Get/Post請求,并設(shè)置ip與port代理的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
淺談java 增強型的for循環(huán) for each
下面小編就為大家?guī)硪黄獪\談java 增強型的for循環(huán) for each。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
java編程中字節(jié)流轉(zhuǎn)換成字符流的實現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava編程中字節(jié)流轉(zhuǎn)換成字符流的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
使用maven整合Spring+SpringMVC+Mybatis框架詳細步驟(圖文)
這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
springboot中非容器類如何獲取配置文件數(shù)據(jù)
這篇文章主要介紹了springboot中非容器類如何獲取配置文件數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)詳解
這篇文章主要給大家介紹了關(guān)于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-03-03
SpringBoot2.0集成MQTT消息推送功能實現(xiàn)
這篇文章主要介紹了SpringBoot2.0集成MQTT消息推送功能實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

