404 Errors using Vert.x web proxy code taken from its documentation - webserver

I am using the Vert.x Web library to create a test application for a reverse proxy. I am using the information provided on the Vert.x site:
https://vertx.io/docs/4.1.0/vertx-web-proxy/java/#_using_vert_x_web_proxy
I have written some simple test code, based on the documentation at the above site:
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);
}
}
I have a web server (the so- called "origin" in Vert.x terminology) running on Port 8080. The only difference between the code above and the code on the web site is that I am using different port numbers for the proxy and the origin server. According to the documentation on the Vert.x website, a browser going to http://localhost:8010/foo should access the origin server.
Instead, I am getting 404 Not found errors.
Is there something missing in this code? Maybe something not covered in the documentation?
Does anyone have any idea how to make it work properly?

#Factor Three you are missing backend part of the guide and also you are mixing parts in an unorganized way. Following the guide it can be like this:
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));

Related

Strange issue with Vertx Http request

I configured an HTTPS website on AWS, which allows visiting from a white list of IPs.
My local machine runs with a VPN connection, which is in the white list.
I could visit the website from web browser or by the java.net.http package with the below code:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://mywebsite/route"))
.GET() // GET is default
.build();
HttpResponse<Void> response = client.send(request,
HttpResponse.BodyHandlers.discarding());
But if I replaced the code with a Vertx implementation from io.vertx.ext.web.client package, I got a 403 forbidden response from the same website.
WebClientOptions options = new WebClientOptions().setTryUseCompression(true).setTrustAll(true);
HttpRequest<Buffer> request = WebClient.create(vertx, options)
.getAbs("https://mywebsite/route")
.ssl(true).putHeaders(headers);
request.send(asyncResult -> {
if (asyncResult.succeeded()) {
HttpResponse response = asyncResult.result();
}
});
Does anyone have an idea why the Vertx implementation is rejected?
Finally got the root cause. I started a local server that accepts the testing request and forwards it to the server on AWS. The testing client sent the request to localhost and thus "Host=localhost:8080/..." is in the request header. In the Vert.X implementation, a new header entry "Host=localhost:443/..." is wrongly put into the request headers. I haven't debug the Vert.X implementation so I have no idea why it behaviors as this. But then the AWS firewall rejected the request with a rule that a request could not come from localhost.

stompjs socket connection in kubernetes

I'm using stompjs, sock-client to establish a socket connection from a react-app to a springboot app. Both front-end and back-end are hosted in the same kubernetes cluster, but in different namespaces. Both are load balanced.
When running locally, this works fine:
UI:
const endpoint = 'http://localhost:8080/websocket';
let socket = new SockJs(endpoint);
stompClient = StompJs.over(socket);
stompClient.connect({}, this.onStompConnectSuccess, this.onStompErrorCallback);
Server side:
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").setAllowedOrigins("http://localhost:3000").withSockJS();
}
However what URL do i use for the hosted versions? Setting the load-balanced URLs gives an error
Whoops! Lost connection to https://load-balanced-url/websocket

how to implement rest client with gluon plugin

I am following the gluon rest sample to access to a rest server. But I get nothing from the server. the object is null. so what is wrong with the code. The object is there and reachable by http.
RestClient restClient = RestClient.create()
.method("GET")
.host("https://storage.waw.cloud.ovh.net")
.path("/v1/AUTH_17fd5ed14d1440b0abc4918f6a492bd9/dataCours/pourShema.json")
.contentType("charset=utf-8");
InputStreamInputConverter<CoursFrancaisJson> converter = new JsonInputConverter<>(CoursFrancaisJson.class);
GluonObservableObject<CoursFrancaisJson> retrieveObject = DataProvider.retrieveObject(restClient.createObjectDataReader(converter));

feign request microservice using domain url

I am invoking microservice api using feign like this now:
Response<List<AppResponse>> apps = appController.getApps();
And this is server side:
#RequestMapping(value = "/app")
#FeignClient(name = "soa-service")
public interface IAppController {
#GetMapping(value = "/list")
Response<List<AppResponse>> getApps();
}
Because the client side and server side registerd to eureka(the eureka could find the internal registed ip address),the invoke works fine.My question is : when the client and server not in one network(maybe the client not registed to eureka and deploy to external net). Is it possible to invoke microservice using domain url like "www.api.example.com/app/list"?
ps:I know one solution to change my invoke using okhttpclient,but the problem is: I must change all old feign invoke to new okhttp rest invoke.
#RequestMapping(value = "/app")
#FeignClient(name = "soa-service", url = "http://www.api.example.com/app/list")
public interface IAppController {
#GetMapping(value = "/list")
Response<List<AppResponse>> getApps();
}

Why does the webflux client give a 404?

I'm trying to make a webflux client to connect to a remote websocket. There is an example websocket located at https://www.websocket.org/echo.html. I can have my browser make a wss request there simply by pressing "connect". In my browser developer toolbar I can then see that a succesful request is made to wss://echo.websocket.org/?encoding=text
This url is also mentioned in https://stackify.com/reactive-spring-5/ (the code there doesn't work because "input" is not defined).
However, when I try to access the same url from spring boot 2.0.0 with webflux, I get a 404:
#PostConstruct
public void run() throws URISyntaxException {
WebClient webClient = WebClient.create();
Flux<String> r =
webClient.get().uri("wss://echo.websocket.org/?encoding=text")
.retrieve()
.bodyToFlux(String.class)
.log();
r.subscribe(res -> System.out.println("ok: " + res), ex -> System.out.println(ex));
}
error:
org.springframework.web.reactive.function.client.WebClientResponseException: ClientResponse has erroneous status code: 404 Not Found
My best guess is that it somehow does not work with urls that start with "wss://". Can I change the code so that the request will be successful?
WebClient is not a WebSocket client, ReactorNettyWebSocketClient (mentioned in the article you linked) is a WebSocketClient. Please use this instead.