

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将读取分发到副本（已禁用集群模式）
<a name="BestPractices.Clients-lettuce-cmd-readfrom"></a>

对于已禁用集群模式且具有副本的集群，您可以使用 Lettuce 的`StaticMasterReplicaTopologyProvider``ReadFrom`设置将写入定向到主节点并在副本之间分配读取。这通过卸载主服务器的读取来提高读取吞吐量。

使用主端点进行写入，使用读取器端点进行读取。读取器端点会自动在可用副本之间对连接进行负载平衡。

```
// Primary endpoint for writes, reader endpoint for read distribution
RedisURI primaryURI = RedisURI.Builder.redis("primary-endpoint.cache.amazonaws.com")
    .withPort(6379).withSsl(true).build();
RedisURI readerURI = RedisURI.Builder.redis("reader-endpoint.cache.amazonaws.com")
    .withPort(6379).withSsl(true).build();

RedisClient redisClient = RedisClient.create(clientResources);
redisClient.setOptions(clientOptions);

// Open a primary-replica connection using the StaticMasterReplicaTopologyProvider
StatefulRedisMasterReplicaConnection<String, String> connection =
    MasterReplica.connect(redisClient, StringCodec.UTF8, Arrays.asList(primaryURI, readerURI));

// Direct reads to replicas, writes always go to the primary
connection.setReadFrom(ReadFrom.REPLICA_PREFERRED);
```

有以下`ReadFrom`选项可用：
+ `REPLICA_PREFERRED`— 在副本可用时从副本读取并回退到主副本（建议大多数用例使用）。
+ `REPLICA`— 仅从副本读取。如果没有副本可用，则失败。
+ `PRIMARY_PREFERRED`— 从主服务器读取，回退到副本。
+ `ANY_REPLICA`— 从任何副本节点读取。
+ `PRIMARY`— 仅从主节点读取。在需要很强的一致性时使用。

**注意**  
由于复制是异步的，因此从副本读取的数据可能会有些陈旧。如果您的应用程序需要很强的一致性，请使用`ReadFrom.PRIMARY`或`ReadFrom.PRIMARY_PREFERRED`。