09.1 配置是地基
wrangler.jsonc 一个文件管全部。我把它拆成必填和可选。
{
"name": "my-agent-app",
"main": "src/server.ts",
"compatibility_date": "2025-01-01",
"compatibility_flags": ["nodejs_compat"], // 必需!
"durable_objects": {
"bindings": [
{ "name": "Counter", "class_name": "Counter" },
{ "name": "ChatAgent", "class_name": "ChatAgent" }
]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["Counter", "ChatAgent"] }],
"ai": { "binding": "AI" },
"vars": { "API_BASE_URL": "https://api.example.com" }
}
09.2 多 Agent 怎么配
入口文件 export 多个类,每个类一条 binding + 一条 new_sqlite_classes。
"durable_objects": {
"bindings": [
{ "name": "Counter", "class_name": "Counter" },
{ "name": "ChatRoom", "class_name": "ChatRoom" }
]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["Counter", "ChatRoom"] }]
09.3 迁移三件套
类改名、删类,靠 migrations 版本号管。
"migrations": [
{ "tag": "v1", "new_sqlite_classes": ["OldName"] },
{ "tag": "v2", "renamed_classes": [{ "from": "OldName", "to": "NewName" }] },
{ "tag": "v3", "deleted_classes": ["AgentToDelete"] }
]
| 字段 | 用途 | 标叔的提醒 |
|---|---|---|
| new_sqlite_classes | 新建带存储的类 | 漏了不落盘 |
| renamed_classes | 改名 | 代码/binding/导出三处同步改 |
| deleted_classes | 删除 | 数据永久没,谨慎 |
09.4 部署与区域
npm run deploy
一条命令,Agent 上线到 Cloudflare 全球网络。你可以给实例指定运行区域,降延迟:
const agent = await getAgentByName(env.MyAgent, "instance", {
locationHint: "enam", // wnam|enam|sam|apac ...
});
要数据驻留欧盟?加 jurisdiction: "eu"。GDPR 合规直接满足。
核心建议
默认让它全球跑。只有延迟敏感或合规要求,才显式指定区域和辖区。别过度优化。
部署讲完。下一章聊一个心态:你只写业务逻辑。