Microservices Best Practices for Java
Total Page:16
File Type:pdf, Size:1020Kb
Front cover Microservices Best Practices for Java Michael Hofmann Erin Schnabel Katherine Stanley Redbooks International Technical Support Organization Microservices Best Practices for Java December 2016 SG24-8357-00 Note: Before using this information and the product it supports, read the information in “Notices” on page vii. First Edition (December 2016) This edition applies to WebSphere Application Server Liberty v9. © Copyright International Business Machines Corporation 2016. All rights reserved. Note to U.S. Government Users Restricted Rights -- Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents Notices . vii Trademarks . viii Preface . ix Authors. ix Now you can become a published author, too! . xi Comments welcome. xi Stay connected to IBM Redbooks . xi Chapter 1. Overview . 1 1.1 Cloud native applications . 2 1.2 Twelve factors . 2 1.3 Microservices . 3 1.3.1 The meaning of “small” . 4 1.3.2 Independence and autonomy . 4 1.3.3 Resilience and Fault tolerance . 6 1.3.4 Automated environment . 6 1.4 Philosophy and team structure . 7 1.5 Examples . 7 1.5.1 Online retail store . 7 1.5.2 Game On! . 8 Chapter 2. Creating Microservices in Java . 9 2.1 Java platforms and programming models . 10 2.1.1 Spring Boot . 10 2.1.2 Dropwizard . 10 2.1.3 Java Platform, Enterprise Edition . 10 2.2 Versioned dependencies. 11 2.3 Identifying services . 11 2.3.1 Applying domain-driven design principles. 12 2.3.2 Translating domain elements into services. 13 2.3.3 Application and service structure . 15 2.3.4 Shared library or new service? . 17 2.4 Creating REST APIs . 18 2.4.1 Top down or bottom up? . 18 2.4.2 Documenting APIs . 18 2.4.3 Use the correct HTTP verb . 19 2.4.4 Create machine-friendly, descriptive results . 20 2.4.5 Resource URIs and versioning . 20 Chapter 3. Locating services . 25 3.1 Service registry . 26 3.1.1 Third-party registration versus self-registration. 26 3.1.2 Availability versus consistency . 27 3.2 Service invocation . 27 3.2.1 Server side . 27 3.2.2 Client side . 29 3.3 API Gateway . 32 © Copyright IBM Corp. 2016. All rights reserved. iii Chapter 4. Microservice communication . 33 4.1 Synchronous and asynchronous. 34 4.1.1 Synchronous messaging (REST) . 34 4.1.2 Asynchronous messaging (events). 35 4.1.3 Examples . 36 4.2 Fault tolerance . 37 4.2.1 Resilient against change. 37 4.2.2 Timeouts . 39 4.2.3 Circuit breakers. 39 4.2.4 Bulkheads . 39 Chapter 5. Handling data . 41 5.1 Data-specific characteristics of a microservice . 42 5.1.1 Domain-driven design leads to entities . 42 5.1.2 Separate data store per microservice . 43 5.1.3 Polyglot persistence . 45 5.1.4 Data sharing across microservices . 45 5.1.5 Event Sourcing and Command Query Responsibility Segregation. 47 5.1.6 Messaging systems . 48 5.1.7 Distributed transactions . 49 5.2 Support in Java . 50 5.2.1 Java Persistence API . 50 5.2.2 Enterprise JavaBeans. 57 5.2.3 BeanValidation . 59 5.2.4 Contexts and Dependency Injection . 61 5.2.5 Java Message Service API . 62 5.2.6 Java and other messaging protocols . ..