Nacos
Nacos(Naming and Configuration Service)是阿里巴巴开源的服务发现 + 动态配置平台,是国内微服务架构中最主流的注册中心之一,支持 AP 和 CP 两种一致性模式切换。
核心功能
| 功能 | 说明 |
|---|
| 服务注册与发现 | 支持 DNS 和 RPC 两种发现模式 |
| 动态配置管理 | 替代 Spring Cloud Config + Bus |
| 服务健康检查 | 心跳、TCP、HTTP、MySQL 多种探针 |
| 流量管理 | 权重路由、灰度发布 |
| 命名空间隔离 | 多环境(dev/test/prod)配置隔离 |
整体架构
┌──────────────────────────────────────────┐
│ Nacos Server │
│ ┌──────────┐ ┌──────────┐ ┌────────┐ │
│ │ Naming │ │ Config │ │Console │ │
│ │ Service │ │ Service │ │ (UI) │ │
│ └────┬─────┘ └────┬─────┘ └────────┘ │
│ │ │ │
│ ┌────┴──────────────┴────────────────┐ │
│ │ Raft / Distro 一致性协议 │ │
│ └────────────────────────────────────┘ │
│ ┌────────────────────────────────────┐ │
│ │ 持久化存储(内嵌 Derby / MySQL) │ │
│ └────────────────────────────────────┘ │
└──────────────────────────────────────────┘
↕ HTTP / gRPC(2.x)
┌─────────┐ ┌─────────┐
│Provider │ │Consumer │
└─────────┘ └─────────┘
AP vs CP 模式
| 模式 | 协议 | 适用实例类型 | 特点 |
|---|
| AP(默认) | Distro | 临时实例(ephemeral) | 可用性优先,客户端心跳维持 |
| CP | Raft | 持久实例(persistent) | 强一致,注册需 Leader 写入 |
# 切换为 CP(持久实例)
spring:
cloud:
nacos:
discovery:
ephemeral: false # 持久实例,使用 Raft
服务发现集成
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
spring:
application:
name: order-service
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: dev # 命名空间(环境隔离)
group: DEFAULT_GROUP
cluster-name: SH # 集群名(同集群优先路由)
weight: 1 # 路由权重 1~100
动态配置管理
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
# bootstrap.yml
spring:
application:
name: order-service
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
namespace: dev
group: DEFAULT_GROUP
# 默认 dataId = ${spring.application.name}.${file-extension}
# = order-service.yaml
@RefreshScope // 配置变更后自动刷新 Bean
@RestController
public class OrderController {
@Value("${order.timeout:5000}")
private int timeout;
}
配置 DataId 命名规则
${prefix}-${spring.profiles.active}.${file-extension}
| 示例 DataId | 含义 |
|---|
order-service.yaml | 默认配置 |
order-service-dev.yaml | dev 环境配置 |
shared-db.yaml | 多服务共享配置 |
健康检查
| 探针类型 | 配置方式 | 说明 |
|---|
| 客户端心跳 | 默认(临时实例) | Client 每 5s 发心跳,15s 不健康,30s 删除 |
| HTTP | 持久实例 | Server 主动探测 |
| TCP | 持久实例 | 检查端口可达性 |
| MySQL | 持久实例 | 执行 SQL 检查 |
高可用集群
nacos1:8848 ─┐
nacos2:8848 ─┼── 共享 MySQL(推荐)
nacos3:8848 ─┘
# cluster.conf
192.168.1.1:8848
192.168.1.2:8848
192.168.1.3:8848
# application.properties(生产)
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8
db.user.0=nacos
db.password.0=nacos
命名空间 + Group 多维隔离
命名空间(Namespace) → 环境隔离(dev / test / prod)
Group → 业务线隔离(ORDER / USER / PAYMENT)
DataId / ServiceName → 具体服务 / 配置
与 Eureka / Consul 对比
| 特性 | Nacos | Eureka | Consul |
|---|
| 注册中心 | ✅ | ✅ | ✅ |
| 配置中心 | ✅ | ❌ | ✅(KV) |
| 健康检查 | 心跳 / 主动 | 心跳 | TCP/HTTP/Script |
| CAP 模式 | AP + CP | AP | CP |
| 多数据中心 | 支持 | 弱 | 原生 |
| 生态 | Spring Cloud Alibaba | Spring Cloud Netflix | 多语言 |
相关链接
Spring Cloud 实战