Introduction

Home

Quick Start


© 2019-2022 rxmicro.io. Free use of this software is granted under the terms of the Apache License 2.0.

Copies of this entity may be made for Your own use and for distribution to others, provided that You do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.

If You find errors or omissions in this entity, please don’t hesitate to submit an issue or open a pull request with a fix.

Microservices - also known as the microservice architecture - is an architectural style that structures an application as a collection of services that are:

(Read more at https://microservices.io/…​)

Thus, a microservice project consists of several microservices. Each microservice must fulfill only one business task.

Let’s look at a microservice that displays the current date and time in UTC format:

public final class MicroService1 {

    public static void main(final String[] args) {
        System.out.println(Instant.now());
    }
}

Does this microprogram constitute a microservice?
Yes, since this microprogram fulfills a business task.
Unfortunately, this program has a serious disadvantage: it interacts with clients through the console. Therefore, only a client’s program with a console interface launched in a session of the current logged-in OS user will be able to interact with this microservice! This restriction makes it impossible to scale this microservice!

Can we improve this situation? Yes, we can:

public final class MicroService2 {

    public static void main(final String[] args) throws Exception {
        Files.write(
                Paths.get("/var/microservice/now-instant.txt"),
                Instant.now().toString().getBytes(UTF_8)
        );
    }
}

This microservice uses a file system to interact with client’s programs. In this way, the only requirement for the client’s program is to be run on the same computer on which the microservice is running. The situation has improved, but it is still impossible to scale this microservice horizontally!

Can we improve this situation? Yes, we can:

public final class MicroService3 {

    public static void main(final String[] args) throws Exception {
        try (final ServerSocket serverSocket = new ServerSocket(8080)) {
            try (final Socket clientSocket = serverSocket.accept()) {
                try (final OutputStream out = clientSocket.getOutputStream()) {
                    // read command from input stream
                    out.write(Instant.now().toString().getBytes(UTF_8));
                }
            }
        }
    }
}

Now, the microservice uses the network to interact with clients. This implementation of the microservice is scalable as the microservice can now be run on several networked computers. The situation has improved markedly, but there are problems with networking:

Can we improve this situation? Yes, for this purpose You can use the HTTP protocol with the REST architecture style:

public final class MicroService4 {

    public static void main(final String[] args) throws IOException {
        final HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 8080), 0);
        server.createContext("/now-instant", exchange -> {
            final String content = Instant.now().toString();
            exchange.sendResponseHeaders(200, content.length());
            exchange.getResponseHeaders().add("Content-Type", "text/txt");
            try (final OutputStream body = exchange.getResponseBody()) {
                body.write(content.getBytes(UTF_8));
            }
        });
        server.start();
    }
}

That’s why microservices are often referred to as REST-based microservices

For simple tasks, the entire logic of the microservice can be found in one class, which is often called microservice. If a microservice has to solve a complex task, then this microservice is divided into two logical components:

  1. REST controller, the main task of which is:

    1. to accept HTTP requests;

    2. to validate HTTP requests;

    3. to convert HTTP requests into Java models;

    4. to invoke request handlers;

    5. once the response model is received, convert it to an HTTP response.

  2. Business service, the main task of which is:

    1. if the task is of medium complexity, then independently calculate the result and return it to the REST controller;

    2. if it is a high-complexity task, then decompose it into sub-tasks and delegate its execution to other microservices. After all sub-tasks have been completed, merge the result and return it to the REST controller.

Therefore, the following is implied in this guide:

  1. If You find the term microservice, it means REST-based microservice, unless stated otherwise!

  2. If You find the term REST controller, it means a logical component of the microservice that performs its direct functions!

Introduction

Home

Quick Start