這篇文章主要介紹“spring-boot使用lettuce redis客戶端的方法”,在日常操作中,相信很多人在spring-boot使用lettuce redis客戶端的方法問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”spring-boot使用lettuce redis客戶端的方法”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比鞏義網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鞏義網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鞏義地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。
config類:
package net.loyin.cloud.upms.config; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.*; 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.StringRedisSerializer; import java.time.Duration; import java.util.HashSet; import java.util.Set; @Configuration @AutoConfigureAfter(RedisAutoConfiguration.class) public class RedisConfig { @Value("${spring.redis.database:0}") private int database; @Value("${spring.redis.host:localhost}") private String host; @Value("${spring.redis.nodes:}") private String clusterNodes; @Value("${spring.redis.password:}") private String password; @Value("${spring.redis.port:6379}") private int port; @Value("${spring.redis.timeout:1000}") private long timeout; @Value("${spring.redis.lettuce.pool.max-idle:8}") private int maxIdle; @Value("${spring.redis.lettuce.pool.min-idle:8}") private int minIdle; @Value("${spring.redis.lettuce.pool.max-active:10}") private int maxActive; @Value("${spring.redis.lettuce.pool.max-wait:-1}") private long maxWait; /** * 配置自定義redisTemplate * * @return */ @Bean public RedisTemplateredisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值 /*Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper);*/ //使用StringRedisSerializer來序列化和反序列化redis的key值 StringRedisSerializer stringRedisSerializer=new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setHashValueSerializer(stringRedisSerializer); template.afterPropertiesSet(); return template; } @Bean LettuceConnectionFactory redisConnectionFactory() { RedisConfiguration redisConfiguration = null; if (clusterNodes != null&&"".equals(clusterNodes)==false) { // 集群版配置 RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(); String[] serverArray = clusterNodes.split(","); Set nodes = new HashSet (); for (String ipPort : serverArray) { String[] ipAndPort = ipPort.split(":"); nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1]))); } redisClusterConfiguration.setPassword(RedisPassword.of(password)); redisClusterConfiguration.setClusterNodes(nodes); // redisClusterConfiguration.setMaxRedirects(maxRedirects); redisConfiguration = redisClusterConfiguration; } else { // 單機(jī)版配置 RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setDatabase(database); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(port); if (password != null && "".equals(password) == false) { redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); } redisConfiguration = redisStandaloneConfiguration; } GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxIdle(maxIdle); poolConfig.setMinIdle(minIdle); poolConfig.setMaxTotal(maxActive); poolConfig.setMaxWaitMillis(maxWait); LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder() .commandTimeout(Duration.ofMillis(timeout)) .poolConfig(poolConfig) .build(); LettuceConnectionFactory factory = new LettuceConnectionFactory(redisConfiguration, clientConfig); return factory; } }
yml文件:
spring: redis: database: 0 # Redis默認(rèn)情況下有16個(gè)分片,這里配置具體使用的分片,默認(rèn)是0 host: 192.168.86.130 port: 6379 lettuce: # netty 線程安全的reids客戶端 pool: max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) 默認(rèn) 8 max-idle: 8 # 連接池中的最大空閑連接 默認(rèn) 8 max-wait: -1 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制) 默認(rèn) -1 min-idle: 0 # 連接池中的最小空閑連接 默認(rèn) 0 timeout: 10000 # 連接超時(shí)(ms)
如上配置,使用了連接池。
pom.xml如下:
4.0.0 net.loyin.study redis 1.0-SNAPSHOT aliyun-repos https://maven.aliyun.com/repository/public false 1.8 2.1.7.RELEASE org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE org.projectlombok lombok 1.18.8 compile org.springframework.boot spring-boot-starter-web ${spring.boot.version} org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-data-redis ${spring.boot.version} org.springframework.boot spring-boot-starter-test test org.apache.commons commons-pool2 org.apache.maven.plugins maven-deploy-plugin ${maven-deploy-plugin.version} true org.apache.maven.plugins maven-compiler-plugin ${java.version}
到此,關(guān)于“spring-boot使用lettuce redis客戶端的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!