Beruflich Dokumente
Kultur Dokumente
x
Chapter 03
HTTP Based Microservices
Firmansyah.profess@gmail.com
2018
00. Sequence Diagram
We will create two verticle classes with following
sequence diagram to demonstrate HTTP based
Microservices
HttpMicro02 HttpMicro01
HTTP Request
HTTP Request /Firmansyah
HTTP Response
01. Create HttpMicro01 Class
a. Create a directory called “chapter03”
b. Generate the project structure using
maven inside chapter03 folder:
mvn io.fabric8:vertx-maven-plugin:1.0.5:setup \
-DprojectGroupId=io.vertx.chapter03 \
-DprojectArtifactId=http-micro-vertx-app \
-Dverticle=io.vertx.chapter03.HttpMicro01 \
-Ddependencies=web
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8080);
}
Notes:
• This code creates an HTTP server on port 8080 and registers a
requestHandler and use the accept method of the router.
• Vert.x Web provides a Router on which we can register Routes.
Routes are the mechanism by which Vert.x Web checks the path
and invokes the associated action.
• Once we have created the Router object, we register two
routes. The first one handles requests on / and just writes
hello. The second route has a path parameter (:name).
The handler will call helloJson to return JSON response
01. Create HttpMicro01 Class
d. Package app as a fat jar, launch:
mvn clean package
Notes:
1. The last command adds another dependency: the Vert.x web
client, an asynchronous HTTP client. We will use this client
to call the first microservice (HttpMicro01 class).
2. The command has also added the Vert.x RxJava binding.
02. Create HttpMicro02 Class
c. Modify io.vertx.chapter03.HttpMicro02
1. In the start method, we create a WebClient and a Router.
2. On the created router, we register a route on “/” and start the
HTTP server, passing the router accept method as
requestHandler.
3. The handler of the route is a method
invokeHttpMicro01() with following logic:
• Uses the web client to invoke the first microservice with a
specific path (/firmansyah)
• The handler we passed in into request.send() is
invoked when either the response arrives or an error
occurs.
• When it succeeds, we write the received payload to the
response; otherwise, we reply with a 500 response
02. Create HttpMicro02 Class
d. Run this application, launch:
mvn compile vertx:run
e. Browse URL http://localhost:8081/ and look at
the application output.
f. Shutdown HttpMicro01 App and refresh the
browser, look at the application output.
03. Use RxJava
a. Now let’s change the current behavior to call
the HttpMicro01 twice with two different (path)
parameters and we want to write a response
assembling both results.
b. Using callback as previous code will be too
complicated, we can use reactive programming
and RxJava to simplify this code.
c. Modify invokeHttpMicro01() method
inside HttpMicro02 class.
Thank You!
Any questions? You can find me at
firmansyah.profess@gmail.com
Credits
PPT: ALLPPT.com
Music: https://www.bensound.com