一、springboot集成redis一般配置
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.0</version>
</dependency>
配置文件
spring:
redis:
database: 0
host: 127.0.0.1
password: 123456
lettuce:
pool:
max-active: 8 #最大连接数据库连接数,设 0 为没有限制
max-idle: 8 #最大等待连接中的数量,设 0 为没有限制
max-wait: -1ms #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
min-idle: 0 #最小等待连接中的数量,设 0 为没有限制
shutdown-timeout: 100ms
port: 6379
配置类
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Resource
private RedisConnectionFactory factory;
@Bean
public RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer());
return redisTemplate;
}
}
使用
@Resource
private RedisTemplate<String,Object> redisTemplate;
二、引入第二个数据源other
在一般配置的基础上,引入第二个redis数据源
yml配置文件添加
spring:
redis:
database: 0
host: 127.0.0.1
password: 123456
lettuce:
pool:
max-active: 8 #最大连接数据库连接数,设 0 为没有限制
max-idle: 8 #最大等待连接中的数量,设 0 为没有限制
max-wait: -1ms #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
min-idle: 0 #最小等待连接中的数量,设 0 为没有限制
shutdown-timeout: 100ms
port: 6379
other: # 第二个数据源的配置,至少要配置一个database,如果host,port,password不配置,则使用spring.redis中的配置
database: 2
host: 192.168.1.103
port: 6379
password: 123456
第二个数据源的配置,至少要配置一个database,如果host,port,password不配置,则使用spring.redis中的配置
添加配置类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("spring.other")
@Data
public class OtherRedisProperties {
private Integer database;
private String host;
private String password;
private Integer port;
}
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableConfigurationProperties(OtherRedisProperties.class)
public class OtherRedisConfig {
@Bean
@ConditionalOnMissingBean(name = "otherRedisTemplate")
public RedisTemplate<String, Object> otherRedisTemplate(RedisProperties redisProperties, OtherRedisProperties otherRedisProperties, @Autowired(required = false) RedisSerializer<Object> defaultRedisSerializer) {
LettucePoolingClientConfiguration build = LettucePoolingClientConfiguration.builder().poolConfig(getPoolConfig(redisProperties.getLettuce().getPool())).build();
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(getStandaloneConfig(redisProperties, otherRedisProperties), build);
connectionFactory.afterPropertiesSet();
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
if (defaultRedisSerializer != null) {
redisTemplate.setDefaultSerializer(defaultRedisSerializer);
}
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
private GenericObjectPoolConfig<?> getPoolConfig(RedisProperties.Pool properties) {
GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(properties.getMaxActive());
config.setMaxIdle(properties.getMaxIdle());
config.setMinIdle(properties.getMinIdle());
if (properties.getTimeBetweenEvictionRuns() != null) {
config.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRuns().toMillis());
}
if (properties.getMaxWait() != null) {
config.setMaxWaitMillis(properties.getMaxWait().toMillis());
}
return config;
}
protected final RedisStandaloneConfiguration getStandaloneConfig(RedisProperties properties, OtherRedisProperties otherRedisProperties) {
PropertyMapper map = PropertyMapper.get();
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(properties.getHost());
config.setPort(properties.getPort());
config.setPassword(properties.getPassword());
config.setDatabase(properties.getDatabase());
//设置新设置的redis参数
map.from(otherRedisProperties.getHost()).whenNonNull().to(config::setHostName);
map.from(otherRedisProperties.getPort()).whenNonNull().to(config::setPort);
map.from(otherRedisProperties.getDatabase()).whenNonNull().to(config::setDatabase);
map.from(otherRedisProperties.getPassword()).whenNonNull().to(config::setPassword);
return config;
}
}
使用
@Resource
private RedisTemplate<String,Object> otherRedisTemplate;