我只是想确保这是实现这一目标的最理想方式。
设置如下:基本上,我们有 3 台通过 DNS 轮询“平衡”的服务器。每台服务器上都配置了 Varnish,后面有一个标准的 lamp 堆栈。
基本上,当请求到达时,我们会检查默认后端是否健康,如果不健康,我们会返回到我们的导向器,它会轮流使用其他两个服务器,直到默认后端再次健康。所以我们只希望 Varnish 始终使用本地主机,除非我们的后端不健康。这是我的代码:
probe healthcheck {
.url = "/info.php";
.timeout = 1s;
.interval = 4s;
.window = 5;
.threshold = 3;
.expected_response = 200;
}
# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8080";
.probe = healthcheck;
}
#Cluster nodes
backend lamp02 {
.host = "192.168.0.102";
.port = "8080";
.probe = healthcheck;
}
backend lamp03 {
.host = "192.168.0.103";
.port = "8080";
.probe = healthcheck;
}
sub vcl_init {
new server_pool = directors.round_robin();
server_pool.add_backend(lamp02);
server_pool.add_backend(lamp03);
}
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if (!std.healthy(req.backend_hint)) {
set req.backend_hint = server_pool.backend();
} else {
set req.backend_hint = default;
}
}
这是最有效的方法吗?
谢谢!
答案1
是的。您可以采取积极的方式,而不是消极的方式,如果您的 VCL 变得复杂,这可能会读起来更好,但您的方法很好。
if (std.healthy(req.backend_hint)) {
set req.backend_hint = default;
} else {
set req.backend_hint = server_pool.backend();
}