spring boot 2.0.3使用Lettuce Redis並配置 pool
阿新 • • 發佈:2019-01-07
依賴的jar包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency>
RedisTemplate預設提供的序列化器
RedisSerializer redis序列化的介面類
OxmSerializer xml到object的序列化/反序列化
StringRedisSerializer string字串的序列化/反序列化
JacksonJsonRedisSerializer json到object的序列化/反序列化
Jackson2JsonRedisSerializer json到object的序列化/反序列化
JdkSerializationRedisSerializer java物件的序列化/反序列化
JAVA code:
配置檔案:存放位置properties/redis.properties
# Redis settings spring.redis.database=0 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.ssl=false #客戶端超時時間單位是毫秒 預設是2000 spring.redis.connTimeout=10000 spring.redis.CachingTime=900 #spring.redis.url=spring.redis://[email protected]:6379/2 #最大空閒數 spring.redis.maxIdle=300 #最小空閒數 spring.redis.minIdle=1 #連線池的最大資料庫連線數。設為0表示無限制,如果是jedis 2.4以後用spring.redis.maxTotal spring.redis.maxActive=600 spring.redis.maxWait=2 #控制一個pool可分配多少個jedis例項,用來替換上面的spring.redis.maxActive,如果是jedis 2.4以後用該屬性 spring.redis.maxTotal=1000 #最大建立連線等待時間。如果超過此時間將接到異常。設為-1表示無限制。 spring.redis.maxWaitMillis=3000 #連線的最小空閒時間 預設1800000毫秒(30分鐘) spring.redis.minEvictableIdleTimeMillis=300000 #每次釋放連線的最大數目,預設3 spring.redis.numTestsPerEvictionRun=1024 #逐出掃描的時間間隔(毫秒) 如果為負數,則不執行逐出執行緒, 預設-1 spring.redis.timeBetweenEvictionRunsMillis=30000 #是否在從池中取出連線前進行檢驗,如果檢驗失敗,則從池中去除連線並嘗試取出另一個 spring.redis.testOnBorrow=true #返回性校驗 spring.redis.testOnReturn=true #在空閒時檢查有效性, 預設false spring.redis.testWhileIdle=true
RedisProperties配置類:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@Data
@PropertySource(value= {"classpath:properties/redis.properties"})
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
private static final String PROPERTIES_FILE = "application.properties";
/**
* spring.redis.database=0
* spring.redis.host=192.168.33.200
* spring.redis.port=6379
* spring.redis.ssl=false
* spring.redis.password=123456
* spring.redis.connTimeout=5000ms
* spring.redis.maxActive=500
* spring.redis.maxIdle=10
* spring.redis.minIdle=0
* spring.redis.maxWait=5000ms
* */
private Integer database;
private String host;
private Integer port;
private Boolean ssl;
private String password;
private Long connTimeout;
private Integer maxActive;
private Integer maxIdle;
private Integer minIdle;
private Integer maxWait;
}
RedisConfig配置類:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.myself.platform.properties.RedisProperties;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
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.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* Created by qxr4383 on 2018/12/27.
*/
@Configuration
@EnableCaching
public class RedisConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
//設定序列化
//使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(預設使用JDK的序列化方式)
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
//指定要序列化的域,field,get和set,以及修飾符範圍,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 配置連線工廠
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);//key序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//value序列化
redisTemplate.setHashKeySerializer(stringSerializer);//Hash key序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);//Hash value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig genericObjectPoolConfig) {
// 單機版配置
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase()==null ? 0 : redisProperties.getDatabase());
redisStandaloneConfiguration.setHostName(redisProperties.getHost());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
// 叢集版配置
// 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);
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(redisProperties.getConnTimeout()))
.poolConfig(genericObjectPoolConfig)
.build();
if (redisProperties.getSsl()){
clientConfig.isUseSsl();
}
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration,clientConfig);
return factory;
}
/**
* GenericObjectPoolConfig 連線池配置
*
* @return
*/
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig() {
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
genericObjectPoolConfig.setMinIdle(redisProperties.getMinIdle());
genericObjectPoolConfig.setMaxTotal(redisProperties.getMaxActive());
genericObjectPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
return genericObjectPoolConfig;
}
@Bean
public CacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
.RedisCacheManagerBuilder
.fromConnectionFactory(lettuceConnectionFactory);
return builder.build();
}
}
redis工具介面類:
import java.util.List;
import java.util.Set;
public interface RedisService {
boolean set(String key, Object value);
boolean set(String key, Object value, Long expireTime);
void remove(String... keys);
/**
* 批量刪除key
* @param pattern
*/
void removePattern(String pattern);
/**
* 刪除對應的value
* @param key
*/
void remove(String key);
/**
* 判斷快取中是否有對應的value
* @param key
* @return
*/
boolean exists(String key);
/**
* 讀取快取
* @param key
* @return
*/
Object get(String key);
/**
* 雜湊 新增
* @param key
* @param hashKey
* @param value
*/
void hmSet(String key, Object hashKey, Object value);
/**
* 雜湊獲取資料
* @param key
* @param hashKey
* @return
*/
Object hmGet(String key, Object hashKey);
/**
* 列表新增
* @param k
* @param v
*/
void lPush(String k, Object v);
/**
* 列表獲取
* @param k
* @param l
* @param l1
* @return
*/
List<Object> lRange(String k, long l, long l1);
/**
* 集合新增
* @param key
* @param value
*/
void setArray(String key, Object value);
/**
* 集合獲取
* @param key
* @return
*/
Set<Object> getArray(String key);
/**
* 有序集合新增
* @param key
* @param value
* @param scoure
*/
void zAdd(String key, Object value, double scoure);
/**
* 有序集合獲取
* @param key
* @param scoure
* @param scoure1
* @return
*/
Set<Object> rangeByScore(String key, double scoure, double scoure1);
}
redis介面實現類:
import com.myself.platform.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
private RedisTemplate redisTemplate;
/**
* 寫入快取
* @param key
* @param value
* @return boolean
*/
@Override
public boolean set(String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 寫入快取設定時效時間
* @param key
* @param value
* @return boolean
*/
@Override
public boolean set(String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量刪除對應的value
* @param keys
*/
@Override
public void remove(String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量刪除key
* @param pattern
*/
@Override
public void removePattern(String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* 刪除對應的value
* @param key
*/
@Override
public void remove(String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判斷快取中是否有對應的value
* @param key
* @return
*/
@Override
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
/**
* 讀取快取
* @param key
* @return
*/
@Override
public Object get(String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 雜湊 新增
* @param key
* @param hashKey
* @param value
*/
@Override
public void hmSet(String key, Object hashKey, Object value){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value);
}
/**
* 雜湊獲取資料
* @param key
* @param hashKey
* @return
*/
@Override
public Object hmGet(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key,hashKey);
}
/**
* 列表新增
* @param k
* @param v
*/
@Override
public void lPush(String k,Object v){
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k,v);
}
/**
* 列表獲取
* @param k
* @param l
* @param l1
* @return
*/
@Override
public List<Object> lRange(String k, long l, long l1){
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k,l,l1);
}
/**
* 集合新增
* @param key
* @param value
*/
@Override
public void setArray(String key,Object value){
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key,value);
}
/**
* 集合獲取
* @param key
* @return
*/
@Override
public Set<Object> getArray(String key){
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
/**
* 有序集合新增
* @param key
* @param value
* @param scoure
*/
@Override
public void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
}
/**
* 有序集合獲取
* @param key
* @param scoure
* @param scoure1
* @return
*/
@Override
public Set<Object> rangeByScore(String key,double scoure,double scoure1){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
}
測試類:
@Autowired
private RedisService redisService;
public String getProperties(){
redisService.set("3213", 1213,900l);
}