使用 ProxyOptions 构建 Vert.x WebClient
在构建 Vert.x 4.5 WebClient 时配置显式 HTTP 代理,并让目标请求始终发往该服务。
ProxyOptions proxy = new ProxyOptions()
.setType(ProxyType.HTTP)
.setHost("proxy.example")
.setPort(8080);
WebClient client = WebClient.create(vertx,
new WebClientOptions().setProxyOptions(proxy));
client.getAbs("https://example.com/").send()
.onSuccess(response -> System.out.println(response.statusCode()))
.onFailure(Throwable::printStackTrace);对于 Vert.x 4.5.7,这假设已存在 Vertx 实例以及相应的 core/WebClient 类。请求的 URL 仍然是网站本身;ProxyOptions 负责选择中间人。在这个公网站点示例中,当该线路被允许且不需要代理账号时,请从HTTP 代理列表中选择一个 HTTP 端点。
如果部署值来自 JVM 属性,请在构建这些选项之前读取并校验它们。System 中存在某个值,并不能证明这个客户端导入过它。
选择端点要求的代理类型,必要时使用受支持的用户名/密码字段。HTTPS 请保留常规的 TLS 校验。
让这个客户端复用于它的工作负载,之后将其关闭。构建另一个已配置的客户端不会影响仍在通过旧实例发送的请求。