Java Microservices: A Practical Guide

You can use this guide to understand what Java microservices are, how you will develop and create them. And also get an overview of libraries for developing Java microservices.


Note: An article of ~ 7000 words is probably not worth reading on a mobile device. Bookmark it and come back later.


Content



The basics of Java microservices


To get a real idea of ​​Java microservices, it makes sense to start with the very basics: the infamous monolith in Java, what it is and what its advantages or disadvantages are.


What is a Java monolith?


Imagine that you are working for a bank or a fintech startup. You provide users with a mobile application that they can use to open a new bank account.


This Java code requires a controller class, which, simplifiedly , looks something like the one shown below.


@Controller
class BankController {

    @PostMapping("/users/register")
    public void register(RegistrationForm form) {
        validate(form);
        riskCheck(form);
        openBankAccount(form);
        // etc..
    }
}

Do you want to:


  1. Validate the registration form.
  2. Check the risk at the user's address to decide whether you want to open a bank account for him or not.
  3. Open bank account

BankController bank.jar bank.war : , , . ( .jar / .war 1-100 ).


.jar — , Java.



Java ?


, Java . , :


  1. / / ...
  2. — ...

bank.jar , .


Java?


, . bank.jar JVM, . .


: « , Mono (lithic) Bank, , , , Java.


Java?


, riskCheck() BankController / bean- Maven / Gradle, . .


RiskCheck , , ( ).


  1. , 5-7 ?
  2. 100 1000 ?
  3. - ?

, , :


  1. — .
  2. , .

, : JVM, . JVM RiskCheck, JVM. .


?


Java ?


, : .


HTTP/REST


HTTP REST- , XML JSON — (, , Google Protocol Buffers).


REST-, , , : , .


Java REST?



JMS / , AMQP. , / SMTP.


, , , « », -, , , - .


Java?


: REST API Java


, , Java . , , HTTP-.


@Controller
class BankController {

    @Autowired
    private HttpClient httpClient;

    @PostMapping("/users/register")
    public void register(RegistrationForm form) {
        validate(form);
        httpClient.send(riskRequest, responseHandler());
        setupAccount(form);
        // etc..
    }
}

, , Java () : RiskCheck. JVM, . :



, Java Microservices: ( .jar .war) .


: ? ? ?


.


Java


. , .



— . , «» , — .


.


:


. :


  1. / / .
  2. , , , , , .
  3. , : , .

, Java- — .


  1. , « », , , , .
  2. « », .
  3. , PDF .

: -


UML- , . , , . ?


, , , , ().


, , , 7- Hibernate , , , .


, ? , , .


@simonbrown Twitter:


… , .
I'll keep saying this… if people can't build monoliths properly, microservices won't help.
Simon Brown


Java. , :


  1. , .
  2. , .
  3. : : , (: agile :) )

, Java .



, . Hadi Hariri (https://twitter.com/hhariri) «Extract Microservice» IntelliJ.



, , , , .



@Service
class UserService {

    public void register(User user) {
        String email = user.getEmail();
        String username =  email.substring(0, email.indexOf("@"));
        // ...
    }
}

Java


@Service
class UserService {

    @Autowired
    private HttpClient client;

    public void register(User user) {
        String email = user.getEmail();
        // now calling the substring microservice via http
        String username =  httpClient.send(substringRequest(email), responseHandler());
        // ...
    }
}

, , , Java HTTP, . , , : Java.


: .


, workflow ( )


Java .


: , () , CRM .


, , , XML.


XML- ():


  1. , XML
  2. : 1- ?
  3. XML
  4. XML ,
  5. , « » «, — »

, .


. , , RabbitMQ, .



, , , :


  1. XML-?
  2. ? ? API?
  3. , ? ?
  4. (, , ), , ?
  5. / .

, , , . :



, , .


...


  • , .
  • , , , .
  • , , .
  • , (. Java ?).
  • , — .

:


:


  • Netflix ( ) ...
  • , , Chaos Monkey, , 5 .
  • , @monzo 1500 , .

→ .


. .:
Chaos Monkey — Netflix stateless- — - . Chaos Monkey , .

.


— . (, ) , ( XML, XML, XML).


, , Java , , . .


, DevOps / / .


-


, : ( : ).


, XML Java, Haskell ( ), Erlang ( :) ).


