spring.redis.common.database.index=0
spring.redis.common.lettuce.pool.min-idle=0
spring.redis.common.lettuce.pool.max-idle=8
spring.redis.common.lettuce.pool.max-active=8
spring.redis.common.lettuce-factory.max-wait=300
spring.redis.wh.host=192.168.1.130
spring.redis.wh.port=6379
spring.redis.wh.password=root
spring.redis.es.host=192.168.1.130
spring.redis.es.port=6380
spring.redis.es.password=root
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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 java.time.Duration;
@Configuration
public class LettuceRedisConfig {
@Value("${spring.redis.common.lettuce.pool.max-idle}")
int maxIdle;
@Value("${spring.redis.common.lettuce.pool.min-idle}")
int minIdle;
@Value("${spring.redis.common.lettuce.pool.max-active}")
int maxTotal;
@Value("${spring.redis.common.database.index}")
int index;
@Value("${spring.redis.common.lettuce-factory.max-wait}")
long maxWaitMillis;
@Primary
@Bean(name = "RedisConnectionFactoryWh")
public LettuceConnectionFactory connectionFactoryWh(
@Value("${spring.redis.wh.host}") String hostName,
@Value("${spring.redis.wh.port}") int port,
@Value("${spring.redis.wh.password}") String password) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(index);
redisStandaloneConfiguration.setHostName(hostName);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(maxWaitMillis))
.poolConfig(genericObjectPoolConfig())
.build();
return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
}
@Bean(name = "RedisConnectionFactoryEs")
public LettuceConnectionFactory connectionFactoryEs(
@Value("${spring.redis.es.host}") String hostName,
@Value("${spring.redis.es.port}") int port,
@Value("${spring.redis.es.password}") String password) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(index);
redisStandaloneConfiguration.setHostName(hostName);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(maxWaitMillis))
.poolConfig(genericObjectPoolConfig())
.build();
return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
}
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig() {
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxTotal(maxTotal);
genericObjectPoolConfig.setMaxWaitMillis(maxWaitMillis);
return genericObjectPoolConfig;
}
}
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean(name = "RedisWhDatabase")
public RedisTemplate<String, Object> redisWhDatabase(@Qualifier("RedisConnectionFactoryWh") LettuceConnectionFactory lettuceConnectionFactory) {
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);
RedisTemplate<String, Object> temple = new RedisTemplate<>();
temple.setConnectionFactory(lettuceConnectionFactory);
RedisSerializer stringSerializer = new StringRedisSerializer();
temple.setKeySerializer(stringSerializer);
temple.setValueSerializer(stringSerializer);
temple.setHashKeySerializer(stringSerializer);
temple.setHashValueSerializer(jackson2JsonRedisSerializer);
return temple;
}
@Bean(name = "StringRedisTemplateWh")
public StringRedisTemplate stringRedisTemplateWh(@Qualifier("RedisConnectionFactoryWh") LettuceConnectionFactory lettuceConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(lettuceConnectionFactory);
return stringRedisTemplate;
}
@Bean(name = "RedisEsDatabase")
public RedisTemplate<String, Object> redisEsDatabase(@Qualifier("RedisConnectionFactoryEs") LettuceConnectionFactory lettuceConnectionFactory) {
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);
RedisTemplate<String, Object> temple = new RedisTemplate<>();
temple.setConnectionFactory(lettuceConnectionFactory);
RedisSerializer stringSerializer = new StringRedisSerializer();
temple.setKeySerializer(stringSerializer);
temple.setValueSerializer(stringSerializer);
temple.setHashKeySerializer(stringSerializer);
temple.setHashValueSerializer(jackson2JsonRedisSerializer);
return temple;
}
@Bean(name = "StringRedisTemplateEs")
public StringRedisTemplate stringRedisTemplateEs(@Qualifier("RedisConnectionFactoryEs") LettuceConnectionFactory lettuceConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(lettuceConnectionFactory);
return stringRedisTemplate;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
private static RedisTemplate RedisWhDatabase;
private static StringRedisTemplate stringRedisTemplateWh;
private static RedisTemplate RedisEsDatabase;
private static StringRedisTemplate stringRedisTemplateEs;
@Autowired
public RedisUtil(@Qualifier("RedisWhDatabase") RedisTemplate RedisWhDatabase,
@Qualifier("StringRedisTemplateWh") StringRedisTemplate stringRedisTemplateWh,
@Qualifier("RedisEsDatabase") RedisTemplate RedisEsDatabase,
@Qualifier("StringRedisTemplateEs") StringRedisTemplate stringRedisTemplateEs) {
RedisUtil.RedisWhDatabase = RedisWhDatabase;
RedisUtil.stringRedisTemplateWh = stringRedisTemplateWh;
RedisUtil.RedisEsDatabase = RedisEsDatabase;
RedisUtil.stringRedisTemplateEs = stringRedisTemplateEs;
}
public static void set(String key, String value, Long time) throws IOException {
String city = DataSourceHolder.getDataSource();
switch (CityEnum.valueOf(city)) {
case wuhan:
stringRedisTemplateWh.opsForValue().set(key, value, time, TimeUnit.SECONDS);
System.err.println("wuhan RedisTemplate 插入成功");
break;
case enshi:
stringRedisTemplateEs.opsForValue().set(key, value, time,TimeUnit.SECONDS);
System.err.println("enshi RedisTemplate 插入成功");
break;
default:
break;
}
}
public static String get(String key) {
String city = DataSourceHolder.getDataSource();
String value = null;
switch (CityEnum.valueOf(city)) {
case wuhan:
value = stringRedisTemplateWh.opsForValue().get(key);
break;
case enshi:
value = stringRedisTemplateEs.opsForValue().get(key);
break;
default:
break;
}
return value;
}
}