Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ history, monolith dropped). The full agreed plan is in [`docs/PLAN.md`](docs/PLA
- Name prefix: `ms12p` (parent uses `flaskms`). Teardown deletes the whole resource group, so a
shared name would delete the other project.

## Current state (M3a: every pattern except 3 is live)
## Current state (M3b: all 12 patterns live locally; Azure run is #11)
- Folders: `services/{user,catalog,order,payment,inventory}/` (Flask, one DB each), `gateway/`
(FastAPI), `registry/` (FastAPI), `shared/` (`shop_common`), `contracts/`, `frontend/`, `infra/`,
`scripts/`, `tests/`.
Expand Down Expand Up @@ -56,7 +56,9 @@ history, monolith dropped). The full agreed plan is in [`docs/PLAN.md`](docs/PLA
GitHub secrets so they stay masked in public logs); `scripts/deploy_plan.py` picks services whose
code changed since their running commit (CI-only files rebuild nothing) and waits until each service
answers `/debug/runtime` with the new commit. All workflow actions are pinned to commit SHAs. "CI ok"
is the one job to require in branch protection.
is the one job to require in branch protection. The CI-CD page reads `/api/patterns/delivery`
(gateway `routers/delivery.py`): running commits vs main and the latest runs, from GitHub's public
API, reused 5 min then revalidated by ETag (304s are free), last good answer kept on errors; "behind" uses the Deploy rule (`GITHUB_REPO` overrides the repo).
- Azure (M3b): provision keeps `image_tag`, `image_sizes` and `jwt_key_b64` in infra/.state.json so
`--skip-build` and re-runs neither break image pulls nor log users out. It tags images with the 12-char commit, imports `redis:7.4-alpine` into ACR,
generates the JWT key (`openssl`, passed base64 as a secure param, user reads `JWT_PRIVATE_KEY_B64`),
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ A small Contoso e-commerce system that shows **12 microservices patterns working
Each pattern gets its own page with an explanation, when *not* to use it, and a live demo you
can click.