, ( ), , , : .


, , Haskell , .


: . Fortune 500 Spring, « ». — , .


: , . : Kotlin Java ( JVM 100% ), Haskell Java.


Java


, . Java-, , — .jar / .war.


Java, , , JVM: Java , , , Java, JVM).


, , Docker, Kubernetes The Cloud. ? :


Java


, monobank.jar () riskengine.jar ( ).


, , , .properties, URL .


, , :


-r-r------ 1 ubuntu ubuntu     2476 Nov 26 09:41 application.properties
-r-x------ 1 ubuntu ubuntu 94806861 Nov 26 09:45 monobank-384.jar

ubuntu@somemachine:/var/www/www.monobank.com/java$ java -jar monobank-384.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
...

-r-r------ 1 ubuntu ubuntu     2476 Nov 26 09:41 application.properties
-r-x------ 1 ubuntu ubuntu 94806861 Nov 26 09:45 risk-engine-1.jar

ubuntu@someothermachine:/var/www/risk.monobank.com/java$ java -jar risk-engine-1.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
...

: .properties .jar ?


, .


, SSH Ansible Java


, Java- — , Java- 20 . :


  • (Maven, Gradle)
  • SSH / SCP .jars
  • Bash
  • : Ansible.

«» , chaos monkeys, , , ZooKeeper , .


, , .


Docker Java


. Docker .


, , :




, JVM . JVM.zip Raspberry Pi ( ), .jar.


, PHP Python, .


, Java- ( ): , Postgres, , Redis.


, Docker Java, Java, :


  • , Testcontainers.
  • "" . Discourse. Docker , , : Discourse, Ruby, Postgres, Redis.

Oracle , Docker.


, , .jar, :


  • JAR Docker
  • Docker

Docker Swarm Kubernetes Java


, Docker. , Java, Docker, .jar. Java, : .


: , Docker, , , (brrrr)?


— Docker Swarm Kubernetes.


, : , YAML (. : Yaml) . , , .


, Java :


  • Docker Swarm / Kubernetes
  • YAML, .

Java


, , n- ? , , ?


:


  1. ( , Spring Boot), Wrapper.java — , . .
  2. Docker Swarm Kubernetes .
  3. . DEV / TEST. , , .

, Java , , (, ActiveMQ RabbitMQ) , , , Java .


DevOps. Microservice Testing Libraries, .


, :


Java


, Java, , .


Java ?


, JVM
.


( , JVM ), .


, : , .


, , BillingService.


HTTP / REST


, . , - PDF.


, HTTP. ( , PDF . .)


@Service
class BillingService {

    @Autowired
    private HttpClient client;

     public void bill(User user, Plan plan) {
        Invoice invoice = createInvoice(user, plan);
        httpClient.send(invoiceRequest(user.getEmail(), invoice), responseHandler());
        // ...
    }
}

, HTTP-. , :


  1. : , .
  2. DELAYED: , .
  3. ERROR: , , .

, , . , API, .


Chaos Monkey, , , , .


— . , , 50 10 . , , BillingService . , .


, , , , , , (, , , )


, , Hystrix Netflix. , .



. BillingService , , Spring RabbitMQ .


, RabbitMQ, , . PDF .


@Service
class BillingService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

     public void bill(User user, Plan plan) {
        Invoice invoice = createInvoice(user, plan);
        // converts the invoice to,for example, json and uses it as the message's body
        rabbitTemplate.convertAndSend(exchange, routingkey, invoice);
        // ...
    }
}

, OK ERROR, HTTP-. :


  1. ? ? ( -).
  2. ? ? ( ).
  3. : « / » « ?» ( -).

, . , , . :


  • JMS, ActiveMQ, (XA) .
  • RabbitMQ, , , , .
  • - , , ActiveMQ RabbitMQ , Docker.

Java- ?


