您好,登錄后才能下訂單哦!
這篇文章主要介紹“spring-boot使用lettuce redis客戶端的方法”,在日常操作中,相信很多人在spring-boot使用lettuce redis客戶端的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”spring-boot使用lettuce redis客戶端的方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
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 RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> 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<RedisNode> nodes = new HashSet<RedisNode>(); 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 { // 單機版配置 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默認情況下有16個分片,這里配置具體使用的分片,默認是0 host: 192.168.86.130 port: 6379 lettuce: # netty 線程安全的reids客戶端 pool: max-active: 8 # 連接池最大連接數(使用負值表示沒有限制) 默認 8 max-idle: 8 # 連接池中的最大空閑連接 默認 8 max-wait: -1 # 連接池最大阻塞等待時間(使用負值表示沒有限制) 默認 -1 min-idle: 0 # 連接池中的最小空閑連接 默認 0 timeout: 10000 # 連接超時(ms)
如上配置,使用了連接池。
pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.loyin.study</groupId> <artifactId>redis</artifactId> <version>1.0-SNAPSHOT</version> <repositories> <repository> <id>aliyun-repos</id> <url>https://maven.aliyun.com/repository/public</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <properties> <java.version>1.8</java.version> <spring.boot.version>2.1.7.RELEASE</spring.boot.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot.version}</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- redis依賴commons-pool 這個依賴一定要添加 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>${maven-deploy-plugin.version}</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> </project>
到此,關于“spring-boot使用lettuce redis客戶端的方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。