> **Status: M3a, async and packaging.** 11 of 12 patterns are live. M3a adds the Saga,
> Event-Driven Architecture and Containerization to M1 (patterns 1, 2, 5, 6) and M2 (4, 10, 11, 12).
> The foundation is copied from
> **Status: M3b, delivery.** All 12 patterns are live locally, each with a working demo. The
> first real Azure run is the last step (issue #11). The foundation is copied from
> [Azure-Ecommerce-Monolith-Microservices-Migration](https://github.com/MAOFILHO/Azure-Ecommerce-Monolith-Microservices-Migration).
> CI-CD and the Azure run arrive in M3b. See [docs/PLAN.md](docs/PLAN.md).
> See [docs/PLAN.md](docs/PLAN.md) and the [M3b issues](https://github.com/MAOFILHO/Microservices-12-Patterns/issues).

## The 12 patterns

| # | Pattern | What it does | Status |
|---|---|---|---|
| 1 | Database per Service | Gives each service its own database | **Live (M1)** |
| 2 | Bounded Context | Splits one large domain into clear business domains | **Live (M1)** |
| 3 | CI-CD Pipeline | Builds, tests and deploys every service automatically | M3b |
| 3 | CI-CD Pipeline | Builds, tests and deploys every service automatically | **Live (M3b)** |
| 4 | Circuit Breaker | Stops calls to a failing service to prevent more failures | **Live (M2)** |
| 5 | API Gateway | Routes client requests to the right service | **Live (M1)** |
| 6 | Observability | Collects logs, metrics and traces from every service | **Live (M1)** |
Expand Down Expand Up @@ -82,6 +81,13 @@ database queries, drawn as a waterfall. Metrics per service come from each `/met

<img width="100%" alt="Observability demo: a trace waterfall across gateway, user, order, catalog and inventory" src="docs/screenshots/p06-observability.jpg" />

### 3. CI-CD Pipeline
Which commit each running service was built from, compared with the latest commit on main, and
the latest CI and Deploy runs, read from GitHub. Locally every process is a "local build"; in Azure
each image carries its commit, so a service that is behind main shows up at once.

<img width="100%" alt="CI-CD demo: each service's build commit compared with main, and the latest CI and Deploy runs" src="docs/screenshots/p03-ci-cd.jpg" />

### 4. Circuit Breaker
Switch payment to slow and send 10 calls. Each order replica's breaker opens after 3 failures.
A real failure took 2,215 ms; a call the open breaker stopped took 0 ms. After 15 s with payment
Expand Down
Binary file added docs/screenshots/p03-ci-cd.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions frontend/src/api/gatewayClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,28 @@ export const asyncApi = {
}),
runtime: () => request<{ processes: RuntimeInfo[] }>("/api/patterns/runtime"),
};

// --- M3b demo endpoint (pattern 3) ---------------------------------------------------------------

export interface WorkflowRun {
workflow: string;
status: string;
conclusion: string | null;
commit: string;
url: string;
created_at: string;
}

/** current: built from main. behind: main changed its code. unchanged: older build, nothing of it changed. */
export type DeliveryStatus = "current" | "behind" | "unchanged" | "local" | "unknown";

export interface DeliveryView {
repo: string;
main: { sha: string; short: string; url: string } | null;
services: { service: string; instance: string; git_sha: string | null; status: DeliveryStatus }[];
runs: { ci: WorkflowRun | null; deploy: WorkflowRun | null };
}

export const deliveryApi = {
view: () => request<DeliveryView>("/api/patterns/delivery"),
};
2 changes: 1 addition & 1 deletion frontend/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Home() {
</Link>
<Link to="/patterns" className="card" style={{ textDecoration: "none", color: "inherit" }}>
<h3>Patterns</h3>
<p className="muted">The 12 patterns. 11 are live now; CI-CD arrives in M3b.</p>
<p className="muted">The 12 patterns. All 12 are live, each with a working demo.</p>
</Link>
</div>

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/PatternPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReactNode } from "react";
import { Link, useParams } from "react-router-dom";
import ApiGatewayDemo from "../patterns/demos/ApiGatewayDemo";
import BoundedContextDemo from "../patterns/demos/BoundedContextDemo";
import CiCdDemo from "../patterns/demos/CiCdDemo";
import CircuitBreakerDemo from "../patterns/demos/CircuitBreakerDemo";
import ContainerizationDemo from "../patterns/demos/ContainerizationDemo";
import ContractTestingDemo from "../patterns/demos/ContractTestingDemo";
Expand All @@ -25,6 +26,7 @@ const DEMOS: Record<string, () => ReactNode> = {
"event-driven-architecture": () => <EventDrivenDemo />,
saga: () => <SagaDemo />,
containerization: () => <ContainerizationDemo />,
"ci-cd-pipeline": () => <CiCdDemo />,
};

export default function PatternPage() {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Patterns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function Patterns() {
<h1>The 12 patterns</h1>
<p className="muted">
Each pattern has a page that explains it, says when not to use it, and runs a live demo against the real
services. {liveCount} of 12 are live; the last one, CI-CD, arrives in milestone M3b.
services. {liveCount} of 12 are live; every one has a working demo.
</p>
</section>
<div className="grid cols-3">
Expand Down
24 changes: 23 additions & 1 deletion frontend/src/patterns/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,29 @@ export const PATTERNS: PatternInfo[] = [
],
source: { label: "Martin Fowler — BoundedContext", url: "https://martinfowler.com/bliki/BoundedContext.html" },
},
{ n: 3, slug: "ci-cd-pipeline", name: "CI-CD Pipeline", what: "Builds, tests and deploys every service automatically.", milestone: "M3", live: false },
{
n: 3,
slug: "ci-cd-pipeline",
name: "CI-CD Pipeline",
what: "Builds, tests and deploys every service automatically.",
milestone: "M3",
live: true,
problem:
"With 5 services and a gateway, building and testing everything on every change is slow, and deploying by hand from a laptop is error-prone: nobody knows which version runs where, and one tired evening ships the wrong one.",
inThisProject: [
"Every pull request first plans its jobs from the changed files and the service table: a change to the payment service runs payment's tests and image only.",
"Shared code, CI itself or the service table run everything. Changing the contract consumer also runs every provider's contract check.",
"Every image is built for linux/amd64 in CI, started, health-checked, and rejected if it runs as root.",
"Deploy is a manual workflow on main. It signs in to Azure with OIDC (no stored password), rebuilds only the services that changed since the commit they run, and waits until each answers with the new commit.",
"Every running service reports the commit it was built from. This page marks a service behind main only when main changed its code since that build, the same rule Deploy uses.",
],
whenNotToUse: [
"Path-based skipping trusts the folder layout. Code that one service imports from another folder must be declared, or a change can skip a test it should run.",
"Manual deploys suit a demo. A busy team usually deploys every merge automatically, behind tests and feature flags.",
"Rebuilding only what changed assumes services are independent. Breaking API changes still need contract tests and a deploy order.",
],
source: { label: "Martin Fowler — Continuous Delivery", url: "https://martinfowler.com/bliki/ContinuousDelivery.html" },
},
{
n: 4,
slug: "circuit-breaker",
Expand Down
134 changes: 134 additions & 0 deletions frontend/src/patterns/demos/CiCdDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { useEffect, useState } from "react";
import { DeliveryStatus, DeliveryView, WorkflowRun, deliveryApi } from "../../api/gatewayClient";

const PILL: Record<DeliveryStatus, string> = {
current: "done",
unchanged: "done",
behind: "failed",
local: "pending",
unknown: "pending",
};
const LABEL: Record<DeliveryStatus, string> = {
current: "on latest main",
unchanged: "no changes since its build",
behind: "behind main: Deploy rebuilds it",
local: "local build",
unknown: "unknown",
};

function Run({ title, run }: { title: string; run: WorkflowRun | null }) {
return (
<div className="context-box">
<h4>{title}</h4>
{run ? (
<p style={{ margin: 0 }}>
<span className={`pill ${run.conclusion === "success" ? "done" : run.conclusion ? "failed" : "running"}`}>
{run.conclusion ?? run.status}
</span>{" "}
commit <code>{run.commit}</code> · {new Date(run.created_at).toLocaleString()} ·{" "}
<a href={run.url} target="_blank" rel="noreferrer">
open the run
</a>
</p>
) : (
<p className="muted" style={{ margin: 0 }}>
No run on main yet, or GitHub is unreachable.
</p>
)}
</div>
);
}

export default function CiCdDemo() {
const [view, setView] = useState<DeliveryView | null>(null);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
let active = true;
deliveryApi
.view()
.then((v) => active && setView(v))
.catch((err) => active && setError(String(err)));
return () => {
active = false;
};
}, []);

if (!view && !error) return <p className="muted">Reading the running services and GitHub…</p>;

const behind = view?.services.filter((s) => s.status === "behind").length ?? 0;

return (
<div className="grid" style={{ gap: "1.25rem" }}>
{error && <p style={{ color: "var(--contoso-danger)" }}>{error}</p>}
<div>
<h3>What each running service was built from</h3>
<p className="muted">
Latest commit on main:{" "}
{view?.main ? (
<a href={view.main.url} target="_blank" rel="noreferrer">
<code>{view.main.short}</code>
</a>
) : (
"unknown (GitHub unreachable)"
)}
. {behind > 0 ? `${behind} service(s) are behind main: the Deploy button rebuilds exactly those.` : ""}
</p>
<div className="table-scroll">
<table className="data-table">
<thead>
<tr>
<th>Service</th>
<th>Built from</th>
<th>Compared with main</th>
</tr>
</thead>
<tbody>
{view?.services.map((s) => (
<tr key={s.instance}>
<td>
<span className={`svc-dot svc-${s.service}`} />
{s.instance}
</td>
<td>
<code>{s.git_sha ?? "–"}</code>
</td>
<td>
<span className={`pill ${PILL[s.status]}`}>{LABEL[s.status]}</span>
</td>
</tr>
))}
</tbody>
</table>
</div>
<p className="muted" style={{ fontSize: "0.85rem" }}>
Locally every process says "local build": make run starts the code from your working copy. In Azure, each
image carries the commit it was built from.
</p>
</div>

<div className="grid cols-2">
<Run title="Latest CI run on main" run={view?.runs.ci ?? null} />
<Run title="Latest Deploy run" run={view?.runs.deploy ?? null} />
</div>

<div>
<h3>The pipeline</h3>
<ol>
<li>
<strong>Pull request:</strong> a planning job reads the changed files and the service table, then runs only
the unit tests, contract checks and container builds that change touches. Shared code runs everything.
</li>
<li>
<strong>Merge to main:</strong> everything runs once more. One job, "CI ok", sums it all up.
</li>
<li>
<strong>Deploy (a person presses the button):</strong> signs in to Azure with OIDC (no stored password),
rebuilds only the services whose code changed since the commit they run, rolls them out, and waits until
each one answers with the new commit.
</li>
</ol>
</div>
</div>
);
}
3 changes: 2 additions & 1 deletion gateway/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from shop_common.discovery import DISCOVERY

from . import config, observability
from .routers import patterns, resilience, services, shop
from .routers import delivery, patterns, resilience, services, shop

app = FastAPI(
title="Microservices 12 Patterns — API Gateway",
Expand Down Expand Up @@ -32,6 +32,7 @@
app.include_router(shop.router)
app.include_router(patterns.router)
app.include_router(resilience.router)
app.include_router(delivery.router)
app.include_router(services.router)


Expand Down
Loading
Loading