-, , Spring Boot, .jar, -, Tomcat Jetty, . .


, , , Kubernetes GraalVM, .


: Quarkus, Micronaut, Vert.x, Helidon.


, , , , :


Spring Boot, , , , , Spring Boot .


, , , , , , - , , 50 .


, , , () « » , , , , .


.


:


  • ORM .
  • .
  • , ( , Hibernate), .

(, , , DevOps, ) , hello world. , , , JRebel DCEVM.


(Simon Brown): ( ) , ( ) — .


, .


REST Java?


HTTP REST API. , , HTTP:


Http Java ( Java 11), Http Apache OkHttp.


, «», , JAX-RS WebSocket.


, HTTP-, HTTP- . OpenFeign .


Java?


, , , ActiveMQ (Classic Artemis), RabbitMQ Kafka. , .


:


  • ActiveMQ RabbitMQ , . .
  • ActiveMQ ( ), RabbitMQ / Docker / TestContainer.
  • Kafka . , «» ( ), .

, RabbitMQ ( ) Kafka, Pivotal .


, , . , - , RabbitMQ ActiveMQ.


, RabbitMQ , 20-30K . Kafka 100K . , , .


: Alibaba Group, , ( ) . , — 99% - Java.


.


?


Spring ( Spring) - Arquillian ( JavaEE).


Docker Testcontainers, , , Oracle , .


HTTP-, Wiremock. (ActiveMQ) (RabbitMQ), Awaitility DSL.


, «», Junit, TestNG AssertJ Mockito.


, , , , .


Java?


— . , , less grep, n — , .


. , « » .


:



?


, , IPS. . , [ip = 192.168.200.1] , [ip = 192.168.200.2], .


:


  • application.properties , , .
  • (, Amazon EC2 IP-, - «» ), , , , , «» , IP .
  • , , , : , , , , ? , ? ?

, , .


, Eureka Zookeeper, «» , , , , . , .


, - ZooKeeper.


Java?


, . , HTTPS Oauth2 .


, ?


, , . Docker / Testcontainers, / Ansible.


.


: Yaml


, Yaml. , « ». , Ansible, Kubernetes.


«» YAML, Ansible , , , IDE. , .


Yaml:
  - is:
    - so
    - great

? ? ?


, . , .



Java, , . , .



, , . ?


, - . - n .


, - Java-IoT. , , . ..


, , , , « ». , - domain driven design , , « » , , , .


n ( ) « » , .



( (!)) , :


. 10 , , , . .


. .



, , :


, () , , . (.. ).


, .



, 20 ( ) 20 . , - 400 Hibernate .


: , .


, , , .


: Java- , , .



, , (, , ), , .


, , :


@Controller
class LoginController {

    // ...

    @PostMapping("/login")
    public boolean login(String username, String password) {
        User user = userDao.findByUserName(username);
        if (user == null) {
            // handle non existing user case
            return false;
        }
        if (!user.getPassword().equals(hashed(password))) {
            // handle wrong password case
            return false;
        }
        // 'Yay, Logged in!';
        // set some cookies, do whatever you want
        return true;
    }
}

(, , ): , «» UserStateChanged — - -.


Java , UserStateChanged Erlang. - - , Steve Yegge , , Google.


, , - .


? .


, .



, , , , [95: login-101: updateUserProfile].


, , , - — .


, , .



: . , , , : , .


№ 47?


? ?


?


Erlang , ?


, ! ?


, , DevOps, , , , . , .



, , . — , .



Java- — . — - Maven . .


, , Maven, .



, DevOps (, Ansible Heroku ), .


Java . . , - ( ), .


Siva Prasad Reddy ( ) :


, , 70% 30% -.

Java?


, , Google . , , -, , .



, Java, Hetzner. , Hetzner.


, Java- , , , (<10).



How many database connections should your Java monolith (connection pool) open on your database server?


Why? How many active users can (approximately) scale your monolith at the same time?


Answer


Leave your answer to these questions in the comments section. I look forward to all the answers.


Now make your choice yourself


If you are still here with me: Thanks for reading!


All Articles