文档索引
在以下地址获取完整文档索引:/d1/llms.txt
在进一步探索之前,使用此文件来发现所有可用的页面。
为你的电商网站使用 D1 读取复制
最后更新:2026年8月25日|复制为 Markdown|查看 Markdown 原文|Agent setup
D1 读取复制是一项允许你将 D1 数据库复制到多个区域的功能。这对于你的电商网站非常有用,因为它可以降低读取延迟并提升读取吞吐量。在本教程中,你将学习如何为你的电商网站使用 D1 读取复制。
虽然本教程使用的是一个虚构的电商网站,但这些原则可以应用于任何需要低读取延迟和读取扩展的场景,例如新闻网站、社交媒体平台或营销网站。
快速上手
如果你想跳过这些步骤并快速开始,请点击下方按钮:
这将在你的 GitHub 账户中创建一个仓库,并将应用部署到 Cloudflare Workers。它还会创建并绑定一个 D1 数据库、创建所需的表,并添加一些示例数据。在部署期间,勾选 Enable read replication 复选框以激活读取复制。
之后你就可以访问已部署的应用了。
先决条件
- 注册一个 Cloudflare 账户 ↗︎。
- 安装
Node.js↗︎。
步骤 1:创建一个 Workers 项目
通过运行以下命令创建一个新的 Workers 项目:
npmyarnpnpm
npm create cloudflare@latest -- fast-commerce
yarn create cloudflare fast-commerce
pnpm create cloudflare@latest fast-commerce
在设置时,选择以下选项:
- 对于 What would you like to start with?,选择
Hello World example。 - 对于 Which template would you like to use?,选择
SSR / full-stack app。 - 对于 Which language do you want to use?,选择
TypeScript。 - 对于 Do you want to use git for version control?,选择
Yes。 - 对于 Do you want to add an AGENTS.md file to help AI coding tools understand Cloudflare APIs?,选择
Yes。 - 对于 Do you want to deploy your application?,选择
No(我们将在部署前做一些更改)。
为了创建 API 路由,你将使用 Hono ↗︎。你需要通过运行以下命令来安装 Hono:
npmyarnpnpmbun
npm i hono
yarn add hono
pnpm add hono
bun add hono
步骤 2:更新前端
上述步骤创建了一个带有默认前端的全新 Workers 项目,并安装了 Hono。你将更新前端以列出产品。你还将向前端添加一个新页面来显示单个产品。
导航到新创建的 Worker 项目文件夹。
cd fast-commerce
更新 public/index.html 文件以列出产品。使用以下代码作为参考。
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>E-commerce Store</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
background-color: #f9fafb;
min-height: 100vh;
display: flex;
flex-direction: column;
}
header {
background-color: white;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #e5e7eb;
}
.store-title {
font-weight: bold;
font-size: 1.25rem;
}
.cart-button {
padding: 0.5rem 1rem;
cursor: pointer;
background: none;
border: none;
}
.products-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1.5rem;
padding: 2rem;
}
.product-card {
background-color: white;
border-radius: 0.5rem;
overflow: hidden;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.product-info {
padding: 1rem;
}
.product-title {
font-size: 1.125rem;
font-weight: 600;
margin-bottom: 0.5rem;
}
.product-description {
color: #4b5563;
font-size: 0.875rem;
margin-bottom: 1rem;
}
.product-price {
font-size: 1.25rem;
font-weight: bold;
margin-bottom: 0.5rem;
}
.product-stock {
color: #4b5563;
font-size: 0.875rem;
margin-bottom: 1rem;
}
.view-details-btn {
display: block;
width: 100%;
padding: 0.5rem 0;
background-color: #2563eb;
color: white;
border: none;
border-radius: 0.375rem;
cursor: pointer;
text-align: center;
text-decoration: none;
font-size: 0.875rem;
}
.view-details-btn:hover {
background-color: #1d4ed8;
}
footer {
background-color: white;
padding: 1rem 2rem;
text-align: center;
border-top: 1px solid #e5e7eb;
color: #4b5563;
font-size: 0.875rem;
}
/* Basic Responsiveness */
@media (max-width: 768px) {
.products-grid {
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
}
@media (max-width: 480px) {
.products-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<header>
<h1 class="store-title">E-commerce Store</h1>
<button class="cart-button">Cart</button>
</header>
<main class="products-grid" id="products-container">
<!-- Products will be loaded here by JavaScript -->
</main>
<footer>
<p>© 2025 E-commerce Store. All rights reserved.</p>
</footer>
<script>
document.addEventListener('DOMContentLoaded', () => {
let products = [];
let d1Duration,
queryDuration = 0;
let dbLocation;
let isPrimary = true;
// Function to create product HTML
function createProductCard(product) {
return `
<div class="product-card" data-category="${product.category}">
<div class="product-info">
<h3 class="product-title">${product.name}</h3>
<p class="product-description">${product.description}</p>
<p class="product-price">$${product.price.toFixed(2)}</p>
<p class="product-stock">${product.inventory} in stock</p>
<a href="product-details.html?id=${product.id}" class="view-details-btn">View Details</a>
</div>
</div>
`;
}
// Function to render content
function renderContent() {
try {
const productsContainer = document.getElementById('products-container');
if (!productsContainer) return;
productsContainer.innerHTML = '';
products.forEach((product) => {
productsContainer.innerHTML += createProductCard(product);
});
} catch (error) {
console.error('Error rendering content:', error);
}
}
// Fetch products
fetch('/api/products')
.then((response) => response.json())
.then((data) => {
products = data;
renderContent();
})
.catch((error) => console.error('Error fetching products:', error));
});
</script>
</body>
</html>
创建一个新的 public/product-details.html 文件来显示单个产品。
public/product-details.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Product Details - E-commerce Store</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
background-color: #f9fafb;
min-height: 100vh;
display: flex;
flex-direction: column;
}
header {
background-color: white;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #e5e7eb;
}
.store-title {
font-weight: bold;
font-size: 1.25rem;
text-decoration: none;
color: black;
}
.cart-button {
padding: 0.5rem 1rem;
cursor: pointer;
background: none;
border: none;
}
.product-container {
max-width: 800px;
margin: 2rem auto;
background-color: white;
border-radius: 0.5rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
padding: 2rem;
}
.product-title {
font-size: 1.875rem;
font-weight: bold;
margin-bottom: 0.5rem;
}
.product-description {
color: #4b5563;
margin-bottom: 1.5rem;
}
.product-price {
font-size: 1.875rem;
font-weight: bold;
margin-bottom: 0.5rem;
}
.product-stock {
font-size: 0.875rem;
color: #4b5563;
text-align: right;
}
.add-to-cart-btn {
display: block;
width: 100%;
padding: 0.75rem;
background-color: #2563eb;
color: white;
border: none;
border-radius: 0.375rem;
cursor: pointer;
text-align: center;
font-size: 1rem;
margin-top: 1.5rem;
}
.add-to-cart-btn:hover {
background-color: #1d4ed8;
}
.price-stock-container {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
}
footer {
background-color: white;
padding: 1rem 2rem;
text-align: center;
border-top: 1px solid #e5e7eb;
color: #4b5563;
font-size: 0.875rem;
margin-top: auto;
}
/* Back button */
.back-button {
display: inline-block;
margin-bottom: 1.5rem;
color: #2563eb;
text-decoration: none;
font-size: 0.875rem;
}
.back-button:hover {
text-decoration: underline;
}
/* Notification */
.notification {
position: fixed;
top: 1rem;
right: 1rem;
background-color: #10b981;
color: white;
padding: 0.75rem 1rem;
border-radius: 0.375rem;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
transform: translateX(150%);
transition: transform 0.3s ease;
}
.notification.show {
transform: translateX(0);
}
</style>
</head>
<body>
<header>
<a href="index.html" class="store-title">E-commerce Store</a>
<button class="cart-button">Cart</button>
</header>
<main class="product-container">
<a href="index.html" class="back-button">← Back to products</a>
<h1 class="product-title" id="product-title">Product Name</h1>
<p class="product-description" id="product-description">
Product description goes here.
</p>
<div class="price-stock-container">
<p class="product-price" id="product-price">$0.00</p>
<p class="product-stock" id="product-stock">0 in stock</p>
</div>
<button class="add-to-cart-btn" id="add-to-cart">Add to Cart</button>
</main>
<div class="notification" id="notification">Added to cart!</div>
<footer>
<p>© 2025 E-commerce Store. All rights reserved.</p>
</footer>
<script>
// Get query parameter from URL
const url = new URL(window.location.href);
const searchParams = new URLSearchParams(url.search);
const productId = searchParams.get("id");
// Fetch product details
fetch(`/api/products/${productId}`)
.then((response) => response.json())
.then((product) => displayContent(product))
.catch((error) =>
console.error("Error fetching product details:", error),
);
// Function to display product details
function displayContent(product) {
document.title = `${product[0].name} - E-commerce Store`;
document.getElementById("product-title").textContent = product[0].name;
document.getElementById("product-description").textContent =
product[0].description;
document.getElementById("product-price").textContent =
`$${product[0].price.toFixed(2)}`;
document.getElementById("product-stock").textContent =
`${product[0].inventory} in stock`;
}
</script>
</body>
</html>
你现在有了一个可以列出产品并显示单个产品的前端。然而,前端尚未连接到 D1 数据库。如果你现在启动开发服务器,将看不到任何产品。在接下来的步骤中,你将创建一个 D1 数据库并创建 API 来获取产品并在前端显示它们。
步骤 3:创建 D1 数据库并启用读取复制
通过运行以下命令创建一个新的 D1 数据库:
npx wrangler d1 create fast-commerce
将终端中返回的 D1 绑定添加到 wrangler 文件中:
{
"d1_databases": [
{
"binding": "DB",
"database_name": "fast-commerce",
"database_id": "YOUR_DATABASE_ID"
}
]
}
[[d1_databases]]
binding = "DB"
database_name = "fast-commerce"
database_id = "YOUR_DATABASE_ID"
运行以下命令以更新 worker-configuration.d.ts 文件中的 Env 接口。
npm run cf-typegen
接下来,为 D1 数据库启用读取复制。导航到 Workers & Pages > D1 ↗︎,然后选择一个现有数据库 > Settings > Enable Read Replication。
步骤 4:创建 API 路由
更新 src/index.ts 文件以导入 Hono 库并创建 API 路由。
import { Hono } from "hono";
// Set db session bookmark in the cookie
import { getCookie, setCookie } from "hono/cookie";
const app = new Hono<{ Bindings: Env }>();
// Get all products
app.get("/api/products", async (c) => {
return c.json({ message: "get list of products" });
});
// Get a single product
app.get("/api/products/:id", async (c) => {
return c.json({ message: "get a single product" });
});
// Upsert a product
app.post("/api/product", async (c) => {
return c.json({ message: "create or update a product" });
});
export default app;
上述代码创建了三个 API 路由:
GET /api/products:返回一个产品列表。GET /api/products/:id:返回单个产品。POST /api/product:创建或更新一个产品。
然而,这些 API 路由尚未连接到 D1 数据库。在接下来的步骤中,你将在 D1 数据库中创建 products 表,并更新 API 路由以连接到 D1 数据库。
步骤 5:创建本地 D1 数据库模式
通过运行以下命令在 D1 数据库中创建一个 products 表:
npx wrangler d1 execute fast-commerce --command "CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, description TEXT, price DECIMAL(10, 2) NOT NULL, inventory INTEGER NOT NULL DEFAULT 0, category TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"
接下来,通过运行以下命令在 products 表上创建一个索引:
npx wrangler d1 execute fast-commerce --command "CREATE INDEX IF NOT EXISTS idx_products_id ON products (id)"
出于开发目的,你还可以通过运行以下命令在本地 D1 数据库上执行插入语句:
npx wrangler d1 execute fast-commerce --command "INSERT INTO products (id, name, description, price, inventory, category) VALUES (1, 'Fast Ergonomic Chair', 'A comfortable chair for your home or office', 100.00, 10, 'Furniture'), (2, 'Fast Organic Cotton T-shirt', 'A comfortable t-shirt for your home or office', 20.00, 100, 'Clothing'), (3, 'Fast Wooden Desk', 'A wooden desk for your home or office', 150.00, 5, 'Furniture'), (4, 'Fast Leather Sofa', 'A leather sofa for your home or office', 300.00, 3, 'Furniture'), (5, 'Fast Organic Cotton T-shirt', 'A comfortable t-shirt for your home or office', 20.00, 100, 'Clothing')"
步骤 6:添加重试逻辑
为了使应用更具弹性,你可以为 API 路由添加重试逻辑。在 src 目录中创建一个名为 retry.ts 的新文件。
export interface RetryConfig {
maxRetries: number;
initialDelay: number;
maxDelay: number;
backoffFactor: number;
}
const shouldRetry = (error: unknown): boolean => {
const errMsg = error instanceof Error ? error.message : String(error);
return (
errMsg.includes("Network connection lost") ||
errMsg.includes("storage caused object to be reset") ||
errMsg.includes("reset because its code was updated")
);
};
// Helper function for sleeping
const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const defaultRetryConfig: RetryConfig = {
maxRetries: 3,
initialDelay: 100,
maxDelay: 1000,
backoffFactor: 2,
};
export async function withRetry<T>(
operation: () => Promise<T>,
config: Partial<RetryConfig> = defaultRetryConfig,
): Promise<T> {
const maxRetries = config.maxRetries ?? defaultRetryConfig.maxRetries;
const initialDelay = config.initialDelay ?? defaultRetryConfig.initialDelay;
const maxDelay = config.maxDelay ?? defaultRetryConfig.maxDelay;
const backoffFactor =
config.backoffFactor ?? defaultRetryConfig.backoffFactor;
let lastError: Error | unknown;
let delay = initialDelay;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
const result = await operation();
return result;
} catch (error) {
lastError = error;
if (!shouldRetry(error) || attempt === maxRetries) {
throw error;
}
// Add randomness to avoid synchronizing retries
// Wait for a random delay between delay and delay*2
await sleep(delay * (1 + Math.random()));
// Calculate next delay with exponential backoff
delay = Math.min(delay * backoffFactor, maxDelay);
}
}
throw lastError;
}
withRetry 函数是一个工具函数,它以指数退避的方式重试给定的操作。它接受一个配置对象作为参数,允许你自定义重试次数、初始延迟、最大延迟和退避因子。只有在错误是由于网络连接丢失、存储重置或代码更新导致时,它才会重试该操作。
注意
在分布式系统中,重试机制可能存在某些风险。请阅读文章 Retry Strategies in Distributed Systems: Identifying and Addressing Key Pitfalls ↗︎ 以了解更多关于重试机制的风险以及如何避免它们。
重试有时可能导致数据不一致。请确保谨慎处理重试逻辑。
接下来,更新 src/index.ts 文件以导入 withRetry 函数并在 API 路由中使用它。
import { withRetry } from "./retry";
步骤 7:更新 API 路由
更新 API 路由以连接到 D1 数据库。
1. POST /api/product
app.post("/api/product", async (c) => {
const product = await c.req.json();
if (!product) {
return c.json({ message: "No data passed" }, 400);
}
const db = c.env.DB;
const session = db.withSession("first-primary");
const { id } = product;
try {
return await withRetry(async () => {
// Check if the product exists
const { results } = await session
.prepare("SELECT * FROM products where id = ?")
.bind(id)
.run();
if (results.length === 0) {
const fields = [...Object.keys(product)];
const values = [...Object.values(product)];
// Insert the product
await session
.prepare(
`INSERT INTO products (${fields.join(", ")}) VALUES (${fields.map(() => "?").join(", ")})`,
)
.bind(...values)
.run();
const latestBookmark = session.getBookmark();
latestBookmark &&
setCookie(c, "product_bookmark", latestBookmark, {
maxAge: 60 * 60, // 1 hour
});
return c.json({ message: "Product inserted" });
}
// Update the product
const updates = Object.entries(product)
.filter(([_, value]) => value !== undefined)
.map(([key, _]) => `${key} = ?`)
.join(", ");
if (!updates) {
throw new Error("No valid fields to update");
}
const values = Object.entries(product)
.filter(([_, value]) => value !== undefined)
.map(([_, value]) => value);
await session
.prepare(`UPDATE products SET ${updates} WHERE id = ?`)
.bind(...[...values, id])
.run();
const latestBookmark = session.getBookmark();
latestBookmark &&
setCookie(c, "product_bookmark", latestBookmark, {
maxAge: 60 * 60, // 1 hour
});
return c.json({ message: "Product updated" });
});
} catch (e) {
console.error(e);
return c.json({ message: "Error upserting product" }, 500);
}
});
在上述代码中:
- 你从请求体中获取产品数据。
- 然后你检查数据库中是否存在该产品。
- 如果存在,你更新该产品。
- 如果不存在,你插入该产品。
- 然后你在 cookie 中设置书签。
- 最后,你返回响应。
由于你希望以最新数据开始会话,你使用了 first-primary 约束。即使你使用 first-unconstrained 约束或传入书签,写请求也始终会被路由到主库。
在 cookie 中设置的书签可用于保证新会话读取的数据库版本至少与所提供的书签一样新。
如果你使用外部平台来管理你的产品,你可以将此 API 连接到该外部平台,这样当在外部平台中创建或更新产品时,D1 数据库会自动更新产品详情。
2. GET /api/products
app.get("/api/products", async (c) => {
const db = c.env.DB;
// Get bookmark from the cookie
const bookmark = getCookie(c, "product_bookmark") || "first-unconstrained";
const session = db.withSession(bookmark);
try {
return await withRetry(async () => {
const { results } = await session.prepare("SELECT * FROM products").run();
const latestBookmark = session.getBookmark();
// Set the bookmark in the cookie
latestBookmark &&
setCookie(c, "product_bookmark", latestBookmark, {
maxAge: 60 * 60, // 1 hour
});
return c.json(results);
});
} catch (e) {
console.error(e);
return c.json([]);
}
});
在上述代码中:
- 你从 cookie 中获取数据库会话书签。
- 如果未设置书签,你使用
first-unconstrained约束。
- 如果未设置书签,你使用
- 然后你使用该书签创建一个数据库会话。
- 你从数据库中获取所有产品并获取最新的书签。
- 然后你在 cookie 中设置此书签。
- 最后,你返回结果。
3. GET /api/products/:id
app.get("/api/products/:id", async (c) => {
const id = c.req.param("id");
if (!id) {
return c.json({ message: "Invalid id" }, 400);
}
const db = c.env.DB;
// Get bookmark from the cookie
const bookmark = getCookie(c, "product_bookmark") || "first-unconstrained";
const session = db.withSession(bookmark);
try {
return await withRetry(async () => {
const { results } = await session
.prepare("SELECT * FROM products where id = ?")
.bind(id)
.run();
const latestBookmark = session.getBookmark();
// Set the bookmark in the cookie
latestBookmark &&
setCookie(c, "product_bookmark", latestBookmark, {
maxAge: 60 * 60, // 1 hour
});
console.log(results);
return c.json(results);
});
} catch (e) {
console.error(e);
return c.json([]);
}
});
在上述代码中:
- 你从请求参数中获取产品 ID。
- 然后你使用该书签创建一个数据库会话。
- 你从数据库中获取该产品并获取最新的书签。
- 然后你在 cookie 中设置此书签。
- 最后,你返回结果。
步骤 8:测试应用
你现在已经更新了 API 路由以连接到 D1 数据库。你现在可以通过启动开发服务器并导航到前端来测试该应用。
npm run dev
导航到 `http://localhost:8787 ↗︎。你应该会看到列出的产品。点击某个产品以查看产品详情。
要插入一个新产品,请使用以下命令(在开发服务器运行时):
curl -X POST http://localhost:8787/api/product \
-H "Content-Type: application/json" \
-d '{"id": 6, "name": "Fast Computer", "description": "A computer for your home or office", "price": 1000.00, "inventory": 10, "category": "Electronics"}'
导航到 http://localhost:8787/product-details?id=6。你应该会看到这个新产品。
使用以下命令更新该产品,然后再次导航到 http://localhost:8787/product-details?id=6。你将看到更新后的产品。
curl -X POST http://localhost:8787/api/product \
-H "Content-Type: application/json" \
-d '{"id": 6, "name": "Fast Computer", "description": "A computer for your home or office", "price": 1050.00, "inventory": 10, "category": "Electronics"}'
注意
读取复制仅在应用已部署后才会使用。D1 在你本地开发时不会创建只读副本。
要在本地测试它,你可以在 D1 绑定配置中设置 "remote" : true。有关更多信息,请参阅远程绑定文档。
步骤 9:部署应用
由于你在之前的步骤中使用的是本地数据库,你需要在远程数据库中创建 products 表。执行以下 D1 命令以在远程数据库中创建 products 表。
npx wrangler d1 execute fast-commerce --remote --command "CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, description TEXT, price DECIMAL(10, 2) NOT NULL, inventory INTEGER NOT NULL DEFAULT 0, category TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"
接下来,通过运行以下命令在 products 表上创建一个索引:
npx wrangler d1 execute fast-commerce --remote --command "CREATE INDEX IF NOT EXISTS idx_products_id ON products (id)"
可选地,你可以通过运行以下命令将产品插入到远程数据库中:
npx wrangler d1 execute fast-commerce --remote --command "INSERT INTO products (id, name, description, price, inventory, category) VALUES (1, 'Fast Ergonomic Chair', 'A comfortable chair for your home or office', 100.00, 10, 'Furniture'), (2, 'Fast Organic Cotton T-shirt', 'A comfortable t-shirt for your home or office', 20.00, 100, 'Clothing'), (3, 'Fast Wooden Desk', 'A wooden desk for your home or office', 150.00, 5, 'Furniture'), (4, 'Fast Leather Sofa', 'A leather sofa for your home or office', 300.00, 3, 'Furniture'), (5, 'Fast Organic Cotton T-shirt', 'A comfortable t-shirt for your home or office', 20.00, 100, 'Clothing')"
现在,你可以使用以下命令部署应用:
npm run deploy
这会将应用部署到 Workers,并且 D1 数据库将被复制到远程区域。如果用户从任何区域请求该应用,请求将被重定向到数据库被复制的最近区域。
结论
在本教程中,你学习了如何为你的电商网站使用 D1 读取复制。你创建了一个 D1 数据库并为其启用了读取复制。然后你创建了一个 API 来在数据库中创建和更新产品。你还学习了如何使用书签从数据库中获取最新数据。
随后你在远程数据库中创建了 products 表并部署了应用。
你可以对现有的读取密集型应用使用相同的方法,以降低读取延迟并提升读取吞吐量。如果你使用外部平台来管理内容,你可以将外部平台连接到 D1 数据库,这样内容就会自动更新到数据库中。
你可以在 GitHub 仓库 ↗︎ 中找到本教程的完整代码。