Sie sind auf Seite 1von 12

Eclipse Vert.

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 Request /Indonesia

HTTP Response “hello Firmansyah”

HTTP Response “hello Indonesia”

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

This command generates:


1. The Maven project structure,
2. Configures the vertx-maven-plugin, and
3. Creates a verticle class (io.vertx.chapter03.HttpMicro01),
4. Adds the vertx-web dependency, Vert.x Web is a module
that provides everything you need to build modern web
applications on top of Vert.x.
01. Create HttpMicro01 Class
c. Modify io.vertx.chapter03.HttpMicro01 Class
1. Create method for JSON HTTP response (Vert.x provides a
JsonObject class to create and manipulate JSON
structures.):

private void helloJson(RoutingContext rc) {


String message = "hello";
if (rc.pathParam("name") != null) {
message += " " + rc.pathParam("name");
}
JsonObject json = new JsonObject().put("message", message);
rc.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(json.encode());
}

2. Add logic for HTTP Server creation, routes and


parameters request handling inside the start()
method:
01. Create HttpMicro01 Class
public void start() {
Router router = Router.router(vertx);
router.get("/").handler(this::helloJson);
router.get("/:name").handler(this::helloJson);

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

e. Run this application, launch:


java -jar target/http-micro-vertx-app-1.0-SNAPSHOT.jar

f. Browse URL http://localhost:8080/ and look at


the application output.
g. Use Ctrl + C to shut down.
02. Create HttpMicro02 Class
a. Create a directory called “consumer” inside
chapter03 folder
b. Generate the new project structure using
maven inside consumer folder:
mvn io.fabric8:vertx-maven-plugin:1.0.5:setup \
-DprojectGroupId=io.vertx.chapter03 \
-DprojectArtifactId=http-micro02-vertx-app \
-Dverticle=io.vertx.chapter03.HttpMicro02 \
-Ddependencies=web,web-client,rx

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

Das könnte Ihnen auch gefallen