springboot2整合redis集群(lettuce连接池)
引入相关jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Redis相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--lettuce连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
配置application.yml文件
spring:
redis:
timeout: 6000ms
password: 111111
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 192.168.15.128:8001
- 192.168.15.128:8002
- 192.168.15.128:8003
- 192.168.15.128:8004
- 192.168.15.128:8005
- 192.168.15.128:8006
lettuce:
pool:
max-active: 1000 #连接池最大连接数(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
添加配置类
package com.bingo.config;
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.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
测试
package com.bingo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller()
public class Test {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@RequestMapping(value = "/setkey-value")
@ResponseBody
private String getkey(String key,String value) {
redisTemplate.opsForValue().set(key, value);
String result = redisTemplate.opsForValue().get(key).toString();
return result;
}
@RequestMapping(value = "/getkey")
@ResponseBody
private String getkey(String key) {
String value = redisTemplate.opsForValue().get(key).toString();
System.out.println(value);
return value;
}
}
http://localhost:8080/setkey-value?key=name&value=test
http://localhost:8080/getkey?key=name