Skip to content

Commit fdc0959

Browse files
committed
add post uuid
1 parent 6e80102 commit fdc0959

File tree

15 files changed

+386
-39
lines changed

15 files changed

+386
-39
lines changed

content/posts/microservice-apis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def pay_order(order_id: UUID):
455455

456456

457457
### Wrapping Up
458-
上面介绍了微服务的概念, 并通过一个 CoffeeMesh 的项目解释了如何设计一个微服务, 分别通过业务分解和通过子领域分解, 以及设计微服务的3条原则:
458+
上面介绍了微服务的概念, 并通过一个 CoffeeMesh 的项目解释了如何将其分解(decompose)为微服务架构, 分别通过业务分解和通过子领域分解, 以及设计微服务的3条原则:
459459
- Database-per-service principle 数据库独享原则
460460
- Loose coupling principle 松耦合原则
461461
- Single responsibility principle 单一责任原则

content/posts/rest-apis.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
+++
2+
date = '2025-08-17T8:00:00+08:00'
3+
draft = true
4+
title = 'Designing and Building REST APIs'
5+
+++
6+
这篇文章延续之前微服务的内容, 将介绍关于 REST API 的以下几个方面:
7+
- REST API 的设计原则
8+
- Richardson maturity model (RMM) 如何帮助理解 REST 的优势和设计原则
9+
- REST API 中资源(resource)和端点(endpoints)设计的概念
10+
- 使用 HTTP 请求方法(verb)和HTTP状态码(status code)来创建高解释性的REST API
11+
- 为REST API 设计高质量的载荷(payload)和URL查询参数(query parameters)
12+
13+
表达性状态转移 Representational state transfer (REST) 描述了一种通过网络进行通信的应用程序架构风格.
14+
最初, REST 的概念包含了一组用于设计分布式、可扩展 Web 应用的约束条件.
15+
随着时间推移, 出现了更为细致的协议和规范, 为 REST API 的设计提供了明确的指导方针.
16+
如今, REST 已经成为构建 Web API 的最流行选择.
17+
18+
下面将继续在 CoffeeMesh 项目上, 设计相关订单 API.
19+
20+
## What is REST?
21+
REST 由 Roy Fielding 在他的博士论文 "Architectural Styles and the Design of Network-based Software Atchitecture" (PhD diss. University of California,Irvine,2000,p. 109) 中创造.
22+
> 定义: REST 是一个松耦合和高伸缩的 API 架构风格. REST APIs 以资源为核心来组织, 这些资源是可以通过 API 操作的实体.
23+
24+
资源 resource 是可以通过唯一的 URL 来标识的实体, 有两种类型: 集和 collections 和 单体 singletons.
25+
单体标识一个单独的实体, 而集和标识一组实体.
26+
27+
例如, 在 CoffeeMesh 的订单服务负责管理订单, 通过 `/orders/{order_id}` 访问特定订单, 是一个单体端点(singleton endpoint); 而所有订单通过 `/orders` 获取, 是一个集和端点 (collection endpoint).
28+
29+
某些资源还可以嵌套进其他资源中, 例如一个订单的 payload 中, 可能包含一个嵌套数组列出该订单的多个商品
30+
, 例如下面这样:
31+
```JSON
32+
{
33+
"id": "924721eb-a1a1-4f13-b384-37e89c0e0875",
34+
"status": "progress",
35+
"created": "2023-09-01",
36+
"order": [
37+
{
38+
"product": "cappuccino",
39+
"size": "small",
40+
"quantity": 1
41+
},
42+
{
43+
"product": "croissant",
44+
"size": "medium",
45+
"quantity": 2
46+
}
47+
]
48+
}
49+
```
50+
51+
可以创建一个嵌套端点来表示嵌套资源, 例如通过 GET `/orders/{order_id}/status` 端点查询订单的状态和细节信息.
52+
当资源对应的负载 payload 较大时, 使用嵌套端点是一种常见的优化策略, 例如只想知道状态信息, 就不需要查询大量的详细数据了, status 端点返回信息如下:
53+
```JSON
54+
{
55+
"status": "processing"
56+
}
57+
```
58+
59+
### Architectural constraints of REST applications
60+
这里解释 REST 应用的架构约束, 这些约束由 Fielding 列出, 用于规定服务器应如何处理并响应客户端请求.
61+
下面是每个约束的简单描述:
62+
- Client-server architecture 客户端-服务器架构: 用户界面必须与后端解耦 decoupled
63+
- Statelessness 无状态性: 服务器不能在请求之间维护状态
64+
- Cacheability 可缓存性: 返回相同内容的请求, 应支持缓存
65+
- Layered system 分层系统: API 按层架构, 但要向用户隐藏复杂性
66+
- Code on demand 按需代码: 服务器可以按需将代码注入到用户界面
67+
- Uniform interferace 统一接口: API 必须提供一致的接口来访问和操作资源
68+
69+
#### Separation of concers: The client-server architecture principle
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+

