packagecom.framework.config;
importcom.common.utils.sign.DESUtil;
importorg.springframework.cache.annotation.CachingConfigurerSupport;
importorg.springframework.cache.annotation.EnableCaching;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.core.env.Environment;
importorg.springframework.data.redis.connection.RedisConnectionFactory;
importorg.springframework.data.redis.connection.RedisStandaloneConfiguration;
importorg.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
importorg.springframework.data.redis.core.RedisTemplate;
importorg.springframework.data.redis.core.script.DefaultRedisScript;
importorg.springframework.data.redis.serializer.StringRedisSerializer;
importcom.fasterxml.jackson.annotation.JsonAutoDetect;
importcom.fasterxml.jackson.annotation.JsonTypeInfo;
importcom.fasterxml.jackson.annotation.PropertyAccessor;
importcom.fasterxml.jackson.databind.ObjectMapper;
importcom.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
/**
* redis配置
*
* @author elane
*/
@Configuration
@EnableCaching
publicclassRedisConfigextendsCachingConfigurerSupport
{
privatefinalEnvironment environment;
publicRedisConfig(Environment environment){
this.environment=environment;
}
@Bean
publicRedisConnectionFactory myLettuceConnectionFactory(){
RedisStandaloneConfiguration redisStandaloneConfiguration =newRedisStandaloneConfiguration(environment.getProperty("spring.redis.host"),Integer.parseInt(environment.getProperty("spring.redis.port")));
redisStandaloneConfiguration.setDatabase(Integer.parseInt(environment.getProperty("spring.redis.database")));
//获取application.yml 中的密码(密文)
String password = environment.getProperty("spring.redis.password");
//解密密码并停驾到配置中
String pwd=DESUtil.encrypt("111111");//此处用于生成加密后的密码,配置在配置文件中
redisStandaloneConfiguration.setPassword(DESUtil.decrypt(password));
returnnewLettuceConnectionFactory(redisStandaloneConfiguration);
}
@Bean
@SuppressWarnings(value = {"unchecked","rawtypes"})
publicRedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{
//connectionFactory获取到的密码就是解密后的密码
RedisTemplate<Object, Object> template =newRedisTemplate<>();
template.setConnectionFactory(connectionFactory);
FastJson2JsonRedisSerializer serializer =newFastJson2JsonRedisSerializer(Object.class);
ObjectMapper mapper =newObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
serializer.setObjectMapper(mapper);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(newStringRedisSerializer());
template.setValueSerializer(serializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(newStringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
returntemplate;
}
@Bean
publicDefaultRedisScript<Long> limitScript()
{
DefaultRedisScript<Long> redisScript =newDefaultRedisScript<>();
redisScript.setScriptText(limitScriptText());
redisScript.setResultType(Long.class);
returnredisScript;
}
/**
* 限流脚本
*/
privateString limitScriptText()
{
return"local key = KEYS[1]\n"+
"local count = tonumber(ARGV[1])\n"+
"local time = tonumber(ARGV[2])\n"+
"local current = redis.call('get', key);\n"+
"if current and tonumber(current) > count then\n"+
" return tonumber(current);\n"+
"end\n"+
"current = redis.call('incr', key)\n"+
"if tonumber(current) == 1 then\n"+
" redis.call('expire', key, time)\n"+
"end\n"+
"return tonumber(current);";
}
}
|