欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問題)

 更新時(shí)間:2020年12月29日 09:41:48   作者:冬風(fēng)孤立  
這篇文章主要介紹了springboot2整合redis使用lettuce連接池(解決lettuce連接池?zé)o效問題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

lettuce客戶端

Lettuce 和 Jedis 的都是連接Redis Server的客戶端程序。Jedis在實(shí)現(xiàn)上是直連redis server,多線程環(huán)境下非線程安全(即多個(gè)線程對(duì)一個(gè)連接實(shí)例操作,是線程不安全的),除非使用連接池,為每個(gè)Jedis實(shí)例增加物理連接。Lettuce基于Netty的連接實(shí)例(StatefulRedisConnection),可以在多個(gè)線程間并發(fā)訪問,且線程安全,滿足多線程環(huán)境下的并發(fā)訪問(即多個(gè)線程公用一個(gè)連接實(shí)例,線程安全),同時(shí)它是可伸縮的設(shè)計(jì),一個(gè)連接實(shí)例不夠的情況也可以按需增加連接實(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ù)庫默認(rèn)使用db0
spring.redis.database=2
spring.redis.password=
spring.redis.port=6379
spring.redis.host=127.0.0.1
# 連接超時(shí)時(shí)間
spring.redis.timeout=5000
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.lettuce.pool.max-active=3
# 連接池中的最小空閑連接
spring.redis.lettuce.pool.min-idle=2
# 連接池中的最大空閑連接
spring.redis.lettuce.pool.max-idle=3
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
spring.redis.lettuce.pool.max-wait=-1
#在關(guān)閉客戶端連接之前等待任務(wù)處理完成的最長時(shí)間,在這之后,無論任務(wù)是否執(zhí)行完成,都會(huì)被執(zhí)行器關(guān)閉,默認(rè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替換默認(rèn)的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()
    //緩存失效時(shí)間
    .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è)置一個(gè)初始化的緩存空間set集合
  Set<String> cacheNames = new HashSet<>();
  cacheNames.add("my-redis-cache1");
  cacheNames.add("my-redis-cache2");


  //對(duì)每個(gè)緩存空間應(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)
    //默認(rèn)緩存配置
    .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

在這里插入圖片描述
在這里插入圖片描述

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

當(dāng)前springboot lettuce連接池中有三個(gè)連接 加上當(dāng)前查詢窗口的連接,共計(jì)4個(gè)socket連接
當(dāng)前springboot lettuce連接池中有三個(gè)連接 加上當(dāng)前查詢窗口的連接,共計(jì)4個(gè)socket連接。

jemeter 運(yùn)行2分鐘后顯示,平均響應(yīng)時(shí)間為239,吞吐量為4158.2/sec

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

,接口性能相比于使用lettuce連接池中多個(gè)連接,略有下降。 總結(jié) 要想使lettuce連接池生效,即使用多個(gè)redis物理連接。這行設(shè)置不能缺少
genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(100); 這個(gè)設(shè)置是,每隔多少毫秒,空閑線程驅(qū)逐器關(guān)閉多余的空閑連接,且保持最少空閑連接可用,這個(gè)值最好設(shè)置大一點(diǎn),否者影響性能。同時(shí) genericObjectPoolConfig.setMinIdle(minIdle); 中minldle值要大于0。
lettuce連接池屬性timeBetweenEvictionRunsMillis如果不設(shè)置 默認(rèn)是 -1,當(dāng)該屬性值為負(fù)值時(shí),lettuce連接池要維護(hù)的最小空閑連接數(shù)的目標(biāo)minIdle就不會(huì)生效 。源碼中的解釋如下:

	/**
		 * 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 這個(gè)屬性默認(rèn)是true,允許多個(gè)連接公用一個(gè)物理連接。如果設(shè)置false ,每一個(gè)連接的操作都會(huì)開啟和關(guān)閉socket連接。如果設(shè)置為false,會(huì)導(dǎo)致性能下降,本人測(cè)試過了。源碼中解釋如下:

/**
	 * 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這個(gè)屬性是每次獲取連接時(shí),校驗(yàn)連接是否可用。默認(rèn)false,不去校驗(yàn)。默認(rèn)情況下,lettuce開啟一個(gè)共享的物理連接,是一個(gè)長連接,所以默認(rèn)情況下是不會(huì)校驗(yàn)連接是否可用的。如果設(shè)置true,會(huì)導(dǎo)致性能下降。

到此這篇關(guān)于springboot2整合redis使用lettuce連接池(解決lettuce連接池?zé)o效問題)的文章就介紹到這了,更多相關(guān)springboot2使用lettuce連接池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實(shí)現(xiàn)多人聊天室(含界面)

    Java實(shí)現(xiàn)多人聊天室(含界面)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多人聊天室,包含界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • java模擬http的Get/Post請(qǐng)求,并設(shè)置ip與port代理的方法

    java模擬http的Get/Post請(qǐng)求,并設(shè)置ip與port代理的方法

    下面小編就為大家?guī)硪黄猨ava模擬http的Get/Post請(qǐng)求,并設(shè)置ip與port代理的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • 淺談java 增強(qiáng)型的for循環(huán) for each

    淺談java 增強(qiáng)型的for循環(huán) for each

    下面小編就為大家?guī)硪黄獪\談java 增強(qiáng)型的for循環(huán) for each。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • java編程中字節(jié)流轉(zhuǎn)換成字符流的實(shí)現(xiàn)方法

    java編程中字節(jié)流轉(zhuǎn)換成字符流的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄猨ava編程中字節(jié)流轉(zhuǎn)換成字符流的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • 10道springboot常見面試題

    10道springboot常見面試題

    這篇文章主要為大家詳細(xì)介紹了10道springboot常見面試題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • mybatis的使用-Mapper文件各種語法介紹

    mybatis的使用-Mapper文件各種語法介紹

    這篇文章主要介紹了mybatis的使用-Mapper文件各種語法介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)

    使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)

    這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • springboot中非容器類如何獲取配置文件數(shù)據(jù)

    springboot中非容器類如何獲取配置文件數(shù)據(jù)

    這篇文章主要介紹了springboot中非容器類如何獲取配置文件數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)詳解

    Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)詳解

    這篇文章主要給大家介紹了關(guān)于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-03-03
  • SpringBoot2.0集成MQTT消息推送功能實(shí)現(xiàn)

    SpringBoot2.0集成MQTT消息推送功能實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot2.0集成MQTT消息推送功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論