Statistics
9
Views
0
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-12-21
Support
Share

AuthorAlex Soto Bueno & Jason Porter [Alex Soto Bueno]

Optimized for Kubernetes, Quarkus is designed to help you create Java applications that are cloud first, container native, and serverless capable. With this cookbook, authors Alex Soto Bueno and Jason Porter from Red Hat provide detailed solutions for installing, interacting with, and using Quarkus in the development and production of microservices. The recipes in this book show midlevel to senior developers familiar with Java enterprise application development how to get started with Quarkus quickly. You’ll become familiar with how Quarkus works within the wider Java ecosystem and discover ways to adapt this framework to your particular needs. You’ll learn how to: Shorten the development cycle by enabling live reloading in dev mode Connect to and communicate with Kafka Develop with the reactive programming model Easily add fault tolerance to your services Build your application as a Kubernetes-ready container Ease development with OpenAPI and test a native Quarkus application

Tags
No tags
Publisher: O'Reilly Media, Inc.
Publish Year: 2020
Language: 英文
File Format: PDF
File Size: 8.0 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
1. Foreword 2. Preface 1. Who Should Read This Book 2. Why We Wrote This Book 3. Navigating This Book 4. Conventions Used in This Book 5. Using Code Examples 6. O’Reilly Online Learning 7. How to Contact Us 8. Acknowledgments 3. 1. Quarkus Overview 1. Developer-Friendly 2. Integration with Kubernetes 3. Memory and First Response Time 4. A Basic Quarkus Workflow 4. 2. Scaffolding 1. 2.1. Scaffolding a Quarkus Project with Maven 2. 2.2. Scaffolding a Quarkus Project with Gradle 3. 2.3. Scaffolding a Quarkus Project with the Quarkus Start Coding Website 4. 2.4. Scaffolding a Quarkus Project with Visual Studio Code 5. 2.5. Live Reloading with Dev Mode
6. 2.6. Serving Static Resources 5. 3. Developing RESTful Services 1. 3.1. Creating a Simple REST API Endpoint 2. 3.2. Extracting Request Parameters 3. 3.3. Using Semantic HTTP Response Status Codes 4. 3.4. Binding HTTP Methods 5. 3.5. Enabling Cross-Origin Resource Sharing (CORS) 6. 3.6. Using Reactive Routes 7. 3.7. Intercepting HTTP Requests 8. 3.8. Secure Connections with SSL 6. 4. Configuration 1. 4.1. Configuring the Application with Custom Properties 2. 4.2. Accessing Configuration Properties Programmatically 3. 4.3. Overwriting Configuration Values Externally 4. 4.4. Configuring with Profiles 5. 4.5. Changing Logger Configuration 6. 4.6. Adding Application Logs 7. 4.7. Advanced Logging 8. 4.8. Configuring with Custom Profiles 9. 4.9. Creating Custom Sources 10. 4.10. Creating Custom Converters
11. 4.11. Grouping Configuration Values 12. 4.12. Validating Configuration Values 7. 5. Programming Model 1. 5.1. Marshalling/Unmarshalling JSON 2. 5.2. Marshalling/Unmarshalling XML 3. 5.3. Validating Input and Output Values 4. 5.4. Creating Custom Validations 5. 5.5. Validating Objects Programmatically 6. 5.6. Injecting Dependencies 7. 5.7. Creating Factories 8. 5.8. Executing Object Life Cycle Events 9. 5.9. Executing Application Life Cycle Events 10. 5.10. Using a Named Qualifier 11. 5.11. Using Custom Qualifiers 12. 5.12. Qualifying and Configuring Annotations 13. 5.13. Creating Interceptors 14. 5.14. Writing Behavioral Tests 15. 5.15. Writing Unit Tests 16. 5.16. Creating Mock Objects 17. 5.17. Creating Mock Objects with Mockito 18. 5.18. Grouping Several Annotations into One with a Meta-Annotation 19. 5.19. Executing Code Before or After a Test 20. 5.20. Testing the Native Executable
8. 6. Packaging Quarkus Applications 1. 6.1. Running in Command Mode 2. 6.2. Creating a Runnable JAR File 3. 6.3. Über-JAR Packaging 4. 6.4. Building a Native Executable 5. 6.5. Building a Docker Container for JAR File 6. 6.6. Building a Docker Container for Native File 7. 6.7. Build and Dockerize a Native SSL Application 9. 7. Persistence 1. 7.1. Defining a Datasource 2. 7.2. Using Multiple Datasources 3. 7.3. Adding Datasource Health Check 4. 7.4. Defining Transaction Boundaries Declaratively 5. 7.5. Setting a Transaction Context 6. 7.6. Programmatic Transaction Control 7. 7.7. Setting and Modifying a Transaction Timeout 8. 7.8. Setup with Persistence.xml 9. 7.9. Setup Without persistence.xml 10. 7.10. Using Entities from a Different JAR 11. 7.11. Persisting Data with Panache 12. 7.12. Finding All Entity Instances with Panache listAll Method 13. 7.13. Finding Individual Entities with Panache findById Method
14. 7.14. Finding Entities Using Panache Find and List Methods 15. 7.15. Obtaining a Count of Entities Using the Panache count Method 16. 7.16. Paginating Through Entity Lists Using the Panache page Method 17. 7.17. Streaming Results via the Panache Stream Method 18. 7.18. Testing Panache Entities 19. 7.19. Using a Data Access Object (DAO) or Repository Pattern 20. 7.20. Using Amazon DynamoDB 21. 7.21. Working with MongoDB 22. 7.22. Using Panache with MongoDB 23. 7.23. Using Neo4j with Quarkus 24. 7.24. Flyway at Startup 25. 7.25. Using Flyway Programmatically 10. 8. Fault Tolerance 1. 8.1. Implementing Automatic Retries 2. 8.2. Implementing Timeouts 3. 8.3. Avoiding Overloads with the Bulkhead Pattern 4. 8.4. Avoiding Unnecessary Calls with the Circuit Breaker Pattern 5. 8.5. Disabling Fault Tolerance 11. 9. Observability
1. 9.1. Using Automatic Health Checks 2. 9.2. Creating Custom Health Checks 3. 9.3. Exposing Metrics 4. 9.4. Creating Metrics 5. 9.5. Using Distributed Tracing 6. 9.6. Custom Distributed Tracing 12. 10. Integrating with Kubernetes 1. 10.1. Building and Pushing Container Images 2. 10.2. Generating Kubernetes Resources 3. 10.3. Generating Kubernetes Resources with Health Checks 4. 10.4. Deploying Services on Kubernetes 5. 10.5. Deploying Services on OpenShift 6. 10.6. Building and Deploying a Container Image Automatically 7. 10.7. Configuring an Application from Kubernetes 8. 10.8. Configuring an Application from Kubernetes with Config Extension 9. 10.9. Interacting with a Kubernetes Cluster Programmatically 10. 10.10. Testing Kubernetes Client Interactions 11. 10.11. Implementing a Kubernetes Operator 12. 10.12. Deploying and Managing Serverless Workloads with Knative 13. 11. Authentication and Authorization
1. Quarkus Security Basics 2. 11.1. Authentication and Authorization with Elytron Properties File Config 3. 11.2. Authentication and Authorization with Elytron Security JDBC Config 4. 11.3. Authorization with MicroProfile JWT 5. 11.4. Authorization and Authentication with OpenId Connect 6. 11.5. Protecting Web Resources with OpenId Connect 14. 12. Application Secrets Management 1. 12.1. Storing Data Using Kubernetes Secrets 2. 12.2. Store Configuration Secrets Securely with Vault 3. 12.3. Cryptography as a Service 4. 12.4. Generate Database Password as Secret 5. 12.5. Authenticating Services Using Vault Kubernetes Auth 15. 13. Quarkus REST Clients 1. 13.1. Using the JAX-RS Web Client 2. 13.2. Using the MicroProfile REST Client 3. 13.3. Implementing a CRUD Client 4. 13.4. Manipulating Headers 5. 13.5. Using REST Client for Multipart Messages 6. 13.6. Using REST Client to Configure SSL
16. 14. Developing Quarkus Applications Using Spring APIs 1. 14.1. Using Spring Dependency Injection 2. 14.2. Using Spring Web 3. 14.3. Using Spring Data JPA 4. 14.4. Using Spring Security 5. 14.5. Using Spring Boot Properties 17. 15. Working with a Reactive Programming Model 1. 15.1. Creating Async HTTP Endpoints 2. 15.2. Streaming Data Asynchronously 3. 15.3. Using Messaging to Decouple Components 4. 15.4. Reacting to Apache Kafka Messages 5. 15.5. Sending Messages to Apache Kafka 6. 15.6. Marshalling POJOs into/out of Kafka 7. 15.7. Using Kafka Streams API 8. 15.8. Using AMQP with Quarkus 9. 15.9. Using MQTT 10. 15.10. Query Using Reactive SQL 11. 15.11. Insert Using Reactive SQL Client 12. 15.12. Using the Reactive MongoDB Client 13. 15.13. Using the Reactive Neo4j Client 18. 16. Additional Quarkus Features 1. 16.1. Creating Templates with the Qute Template Engine 2. 16.2. Rending HTML Using Qute
3. 16.3. Changing the Location of Qute Templates 4. 16.4. Extending Qute Data Classes 5. 16.5. Describing Endpoints with OpenAPI 6. 16.6. Customizing OpenAPI Spec 7. 16.7. Sending Email Synchronously 8. 16.8. Sending Email Reactively 9. 16.9. Creating Scheduled Jobs 10. 16.10. Using Application Data Caching 19. Minikube 20. Keycloak 21. Knative 22. Index
Quarkus Cookbook Kubernetes-Optimized Java Solutions Alex Soto Bueno and Jason Porter
Quarkus Cookbook by Alex Soto Bueno and Jason Porter Copyright © 2020 Alex Soto Bueno and Jason Porter. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Suzanne McQuade Development Editor: Jeff Bleiel Production Editor: Daniel Elfanbaum Copyeditor: Piper Editorial Proofreader: Amanda Kersey Indexer: Potomac Indexing, LLC Interior Designer: David Futato Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest July 2020: First Edition Revision History for the First Edition 2020-07-14: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492062653 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Quarkus Cookbook, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the authors, and do not represent the publisher’s views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-06265-3 [LSI]
(This page has no text content)
Foreword Red Hat, and JBoss before it, have been at the forefront of helping to shape the evolution of enterprise Java since the early 2000s. With an emphasis on open source and growing communities, these two companies have collaborated with other vendors, consultants, individuals, and standards organizations to ensure Java and some of its most popular frameworks have been developed to address new technological waves and to keep Java one of the most popular programming languages around. As a result, you can find Java being used in areas as diverse as IoT applications, web services, health care, and financial services. Yet when the cloud came along it presented some problems that traditional Java frameworks and even the Java Virtual Machine struggled to match. Red Hat was the first vendor to marry a Java Enterprise Edition application server with the cloud through OpenShift. It continued this trend with all of its Java portfolio of projects and products. However, when the world moved to Linux Containers and then to Kubernetes, the entire application development paradigm for all programming languages moved toward immutability. Java has always been about enabling dynamic applications, and the frameworks built over many years take advantage of that aspect. Addressing this problem space and ensuring that Java remains a viable option for cloud developers requires a change of mindset, new tools, and new frameworks. When we started to work on Quarkus in early 2018, the team had carte blanche to go back to the drawing board and consider all
options. Working with various groups, including the Red Hat OpenJDK team, and seeing the opportunity that GraalVM represented for creating immutable native images from Java, resulted in Quarkus. In the intervening years, Quarkus has taken the Java community by storm and has grown faster and become more popular than we could have imagined back then. We firmly believe that it represents a view of the future for Enterprise Java in the Kubernetes clouds. We’ve re-architected many of the most popular Java frameworks and have written others from scratch, all with an eye toward ensuring that Quarkus is the preeminent Kubernetes- native approach for Java developers, including for new areas such as Serverless and Knative. The authors of this book have been actively involved with Quarkus and have been part of the team for quite a while. They have insights into developing applications with Quarkus that few others have today. They’ve written this book to take advantage of their experiences and to help the reader build Quarkus applications and get Developer Joy! Whether you’re new to Quarkus or already familiar with it, you will find this book to be exactly what you need. It covers a lot of ground, including taking you through the following: basic Quarkus applications; testing those applications; adding advanced concepts from Eclipse MicroProfile; reactive programming, which is a core part of Quarkus and not just an afterthought; how to build with Spring; and more. This is a great book to add to your developer library and use to increase your knowledge and skills. You’ll enjoy working your way through it and
will hopefully find yourself wanting to learn more and join the growing Quarkus community. Enjoy! Onward! Dr. Mark Little VP, Engineering, JBoss CTO
Preface We’re excited to have you with us on this journey of learning and using Quarkus! Unlike traditional Java frameworks, which can be big, cumbersome, heavy-weight, and take months to learn, Quarkus builds on knowledge you already have! It uses JPA, JAX-RS, Eclipse Vert.x, Eclipse MicroProfile, and CDI, just to name a few technologies you’re already familiar with. Quarkus then takes your knowledge and combines it into a compact, easy-to-deploy, fully Kubernetes-optimized container targeting either OpenJDK Hotspot or GraalVM. This allows you to pack your Kubernetes cluster as tightly as possible, making use of every resource on the machine as your application scales to meet demand. Regardless of where you are on your migration to Kubernetes, you’ll find something useful with Quarkus, and this book will give you the tools and resources you need to succeed. Who Should Read This Book Obviously, we want everyone to read this book! However, we have made some assumptions about the reader: You are already familiar with Java and application development within that space. You understand traditional software development practices.
You regularly deploy services into a cluster of machines or into the cloud. Why We Wrote This Book Quarkus is a relatively new framework in a new space (native Java and GraalVM). We wanted to dive into some more examples and how-to sections than what you will find on the internet. Also, we wanted to give you as much substance with this book as possible. There’s no large application to understand or remember. All the examples in this book are self-contained and ready to be used. We hope you keep this as a reference for all your Quarkus development! Navigating This Book The organization of the chapters is fairly loose, but it basically flows as follows: Chapters 1 and 2 introduce you to Quarkus and get your basic project set up. Chapters 3 through 6 introduce the bread-and-butter portion of Quarkus: RESTful applications built using concepts from CDI and Eclipse MicroProfile. These chapters also show you how to package your application. Chapters 7 through 14 relate to harder, though just as important, concepts such as fault tolerance, persistence, security, and interaction with other services. You will also learn about additional integrations Quarkus has with Kubernetes.
Chapters 15 and 16 talk about reactive programming using Quarkus and some additional functionality of the framework such as templating, scheduling, and OpenAPI. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by values determined by context. TIP This element signifies a tip or suggestion.
The above is a preview of the first 20 pages. Register to read the complete e-book.