Preface |
|
xiii | |
Acknowledgments |
|
xiv | |
About This Book |
|
xv | |
About The Author |
|
xviii | |
About The Cover Illustration |
|
xix | |
Part 1 Getting Started With Microservices |
|
1 | (72) |
|
1 Microservices at a glance |
|
|
3 | (25) |
|
1.1 What is a microservice? |
|
|
3 | (2) |
|
1.2 What is a microservices architecture? |
|
|
5 | (6) |
|
Microseruice characteristics |
|
|
6 | (5) |
|
|
11 | (3) |
|
Enabling continuous delivery |
|
|
11 | (2) |
|
High level of maintainability |
|
|
13 | (1) |
|
|
14 | (1) |
|
1.4 Costs and downsides of microservices |
|
|
14 | (1) |
|
1.5 Greenfield vs. brownfield |
|
|
15 | (1) |
|
|
16 | (1) |
|
1.7 Serving a user request: An example of how microservices work in concert |
|
|
17 | (4) |
|
Main handling of the user request |
|
|
17 | (2) |
|
Side effects of the user request |
|
|
19 | (1) |
|
|
20 | (1) |
|
1.8 A .NET microservices technology stack |
|
|
21 | (1) |
|
|
21 | (1) |
|
|
21 | (1) |
|
Setting up a development environment |
|
|
22 | (1) |
|
1.9 A simple microservices example |
|
|
22 | (6) |
|
Creating an empty ASP.NET application |
|
|
23 | (1) |
|
Adding ASP.NET MVC to the project |
|
|
24 | (1) |
|
Adding an MVC controller with an implementation of the endpoint |
|
|
25 | (3) |
|
2 A basic shopping cart microservice |
|
|
28 | (26) |
|
2.1 Overview of the Shopping Cart microservice |
|
|
29 | (3) |
|
Components of the Shopping Cart microservice |
|
|
31 | (1) |
|
2.2 Implementing the Shopping Cart microservice |
|
|
32 | (20) |
|
Creating an empty project |
|
|
32 | (1) |
|
The Shopping Cart microservice's API for other services |
|
|
33 | (9) |
|
Fetching product information |
|
|
42 | (2) |
|
Parsing the product response |
|
|
44 | (2) |
|
Adding a failure-handling policy |
|
|
46 | (2) |
|
Implementing a basic event feed |
|
|
48 | (4) |
|
|
52 | (2) |
|
3 Deploying a microservice to Kubernetes |
|
|
54 | (19) |
|
3.1 Choosing a production environment |
|
|
55 | (2) |
|
3.2 Putting the Shopping Cart microservice in a container |
|
|
57 | (3) |
|
Adding a Dockerfile to the Shopping Cart microservice |
|
|
57 | (1) |
|
Building and running the shopping cart container |
|
|
58 | (2) |
|
3.3 Running the shopping cart container in Kubernetes |
|
|
60 | (2) |
|
Setting up Kubernetes localhost |
|
|
60 | (2) |
|
3.4 Creating Kubernetes deployment for the shopping cart |
|
|
62 | (3) |
|
3.5 Running the shopping cart container on Azure Kubernetes Service |
|
|
65 | (10) |
|
|
66 | (3) |
|
Running the shopping cart in AKS |
|
|
69 | (4) |
Part 2 Building Microservices |
|
73 | (132) |
|
4 Identifying and scoping microservices |
|
|
75 | (25) |
|
4.1 The primary driver for scoping microservices: Business capabilities |
|
|
76 | (7) |
|
What is a business capability? |
|
|
76 | (1) |
|
Identifying business capabilities |
|
|
77 | (1) |
|
Example: Point-of-sale system |
|
|
78 | (5) |
|
4.2 The secondary driver for scoping microservices: Supporting technical capabilities |
|
|
83 | (6) |
|
What is a technical capability? |
|
|
83 | (1) |
|
Examples of supporting technical capabilities |
|
|
83 | (5) |
|
Identifying technical capabilities |
|
|
88 | (1) |
|
4.3 The tertiary driver for scoping microservices: Supporting efficiency of work |
|
|
89 | (1) |
|
4.4 What to do when the correct scope isn't clear |
|
|
90 | (6) |
|
|
91 | (3) |
|
Carving out new microservices from existing microservices |
|
|
94 | (2) |
|
Planning to carve out new microservices later |
|
|
96 | (1) |
|
4.5 Well-scoped microservices adhere to the microservice characteristics |
|
|
96 | (4) |
|
Primary scoping to business capabilities leads to good microservices |
|
|
97 | (1) |
|
Secondary scoping to support technical capabilities leads to good microservices |
|
|
97 | (1) |
|
Tertiary scoping to support efficiency of work |
|
|
98 | (2) |
|
5 Microservice collaboration |
|
|
100 | (30) |
|
5.1 Types of collaboration: Commands, queries, and events |
|
|
101 | (9) |
|
Commands and queries: Synchronous collaboration |
|
|
103 | (4) |
|
Events: Asynchronous collaboration |
|
|
107 | (2) |
|
|
109 | (1) |
|
5.2 Implementing collaboration |
|
|
110 | (20) |
|
Setting up a project for the loyalty program |
|
|
112 | (1) |
|
Implementing commands and queries |
|
|
112 | (1) |
|
Implementing commands with HTTP POST or PUT |
|
|
113 | (3) |
|
Implementing queries with HTTP GET |
|
|
116 | (1) |
|
Implementing an event-based collaboration |
|
|
117 | (5) |
|
|
122 | (1) |
|
Building a Docker container special offers microservice |
|
|
122 | (1) |
|
Building a Docker container for both parts of the loyalty program |
|
|
123 | (2) |
|
Deploying the loyalty program API and the special offers |
|
|
125 | (1) |
|
|
126 | (4) |
|
6 Data ownership and data storage |
|
|
130 | (26) |
|
6.1 Each microservice has a data store |
|
|
130 | (1) |
|
6.2 Partitioning data between microservices |
|
|
131 | (8) |
|
Rule 1: Ownership of data follows business capabilities |
|
|
132 | (1) |
|
Rule 2: Replicate for speed and robustness |
|
|
133 | (4) |
|
Where does a microservice store its data? |
|
|
137 | (2) |
|
6.3 Implementing data storage in a microservice |
|
|
139 | (17) |
|
Preparing a development setup |
|
|
140 | (1) |
|
Storing data owned by a microservice |
|
|
141 | (4) |
|
Storing events raised by a microservice |
|
|
145 | (6) |
|
Setting cache headers in HTTP responses |
|
|
151 | (1) |
|
Reading and using cache headers |
|
|
152 | (4) |
|
7 Designing for robustness |
|
|
156 | (21) |
|
|
157 | (6) |
|
|
159 | (1) |
|
|
160 | (1) |
|
Rolling forward vs. rolling backward |
|
|
161 | (1) |
|
|
161 | (2) |
|
7.2 The client side's responsibility for robustness |
|
|
163 | (5) |
|
Robustness pattern: Retry |
|
|
164 | (2) |
|
Robustness pattern: Circuit breaker |
|
|
166 | (2) |
|
7.3 Implementing robustness patterns |
|
|
168 | (9) |
|
Implementing a fast-paced retry strategy with Polly |
|
|
170 | (1) |
|
Implementing a circuit breaker with Polly |
|
|
171 | (1) |
|
Implementing a slow-paced retry strategy |
|
|
172 | (2) |
|
Logging all unhandled exceptions |
|
|
174 | (2) |
|
|
176 | (1) |
|
8 Writing tests for microservices |
|
|
177 | (28) |
|
|
178 | (6) |
|
The test pyramid: What to test in a microservices system |
|
|
178 | (1) |
|
System-level tests: Testing a complete microservice system end to end |
|
|
179 | (1) |
|
Service-level tests: Testing a microservice from outside its process |
|
|
180 | (3) |
|
Unit-level tests: Testing endpoints from within the process |
|
|
183 | (1) |
|
8.2 Testing libraries: Microsoft.AspNetCore.TestHost and xUnit |
|
|
184 | (4) |
|
Meet Microsoft.AspNetCore.TestHost |
|
|
184 | (1) |
|
|
185 | (1) |
|
xUnit and Microsoft.AspNetCore.TestHost working together |
|
|
185 | (3) |
|
8.3 Writing unit tests using Microsoft.AspNetCore.TestHost |
|
|
188 | (9) |
|
Setting up a unit-test project |
|
|
189 | (1) |
|
Using the TestServer and HttpClient to unit-test endpoints |
|
|
190 | (3) |
|
Injecting mocks into endpoints |
|
|
193 | (4) |
|
8.4 Writing service-level tests |
|
|
197 | (10) |
|
Creating a service-level test project |
|
|
198 | (1) |
|
Creating mocked endpoints |
|
|
198 | (3) |
|
Executing the test scenario against the microservice under test |
|
|
201 | (4) |
Part 3 Handling Cross-Cutting Concerns: Building A Reusable Microservice Platform |
|
205 | (58) |
|
9 Cross-cutting concerns: Monitoring and logging |
|
|
207 | (23) |
|
9.1 Monitoring needs in microservices |
|
|
208 | (3) |
|
9.2 Logging needs in microservices |
|
|
211 | (4) |
|
Tracing requests across microservices |
|
|
212 | (1) |
|
Structured logging with Serilog |
|
|
213 | (2) |
|
9.3 Implementing the monitoring endpoints |
|
|
215 | (4) |
|
Implementing the /health/live monitoring endpoint |
|
|
216 | (1) |
|
Implementing the /health/startup monitoring endpoint |
|
|
217 | (2) |
|
9.4 Implementing structured logging |
|
|
219 | (7) |
|
Adding a trace ID to all log messages |
|
|
222 | (3) |
|
Trace ID is included in outgoing HTTP requests |
|
|
225 | (1) |
|
Logging unhandled exceptions |
|
|
225 | (1) |
|
9.5 Implementing monitoring and logging in Kubernetes |
|
|
226 | (4) |
|
Configure monitoring in Kubernetes |
|
|
226 | (4) |
|
10 Securing microservice-to-microservice communication |
|
|
230 | (13) |
|
10.1 Microservice security concerns |
|
|
231 | (5) |
|
Authenticating users at the edge |
|
|
232 | (2) |
|
Authorizing users in microservices |
|
|
234 | (1) |
|
How much should microservices trust each other? |
|
|
235 | (1) |
|
10.2 Implementing secure microservice-to-microservice communication |
|
|
236 | (7) |
|
Accessing the user identity in the loyalty program |
|
|
236 | (2) |
|
Limiting which microservices can communicate |
|
|
238 | (5) |
|
11 Building a reusable microservice platform |
|
|
243 | (20) |
|
11.1 Creating a new microservice should be quick and easy |
|
|
244 | (1) |
|
11.2 Handling cross-cutting concerns |
|
|
244 | (2) |
|
11.3 Creating a reusable microservice platform |
|
|
246 | (2) |
|
11.4 Packaging and sharing cross-cutting code with NuGet |
|
|
248 | (7) |
|
Creating a logging package |
|
|
249 | (2) |
|
Creating a package with monitoring endpoints |
|
|
251 | (4) |
|
11.5 The ASP.NET pipeline |
|
|
255 | (2) |
|
What belongs in middleware? |
|
|
256 | (1) |
|
|
257 | (2) |
|
|
257 | (1) |
|
|
258 | (1) |
|
11.7 Testing middleware and pipelines |
|
|
259 | (4) |
Part 4 Building Applications |
|
263 | (30) |
|
12 Creating applications over microservices |
|
|
265 | (28) |
|
12.1 End user applications for microservice systems: One or many applications? |
|
|
266 | (2) |
|
General-purpose applications |
|
|
266 | (1) |
|
|
267 | (1) |
|
12.2 Patterns for building applications over microservices |
|
|
268 | (8) |
|
Composite applications: Integrating at the frontend |
|
|
268 | (3) |
|
|
271 | (2) |
|
Backend for frontend (BFF) pattern |
|
|
273 | (1) |
|
|
274 | (1) |
|
Client-side or server-side rendering? |
|
|
275 | (1) |
|
12.3 Example: A shopping cart and a product list |
|
|
276 | (17) |
|
|
279 | (2) |
|
Creating the product list GUI |
|
|
281 | (5) |
|
Creating the shopping cart GUI |
|
|
286 | (3) |
|
Letting users add products to the shopping cart |
|
|
289 | (1) |
|
Letting users remove products from the shopping cart |
|
|
290 | (3) |
Appendix Development environment setup |
|
293 | (6) |
Further reading |
|
299 | (2) |
Index |
|
301 | |