404错误使用Vert.X web代理代码取自其文档



我使用的是Vert。为反向代理创建一个测试应用程序。我正在使用Vert上提供的信息。x网站:

https://vertx.io/docs/4.1.0/vertx-web-proxy/java/_using_vert_x_web_proxy

我根据上面站点的文档编写了一些简单的测试代码:

public class WebTest
{
public static void main(String[] args)
{
Vertx vertx = Vertx.vertx();

HttpClient client = vertx.createHttpClient();
HttpProxy proxy = HttpProxy.reverseProxy(client);
proxy.origin(8080,"localhost");

HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);

router.route(HttpMethod.GET,"/foo").handler(ProxyHandler.create(proxy));
server.requestHandler(router);
server.listen(8010);
}
}

我有一个web服务器(所谓的" original "在绿色。在端口8080上运行。上述代码与网站上的代码之间的唯一区别是,我为代理服务器和原始服务器使用了不同的端口号。根据Vert上的文件。X网站,浏览器转到http://localhost:8010/foo应该访问源服务器。

相反,我得到404 Not found errors.

这段代码中缺少什么吗?也许是文档中没有提到的?

有谁知道如何使它正常工作吗?

@因素三:您缺少指南的后端部分,并且您以无组织的方式混合部分。按照指南,它可以像这样:

Vertx vertx = Vertx.vertx();
// Origin Server (Backend 8080)---------
// Here you deploy the real server with the endpoint ("hello world" in this case)
HttpServer backendServer = vertx.createHttpServer();
Router backendRouter = Router.router(vertx);
backendRouter
.route(HttpMethod.GET, "/foo")
.handler(
rc -> {
rc.response().putHeader("content-type", "text/plain").end("hello world");
});
backendServer.requestHandler(backendRouter).listen(8080);
// Proxy Server (8010)
// Here you deploy the server that will act as a proxy
HttpServer proxyServer = vertx.createHttpServer();
Router proxyRouter = Router.router(vertx);
proxyServer.requestHandler(proxyRouter);
proxyServer.listen(8010);
// Proxy Magic
// Here you route proxy server requests (8010) to the origin server (8080)
HttpClient proxyClient = vertx.createHttpClient();
HttpProxy httpProxy = HttpProxy.reverseProxy(proxyClient);
httpProxy.origin(8080, "localhost");
proxyRouter.route(HttpMethod.GET, "/foo").handler(ProxyHandler.create(httpProxy));

最新更新