content/posts/uuid.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
+++
2+
date = '2025-08-16T8:00:00+08:00'
3+
draft = false
4+
title = 'Intorduce UUID'
5+
+++
6+
7+
UUID(Universally Unique Identifier, 通用唯一标识符) 是一种标准化的128位标识符, 用于在分布式系统中生成几乎不会重复的唯一 ID. 最早于 IETF 制定为 RFC 4122 标准, 保证在不同机器、不同时间生成的 ID 也能保持全局唯一.
8+
9+
UUID 通常以16进制表示, 采用5段结构, 用连字符 `-` 分隔, 例如:
10+
```
11+
550e8400-e29b-41d4-a716-446655440000
12+
```
13+
14+
有如下特点:
15+
- 全局唯一
16+
- 无中心依赖
17+
- 不可预测
18+
- 跨平台通用
19+
20+
UUID 有以下不同版本:
21+
22+
| 版本 | 生成 | 特点 |
23+
| :--- | :--- | :--- |
24+
| v1 | 基于时间戳 + MAC 地址 | 按时间排序,含生成设备信息 |
25+
| v3 | 基于命名空间的 MD5 哈希 | 输入相同则输出相同(MD5 已不再安全) |
26+
| v4 | 基于操作系统的随机数生成 | 完全随机, 最常用 |
27+
| v5 | 基于命名空间的 SHA-1 哈希 | 与 v3 类似, 但使用 SHA-1 |
28+
| v6~v8 | 现代版本(草案) | 提高排序性能和隐私保护 |
29+
30+
其中, 对于需要时间有序的使用 v1, 大多数通用场景使用 v4
31+
32+
UUID 的应用场景
33+
- 数据库主键(分布式环境避免冲突)
34+
- 会话标识(Session ID、Token)
35+
- 文件命名(防止重名)
36+
- 分布式系统节点 ID
37+
- 追踪请求链路(Trace ID)
38+
39+
示例代码
40+
```Python
41+
import uuid
42+
43+
# 生成 UUID v1
44+
u1 = uuid.uuid1()
45+
print("UUID v1:", u1)
46+
47+
# 生成 UUID v4(随机)
48+
u4 = uuid.uuid4()
49+
print("UUID v4:", u4)
50+
51+
# 生成 UUID v3(命名空间 + MD5)
52+
u3 = uuid.uuid3(uuid.NAMESPACE_DNS, "example.com")
53+
print("UUID v3:", u3)
54+
55+
# 生成 UUID v5(命名空间 + SHA-1)
56+
u5 = uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")
57+
print("UUID v5:", u5)
58+
```
59+
> UUID v1: 9f7a1f7e-9e87-11ee-b15d-0242ac120002
60+
> UUID v4: 5f9b44e4-62af-4d13-bd4c-52de5f028f33
61+
> UUID v3: 9073926b-929f-31c2-abc9-fad77ae3e8eb
62+
> UUID v5: 2ed6657d-e927-568b-95e1-2665a8aea6a2
63+

public/archives/index.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@
5151
<span class="max-w-[4rem] md:max-w-none truncate">Home</span></a></li><li class="flex items-center gap-1 md:gap-2 min-w-0"><span class="text-muted-foreground/50 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
5252
</span><span class="text-foreground flex items-center gap-0.5 md:gap-1 font-medium min-w-0 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg>
5353
<span class="max-w-[3rem] md:max-w-none truncate">Archives</span></span></li></ol></nav><header class=mb-8><div class="mb-4 flex items-center gap-3"><svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg><h1 class="text-foreground text-3xl font-bold">Archives</h1></div><p class="text-muted-foreground mb-6">Browse all articles in chronological order and discover what interests you.</p><div class="text-muted-foreground flex items-center gap-4 text-sm"><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
54-
<span>14 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55-
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">14
56-
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">August 2025</h3><p class="text-muted-foreground text-xs">14
57-
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/microservice-with-fastapi/ class=block>Microservice with FastAPI</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
54+
<span>15 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55+
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">15
56+
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">August 2025</h3><p class="text-muted-foreground text-xs">15
57+
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/intorduce-uuid/ class=block>Intorduce UUID</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
58+
<time datetime=2025-08-16>08-16</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
59+
<span>1
60+
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/microservice-with-fastapi/ class=block>Microservice with FastAPI</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
5861
<time datetime=2025-08-15>08-15</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
5962
<span>10
6063
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/python-generics/ class=block>Python Generics</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>

0 commit comments

Comments
 (0)