Connection Modes
-
独立模式:一个 Redis 服务器用于存储和读取数据。
-
主从复制模式:一个主 Redis 服务器用于写入,而一个或多个从服务器用于读取。
-
Redis Sentinel 模式:一个高可用性解决方案,使用 Sentinel 节点监控主服务器并自动故障转移。
-
Redis 集群模式:分布式部署,其中数据跨多个 Redis 服务器分片。
Redis 可以以各种设置运行。每种操作模式都需要特定的配置,将在以下部分中进行解释。
Redis can be operated in various setups. Each mode of operation requires specific configuration that is explained in the following sections.
Redis Standalone
入门的最简单方法是将 Redis Standalone 与单个 Redis 服务器一起使用,
The easiest way to get started is by using Redis Standalone with a single Redis server,
配置`LettuceClientConfiguration`或`JedisConnectionFactory`,如下例所示:
Configure LettuceClientConfiguration
or JedisConnectionFactory
, as shown in the following example:
@Configuration
class RedisStandaloneConfiguration {
/**
* Lettuce
*/
@Bean
public RedisConnectionFactory lettuceConnectionFactory() {
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("server", 6379));
}
/**
* Jedis
*/
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory(new RedisStandaloneConfiguration("server", 6379));
}
}
Write to Master, Read from Replica
Redis 主从设置(没有自动故障转移。有关自动故障转移,请参见:Sentinel)不仅能让数据安全地存储在更多节点,还允许通过使用 Lettuce从从属读取数据,同时把写入推送到主数据库。您可以使用 LettuceClientConfiguration
设置读取/写入策略,如下示例所示:
The Redis Master/Replica setup — without automatic failover (for automatic failover see: redis:sentinel) — not only allows data to be safely stored at more nodes.
It also allows, by using Lettuce, reading data from replicas while pushing writes to the master.
You can set the read/write strategy to be used by using LettuceClientConfiguration
, as shown in the following example:
@Configuration
class WriteToMasterReadFromReplicaConfiguration {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
.readFrom(REPLICA_PREFERRED)
.build();
RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration("server", 6379);
return new LettuceConnectionFactory(serverConfig, clientConfig);
}
}
使用“ |
For environments reporting non-public addresses through the |
Redis Sentinel
为了应对高可用性 Redis,Spring Data Redis 支持 Redis Sentinel,如以下示例所示:使用 RedisSentinelConfiguration
:
For dealing with high-availability Redis, Spring Data Redis has support for Redis Sentinel, using RedisSentinelConfiguration
, as shown in the following example:
/**
* 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);
}
/**
* Jedis
*/
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("mymaster")
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380);
return new JedisConnectionFactory(sentinelConfig);
}
`RedisSentinelConfiguration`还可以通过`RedisSentinelConfiguration.of(PropertySource)`定义,它允许你获取以下属性:
Configuration Properties
|
有时需要与 Sentinels 中的一个进行直接交互。使用`RedisConnectionFactory.getSentinelConnection()`或`RedisConnection.getSentinelCommands()`可以让访问配置的第一个活动 Sentinel。
Sometimes, direct interaction with one of the Sentinels is required. Using RedisConnectionFactory.getSentinelConnection()
or RedisConnection.getSentinelCommands()
gives you access to the first active Sentinel configured.
Redis Cluster
Cluster support 基于与非集群通信相同的构建模块。RedisClusterConnection
是 RedisConnection
的扩展,它处理与 Redis 集群的通信,并将错误转换为 Spring DAO 异常层次结构。RedisClusterConnection
实例使用 RedisConnectionFactory
创建,必须使用关联的 RedisClusterConfiguration
设置,如下面的示例所示:
Cluster support is based on the same building blocks as non-clustered communication. RedisClusterConnection
, an extension to RedisConnection
, handles the communication with the Redis Cluster and translates errors into the Spring DAO exception hierarchy.
RedisClusterConnection
instances are created with the RedisConnectionFactory
, which has to be set up with the associated RedisClusterConfiguration
, as shown in the following example:
@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;
public @Bean RedisConnectionFactory connectionFactory() {
return new LettuceConnectionFactory(
new RedisClusterConfiguration(clusterProperties.getNodes()));
}
}
`RedisClusterConfiguration`还可以通过`RedisClusterConfiguration.of(PropertySource)`定义,它允许你获取以下属性:
Configuration Properties
|
最初的配置点驱动程序库指向一组初始群集节点。源于实时的群集重新配置而产生的更改仅保存在本机驱动程序中,不会写回到配置。 |
The initial configuration points driver libraries to an initial set of cluster nodes. Changes resulting from live cluster reconfiguration are kept only in the native driver and are not written back to the configuration. |