故障转移

932 发布于: 2021-03-25 读完约需 1 分钟

故障转移(Fail over)

当使用具有多个节点的连接池时,如果对节点的调用抛出异常或返回502、503或504响应,则将重试请求

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .ClientCalls(r => r.FailAlways())
    .ClientCalls(r => r.OnPort(9201).SucceedAlways())
    .StaticConnectionPool()
    .Settings(s => s.DisablePing())
);

audit = await audit.TraceCall(
    new ClientCall {
        { AuditEvent.BadResponse, 9200 },
        { AuditEvent.HealthyResponse, 9201 },
    }
);

502错误网关(502 Bad Gateway)

502错误网关将被视为一个需要重试的错误:

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .ClientCalls(r => r.FailAlways(502))
    .ClientCalls(r => r.OnPort(9201).SucceedAlways())
    .StaticConnectionPool()
    .Settings(s => s.DisablePing())
);

audit = await audit.TraceCall(
    new ClientCall {
        { AuditEvent.BadResponse, 9200 },
        { AuditEvent.HealthyResponse, 9201 },
    }
);

503服务不可用(503 Service Unavailable)

503服务不可用也将被视为一个需要重试的错误:

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .ClientCalls(r => r.FailAlways(503))
    .ClientCalls(r => r.OnPort(9201).SucceedAlways())
    .StaticConnectionPool()
    .Settings(s => s.DisablePing())
);

audit = await audit.TraceCall(
    new ClientCall {
        { AuditEvent.BadResponse, 9200 },
        { AuditEvent.HealthyResponse, 9201 },
    }
);

504网关超时(504 Gateway Timeout)

504网关超时也将被视为一个需要重试的错误:

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .ClientCalls(r => r.FailAlways(504))
    .ClientCalls(r => r.OnPort(9201).SucceedAlways())
    .StaticConnectionPool()
    .Settings(s => s.DisablePing())
);

audit = await audit.TraceCall(
    new ClientCall {
        { AuditEvent.BadResponse, 9200 },
        { AuditEvent.HealthyResponse, 9201 },
    }
);

如果调用返回的是一个有效的HTTP状态码,而不是502或503,那么请求将不会被重试。

不同的请求可能有不同的被认为有效的状态码。例如:404 Not Found响应是索引不存在请求的有效状态码

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .ClientCalls(r => r.FailAlways(418))
    .ClientCalls(r => r.OnPort(9201).SucceedAlways())
    .StaticConnectionPool()
    .Settings(s => s.DisablePing())
);

audit = await audit.TraceCall(
    new ClientCall {
        { AuditEvent.BadResponse, 9200 },
    }
);

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

发表评论

登录用户才能发表评论, 请 登 录 或者 注册