redis单机
spring:
redis:
host: 127.0.0.1
port: 6379
database: 0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=mypassword
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
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);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//也可以采用GenericJackson2JsonRedisSerializer
//GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
redis哨兵Sentinel
spring:
redis:
sentinel:
master: mymaster
nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
password: mypassword
# 哨兵
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
spring.redis.sentinel.password=mypassword
redis集群Cluster
spring:
redis:
cluster:
max-redirects: 100
nodes: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
#集群
spring.redis.cluster.max-redirects=100
spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
其他需注意配置
##Redis客户端类型,lettuce,jedis两种
spring.redis.client-type=lettuce
##Redis超时时间,单位毫秒
spring.redis.timeout=3000
##Redis建立连接超时时间,单位毫秒
spring.redis.connect-timeout=3000
##是否启用ssl
spring.redis.ssl=false
##设置一个与服务器进行通信的客户端名称
spring.redis.client-name=client-redis
##通过url方式进行reids配置类似jdbc_url
spring.redis.url=redis://user:password@example.com:6379
spring.redis.username=
#建议不配置username,如果username为aa建议直接配置成下面样子
spring.redis.password=aa:123456
spring.redis.lettuce.pool.enabled=true
# 最大活动连接数
spring.redis.lettuce.pool.max-active=8
# 最大空闲连接数
spring.redis.lettuce.pool.max-idle=8
# 最小空闲连接数
spring.redis.lettuce.pool.min-idle=0
# 最大等待时间,-1表示无限制
spring.redis.lettuce.pool.max-wait=1ms
# 空闲对象逐出器线程的运行间隔时间。当为正值时,空闲对象逐出器线程启动,否则不执行空闲对象逐出。
# 这个不建议配置
spring.redis.lettuce.pool.time-between-eviction-runs=1
# 指定在应用关闭时,等待连接池中连接关闭的超时时间
spring.redis.lettuce.shutdown-timeout=100ms
下面是3中java代码方式的写入
单机
@Configuration
class AppConfig {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("server", 6379));
}
}
哨兵
/**
* Lettuce
*/
@Bean
public RedisConnectionFactory lettuceConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("mymaster")
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380);
return new LettuceConnectionFactory(sentinelConfig);
}
集群
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class ClusterConfigurationProperties {
/*
* spring.redis.cluster.nodes[0] = 127.0.0.1:7379
* spring.redis.cluster.nodes[1] = 127.0.0.1:7380
* ...
*/
List<String> nodes;
/**
* Get initial collection of known cluster nodes in format {@code host:port}.
*
* @return
*/
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
}
@Configuration
public class AppConfig {
/**
* Type safe representation of application.properties
*/
@Autowired ClusterConfigurationProperties clusterProperties;
@Bean
public RedisConnectionFactory connectionFactory() {
return new LettuceConnectionFactory(new RedisClusterConfiguration(clusterProperties))
}
}