vault backup: 2026-05-17 21:40:31

This commit is contained in:
hhs
2026-05-17 21:40:31 +08:00
parent bdeb889ee4
commit df6489e370
14 changed files with 3173 additions and 295 deletions
+51 -19
View File
@@ -1,6 +1,6 @@
---
tags: [microservice, service-discovery, consul, nacos, etcd, kubernetes]
create time: 2026-05-05
create time: 2026-05-05 00:00
---
# 服务发现
@@ -20,7 +20,8 @@ graph TB
A[Service A] -->|1.查询注册中心| R1[(Registry)]
R1 -->|2.返回实例列表| A
A -->|3.自选实例直连| B[Service B]
noteA[客户端内置负载均衡逻辑]
noteA["客户端内置负载均衡逻辑"]
B -.-> noteA
end
subgraph Server["服务端发现模式"]
@@ -28,7 +29,8 @@ graph TB
P -->|查询注册中心| R2[(Registry)]
R2 -->|返回实例| P
P -->|转发| D[Service D]
noteC[客户端无感知,透明代理]
noteC["客户端无感知,透明代理"]
D -.-> noteC
end
```
@@ -47,7 +49,7 @@ graph TB
**优势**:
- 直连调用,少了一跳网络开销
- 客户端可以根据本地缓存做智能选择(如就近节点优先)
- 框架成熟,Eureka/Consul 社区案例丰富
- 框架成熟,Eureka / Consul 社区案例丰富
**劣势**:
- 每个服务实现中都需要集成 SDK,跨语言迁移成本高
@@ -73,34 +75,44 @@ graph TB
阿里开源,支持 AP(临时实例)+ CP(持久实例)双模型,同时提供配置管理功能。
```go
// Nacos 服务注册 & 拉取
// Nacos 服务注册与实例拉取
import (
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
)
// 创建注册客户端
// 1. 创建服务端配置(连接哪个 Nacos)
sc := constant.ServerConfig{
IpAddr: "127.0.0.1",
Port: 8848,
}
```
// 注册服务实例
> 上面仅展示了 serverConfig,实际创建时还需传入 `ClientConfig`(命名空间、超时等)。为简洁起见此处省略。
```go
// 2. 创建 NamingClient
client, _ := clients.NewNamingClient(
value_map.NewValueMap(map[string]any{"serverConfig": sc}),
map[string]any{"serverConfigs": []constant.ServerConfig{sc}},
)
client.RegisterInstance(naming_param.RegisterInstanceParam{
// 3. 注册服务实例(默认临时实例,走心跳保活)
_ = client.RegisterInstance(vo.RegisterInstanceParam{
Ip: "10.0.0.1",
Port: 8080,
ServiceName: "order-service",
ClusterName: "DEFAULT",
})
// 拉取某服务的可用实例列表
instances, _ := client.SelectInstances(naming_param.SelectInstanceParam{
// 4. 拉取某服务的可用实例列表(同步快照,含健康过滤)
instances, _, _ := client.SelectInstances(vo.SelectInstanceParam{
ServiceName: "order-service",
HealthyOnly: true, // 只返回健康实例
})
for _, inst := range instances {
// inst.Ip: inst.Port — 拿到后可直接发起 HTTP / gRPC 调用
}
```
### Consul
@@ -112,6 +124,18 @@ HashiCorp 出品,基于 Raft 共识协议(CP),天然支持健康检查
- 支持 multi-datacenter 部署
- DNS 接口:`order-service.service.consul` 可直接解析 IP
**与 Nacos 选型对比**:
| 维度 | Consul | Nacos |
|------|--------|-------|
| 一致性模型 | CP(Raft) | AP + CP 可切换 |
| 部署复杂度 | 需独立部署 Agent(Sidecar 模式) | Go SDK 内置,无额外代理 |
| 生态集成 | Terraform / Vault 深度绑定 | Spring Cloud / Dubbo 原生支持 |
| 适用场景 | 多云 / 混合云、基础设施层 | Java 技术栈、业务服务层 |
> [!tip] Consul Sidecar 模式
> Consul Agent 通常以 DaemonSet 方式跑在每台节点上,应用通过 localhost 向本地 Agent 注册。这种 **Sidecar 模式**天然适配服务端发现架构——应用把 Consul Agent 当作"本地注册中心"即可。
### etcd + K8s Service
纯容器环境下最自然的选择。etcd 存数据,K8s 提供完整的发现和负载均衡体系。
@@ -186,17 +210,25 @@ sequenceDiagram
4. 进程退出
> [!tip] K8s Pod 优雅终止
> 以 sidecar 方式集成 Consul Agent 时,Pod 配置如下:
> ```yaml
> spec:
> terminationGracePeriodSeconds: 30 # 最多等 30 秒再 SIGKILL
> ```
> 配合 `preStop` Hook 可以提前摘除流量:
> ```yaml
> lifecycle:
> preStop:
> exec:
> command: ["sh", "-c", "sleep 15"] # 给负载均衡摘流的时间
> terminationGracePeriodSeconds: 30 # 最多等 30 秒再 SIGKILL
> initContainers:
> - name: consul-init # 先启动 Sidecar
> image: consul:latest
> command: ["consul", "agent", "-bind", "-config-file=/etc/consul.d/server.json"]
> containers:
> - name: app
> lifecycle:
> preStop:
> exec:
> command: ["sh", "-c", "sleep 15"] # 先停流量(让 LB / Consul 摘流)
> postStart:
> exec:
> command: ["sh", "-c", "sleep 5"] # 等 Sidecar 就绪后再开流量
> ```
> 关键点:**preStop sleep** 留给 LB / Consul 摘流时间,**postStart sleep** 确保 Sidecar 已准备好再做健康检查。
## 关联笔记