修改了redis配置
This commit is contained in:
@ -1,189 +1,50 @@
|
||||
package com.chinaunicom.mall.ebtp.common.config;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
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.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
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;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
@Slf4j
|
||||
public class RedisLettuceConfig extends CachingConfigurerSupport {
|
||||
|
||||
|
||||
@Value("${spring.redis.token.host}")
|
||||
private String tokenHost;
|
||||
@Value("${spring.redis.token.password}")
|
||||
private String tokenPassword;
|
||||
@Value("${spring.redis.token.port}")
|
||||
private int tokenPort;
|
||||
@Value("${spring.redis.token.database}")
|
||||
private int tokenDatabase;
|
||||
@Value("${spring.redis.token.timeout}")
|
||||
private int tokenTimeout;
|
||||
|
||||
@Value("${spring.redis.uuid.host}")
|
||||
private String uuidHost;
|
||||
@Value("${spring.redis.uuid.password}")
|
||||
private String uuidPassword;
|
||||
@Value("${spring.redis.uuid.port}")
|
||||
private int uuidPort;
|
||||
@Value("${spring.redis.uuid.database}")
|
||||
private int uuidDatabase;
|
||||
@Value("${spring.redis.uuid.timeout}")
|
||||
private int uuidTimeout;
|
||||
|
||||
@Value("${spring.redis.cache.host}")
|
||||
private String cacheHost;
|
||||
@Value("${spring.redis.cache.password}")
|
||||
private String cachePassword;
|
||||
@Value("${spring.redis.cache.port}")
|
||||
private int cachePort;
|
||||
@Value("${spring.redis.cache.database}")
|
||||
private int cacheDatabase;
|
||||
@Value("${spring.redis.cache.timeout}")
|
||||
private int cacheTimeout;
|
||||
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
|
||||
@Scope(value = "prototype")
|
||||
public GenericObjectPoolConfig redisPool() {
|
||||
return new GenericObjectPoolConfig();
|
||||
}
|
||||
|
||||
|
||||
@Bean("redisConfigToken")
|
||||
@ConfigurationProperties(prefix = "spring.redis.token")
|
||||
public RedisStandaloneConfiguration redisConfigToken() {
|
||||
return getRedisStandaloneConfiguration(tokenDatabase, tokenHost, tokenPassword, tokenPort);
|
||||
}
|
||||
|
||||
private RedisStandaloneConfiguration getRedisStandaloneConfiguration(int database, String host, String password, int port) {
|
||||
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
|
||||
configuration.setDatabase(database);
|
||||
configuration.setHostName(host);
|
||||
configuration.setPassword(password);
|
||||
configuration.setPort(port);
|
||||
return configuration;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置第一个数据源的RedisTemplate
|
||||
* 注意:这里指定使用名称=factory 的 RedisConnectionFactory
|
||||
* 并且标识第一个数据源是默认数据源 @Primary
|
||||
/**
|
||||
* redis 配置
|
||||
*
|
||||
* @param redisConfigToken
|
||||
* @return
|
||||
* @author dino
|
||||
* @date 2021/2/19 19:29
|
||||
*/
|
||||
@Bean("lettuceFactoryToken")
|
||||
public LettuceConnectionFactory factoryToken(GenericObjectPoolConfig config, @Qualifier("redisConfigToken") RedisStandaloneConfiguration redisConfigToken) {
|
||||
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
|
||||
.poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
|
||||
return new LettuceConnectionFactory(redisConfigToken, clientConfiguration);
|
||||
}
|
||||
|
||||
@Bean(name = "redisTempletToken")
|
||||
public RedisTemplate<String, Object> redisTemplateToken(@Qualifier("lettuceFactoryToken") LettuceConnectionFactory redisConfigToken) {
|
||||
@Configuration
|
||||
@ConditionalOnClass(RedisOperations.class)
|
||||
@EnableConfigurationProperties(RedisProperties.class)
|
||||
public class RedisLettuceConfig {
|
||||
@Bean
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(redisConfigToken);
|
||||
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
|
||||
template.setConnectionFactory(factory);
|
||||
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
template.setKeySerializer(redisSerializer);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(mapper);
|
||||
|
||||
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
||||
template.setKeySerializer(new StringRedisSerializer(StandardCharsets.UTF_8));
|
||||
template.setHashKeySerializer(stringRedisSerializer);
|
||||
template.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
@Bean("redisConfigUuid")
|
||||
@ConfigurationProperties(prefix = "spring.redis.uuid")
|
||||
public RedisStandaloneConfiguration redisConfigUuid() {
|
||||
return getRedisStandaloneConfiguration(uuidDatabase, uuidHost, uuidPassword, uuidPort);
|
||||
}
|
||||
|
||||
@Bean("lettuceFactoryUuid")
|
||||
public LettuceConnectionFactory factoryUuid(GenericObjectPoolConfig config, @Qualifier("redisConfigUuid") RedisStandaloneConfiguration redisConfigUuid) {
|
||||
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
|
||||
.poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
|
||||
return new LettuceConnectionFactory(redisConfigUuid, clientConfiguration);
|
||||
}
|
||||
|
||||
|
||||
@Bean(name = "redisTempletUuid")
|
||||
public RedisTemplate<String, Object> redisTemplateUuid(@Qualifier("lettuceFactoryUuid") LettuceConnectionFactory redisConfigUuid) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(redisConfigUuid);
|
||||
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
template.setKeySerializer(redisSerializer);
|
||||
template.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean("redisConfigCache")
|
||||
@ConfigurationProperties(prefix = "spring.redis.cache")
|
||||
@Primary()
|
||||
public RedisStandaloneConfiguration redisConfigCache() {
|
||||
return getRedisStandaloneConfiguration(cacheDatabase, cacheHost, cachePassword, cachePort);
|
||||
}
|
||||
|
||||
@Bean("lettuceFactoryCache")
|
||||
@Primary()
|
||||
public LettuceConnectionFactory factoryCache(GenericObjectPoolConfig config, @Qualifier("redisConfigCache") RedisStandaloneConfiguration redisConfigCache) {
|
||||
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
|
||||
.poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
|
||||
return new LettuceConnectionFactory(redisConfigCache, clientConfiguration);
|
||||
}
|
||||
|
||||
|
||||
@Bean(name = "redisTempletCache")
|
||||
@Primary()
|
||||
public RedisTemplate<String, Object> redisTemplateCache(@Qualifier("lettuceFactoryCache") LettuceConnectionFactory redisConfigCache) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(redisConfigCache);
|
||||
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
template.setKeySerializer(redisSerializer);
|
||||
template.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,10 +5,12 @@ import com.chinaunicom.mall.ebtp.common.idempotent.annotation.Idempotent;
|
||||
import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@ -42,8 +44,7 @@ public class IdempotentAspect {
|
||||
private static final String DELKEY = "delKey";
|
||||
|
||||
@Autowired
|
||||
@Qualifier("redisTempletToken")
|
||||
private RedisTemplate<String, Object> redisTempletToken;
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Pointcut("@annotation(com.chinaunicom.mall.ebtp.common.idempotent.annotation.Idempotent)")
|
||||
public void pointCut() {
|
||||
@ -77,14 +78,14 @@ public class IdempotentAspect {
|
||||
boolean delKey = idempotent.delKey();
|
||||
|
||||
// do not need check null
|
||||
Map<Object, Object> rMapCache = redisTempletToken.opsForHash().entries(RMAPCACHE_KEY);
|
||||
Map<Object, Object> rMapCache = redisTemplate.opsForHash().entries(RMAPCACHE_KEY);
|
||||
String value = LocalDateTime.now().toString().replace("T", " ");
|
||||
if (null != rMapCache.get(key)) {
|
||||
// had stored
|
||||
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("[idempotent]:" + info,true);
|
||||
}
|
||||
synchronized (this) {
|
||||
boolean submitAble = redisTempletToken.opsForValue().setIfAbsent(key, value,expireTime,timeUnit);
|
||||
boolean submitAble = redisTemplate.opsForValue().setIfAbsent(key, value,expireTime,timeUnit);
|
||||
if (!submitAble) {
|
||||
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("[idempotent]:" + info,true);
|
||||
}
|
||||
@ -106,7 +107,7 @@ public class IdempotentAspect {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<Object, Object> mapCache = redisTempletToken.opsForHash().entries(RMAPCACHE_KEY);
|
||||
Map<Object, Object> mapCache = redisTemplate.opsForHash().entries(RMAPCACHE_KEY);
|
||||
if (mapCache.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
Reference in New Issue
Block a user