Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring-boot 's method of using lettuce redis client

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the method of spring-boot using lettuce redis client". In the daily operation, I believe that many people have doubts about the method of spring-boot using lettuce redis client. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "spring-boot uses lettuce redis client". Next, please follow the editor to study!

Config class:

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 / * configure custom redisTemplate * * @ return * / @ Bean public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) {RedisTemplate template = new RedisTemplate (); template.setConnectionFactory (redisConnectionFactory); / / use Jackson2JsonRedisSerializer to serialize and deserialize the value of redis / * 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); * / / use StringRedisSerializer to serialize and deserialize the key value of redis 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) {/ / Cluster configuration 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 {/ / standalone configuration 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 file:

Spring: redis: database: 0 # Redis has 16 shards by default. Specific shards are configured here. Default is 0 host: 192.168.86.130 port: 6379 lettuce: # netty thread-safe reids client pool: max-active: 8 # maximum number of connections in connection pool (using negative values for no limit) default 8 max-idle: maximum idle connections in 8 # connection pool default 8 max-wait:-1 # connection pool maximum blocking wait Time (use negative values to indicate no limit) default-1 min-idle: 0 # minimum idle connections in the connection pool default 0 timeout: 10000 # connection timeout (ms)

As configured above, connection pooling is used.

Pom.xml is as follows:

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} ${java.version} so far The study on "spring-boot 's method of using lettuce redis client" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report