Skip to main content
Web
- We can use vertex to create the REST API.
- We can also use vertex web client.
- Vertex is very modular and the logic for the server and clients is split up into different packages.
- Server
- Vertex is built on top of Netty, A very popular reactive HTTP server.
- Vertex web supports request routing.
- Request Routing is non blocking and asynchronous.
- The server supports reactive HTTP request Processing Jason binding with Jackson.
- The server also supports Web sockets and HTTP version 2.0
- Client
- In vertex web client, we can use functionality such as asynchronous HTTP and HTTP version 2 cause.
- The client supports Jason body encoding And decoding out of the box.
- The client can be used for the request, response streaming, and the client supports the use of an Rx Java 2 API.
- Http Routing Get Requests
- Define a vertex web router in the vertex main vertical start method.
- The router has methods like HTTP, get, Post, which helps us in defining HTTP endpoints.
- We can pass a route in these End points.
- After the route method is defined, we define a handler
- The handler is asynchronously called When a client call the get end point.
- In the handler, we get a routing context which gives us access to request related information.
- From this handler, we can return our best response using Routing Context’s response method which returns an HttpResponse and end method of HttpResponse.
- context.response().end(response)
- We can pass buffer or string in the end function.
- We pass this router in the HTTP request handler method.
- We can also pass an exception handler after the request, handler method.
- Testing Rest API using vertex web client
- Vertex web client is an asynchronous http client which can be used for testing as well as in the application itself.
- It can be used as a client for other rest API's and also as proxy.
- We add dependency vertex-web-client
- Body Handler
- Body handler is used to parse HTTP Request Bodies.
- If body handler is not enabled on the route, then routingContext getBodyAsJson() Is always NULL.
- To be able to parse request bodies On the server side We need to register a body handler.
- A body handler can be registered via route For a particular route or can be registered globally.
- We have to chain a body handler Before other handlers, where we are passing request bodies.
- When HTTP, request reaches our Web server body handler will be called.
- If a request body is available, the body will be added to the request context, and after that, the handler forwards it to the next handler.
- Vertex web server
- Vertex is a very resource, efficient and highly scalable tool kit.
- It uses a non-blocking HTTP server to handle a lot of HTTP requests.
- Vertex uses Netty away popular HTTP server On backend.
- We can scale our web server to deploy our REST API on multiple single event loops.
- If our server has more than one CPU processor, this approach would utilise the resources fully.
- HTTP server runs on one event loop thread And all request are always Handled on the same thread.
- The server starts on a thread And for each end point, Call same thread processes, the request and response.
- Vertex will never respond from another thread.
- All the other routes Also respond and are scheduled on the same thread.
- Multiple HTTP servers can be started on multiple threads with vertex.
- The load Will be distributed over multiple event loops, for example, one HTTP server per CPU core Can be started.
- If we have four CPU cores We can start four server threads.
- The addition of threads Depends on the usage and performance of application.
- We have 4 HTTP servers on four event loop threads.
- The thread zero is reserved for the main vertical.
- When the server is responding To multiple requests, The request are distributed Over multiple event loop threads.
- Depending on what thread has time, the request is scheduled.
- References
Comments
Post a Comment