我们可以像这样创建一个集群实例.
cluster = Cluster
.builder()
.addContactPoint("192.168.0.30")
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.build();
Run Code Online (Sandbox Code Playgroud)
我们是否会在查询时提供重试请求的时间信息.
任何建议表示赞赏.
谢谢
Car*_*ini 18
要指定这些值,您必须创建自己的RetryPolicy
接口实现.
以下只是一个示例 - 按照您的规则制作您自己的实现:
public class CustomRetryPolicy implements RetryPolicy {
private final int readAttempts;
private final int writeAttempts;
private final int unavailableAttempts;
public CustomRetryPolicy(int readAttempts, int writeAttempts, int unavailableAttempts) {
this.readAttempts = readAttempts;
this.writeAttempts = writeAttempts;
this.unavailableAttempts = unavailableAttempts;
}
@Override
public RetryDecision onReadTimeout(Statement stmnt, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataReceived, int rTime) {
if (dataReceived) {
return RetryDecision.ignore();
} else if (rTime < readAttempts) {
return RetryDecision.retry(cl);
} else {
return RetryDecision.rethrow();
}
}
@Override
public RetryDecision onWriteTimeout(Statement stmnt, ConsistencyLevel cl, WriteType wt, int requiredResponses, int receivedResponses, int wTime) {
if (wTime < writeAttempts) {
return RetryDecision.retry(cl);
}
return RetryDecision.rethrow();
}
@Override
public RetryDecision onUnavailable(Statement stmnt, ConsistencyLevel cl, int requiredResponses, int receivedResponses, int uTime) {
if (uTime < unavailableAttempts) {
return RetryDecision.retry(ConsistencyLevel.ONE);
}
return RetryDecision.rethrow();
}
}
Run Code Online (Sandbox Code Playgroud)
这真的很容易......然后你把它传递给你Cluster
......
RetryPolicy rc = new CustomRetryPolicy(3, 3, 2);
Cluster cluster = Cluster.builder().addContactPoint("192.168.0.30").withRetryPolicy(rc).build();
Run Code Online (Sandbox Code Playgroud)
HTH,卡罗