一、技术选型说明
mermaid
graph TD
A[Spring Cloud Alibaba 2022.0.0] --> B[Nacos 2.2.3]
A --> C[Sentinel 1.8.6]
A --> D[Seata 1.7.0]
A --> E[RocketMQ 4.9.4]
二、环境准备
1. 版本对应关系
组件 | 版本 |
Spring Boot | 3.1.4 |
Spring Cloud | 2022.0.4 |
Spring Cloud Alibaba | 2022.0.0.0 |
Nacos Server | 2.2.3 |
2. Maven父工程配置
xml
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2022.0.0.0
pom
import
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
三、Nacos服务端部署
1. 生产环境推荐架构
mermaid
graph LR
VIP --> N1[Nacos节点1]
VIP --> N2[Nacos节点2]
VIP --> N3[Nacos节点3]
N1 --> MySQL集群
N2 --> MySQL集群
N3 --> MySQL集群
2. Docker-Compose集群部署
yaml
version: '3'
services:
nacos1:
image: nacos/nacos-server:v2.2.3
environment:
- MODE=cluster
- NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=mysql
- MYSQL_SERVICE_DB_NAME=nacos
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=123456
networks:
- nacos_net
nacos2:
image: nacos/nacos-server:v2.2.3
environment: [...]
nacos3:
image: nacos/nacos-server:v2.2.3
environment: [...]
四、微服务核心模块搭建
1. 服务注册与发现
服务提供方配置
yaml
spring:
application:
name: user-service
cloud:
nacos:
discovery:
server-addr: 192.168.1.100:8848,192.168.1.101:8848
namespace: dev-env
group: BUSINESS_GROUP
metadata:
version: 2.0
region: hangzhou
服务消费方实现
java
@RestController
public class OrderController {
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/services")
public List getServices() {
return discoveryClient.getInstances("user-service");
}
}
2. 配置中心集成
bootstrap.yml配置
yaml
spring:
cloud:
nacos:
config:
server-addr: ${NACOS_HOST:localhost}:8848
file-extension: yaml
shared-configs:
- data-id: common-config.yaml
group: GLOBAL_GROUP
refresh: true
extension-configs:
- data-id: datasource-config.yaml
group: DATABASE_GROUP
refresh: false
动态配置刷新
java
@RefreshScope
public class PaymentConfig {
@Value("${payment.timeout:3000}")
private Integer timeout;
}
五、服务通信方案
1. OpenFeign增强配置
java
@FeignClient(
name = "inventory-service",
configuration = FeignConfig.class,
fallbackFactory = InventoryFallbackFactory.class
)
public interface InventoryClient {
@GetMapping("/stock/{skuId}")
Integer getStock(@PathVariable Long skuId);
}
// 自定义配置类
public class FeignConfig {
@Bean
public Retryer retryer() {
return new Retryer.Default(100, 1000, 3);
}
}
2. RestTemplate负载均衡
java
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(3000);
factory.setReadTimeout(5000);
return new RestTemplate(factory);
}
六、服务治理增强
1. 流量权重控制
properties
# 在Nacos控制台设置metadata
preserved.register.weight=80 # 取值范围1-100
2. 服务元数据路由
java
@Bean
public NacosRule nacosRule() {
NacosRule rule = new NacosRule();
rule.setMetadataFilterEnabled(true); // 开启元数据过滤
return rule;
}
七、配置中心高级用法
1. 多环境配置隔离
命名空间规划:
- dev: 开发环境
- test: 测试环境
- prod: 生产环境
数据ID命名规范:
{应用名}-{环境}.{文件格式}
示例:user-service-dev.yaml
2. 敏感配置加密
yaml
# 使用Jasypt加密
spring:
cloud:
nacos:
config:
password: ENC(AQC4F9O6vXrZ6PwC5J4jDg==)
八、监控与运维
1. 健康检查端点
yaml
management:
endpoints:
web:
exposure:
include: health,info,nacos-discovery
endpoint:
health:
show-details: always
2. 日志追踪集成
xml
org.springframework.cloud
spring-cloud-starter-sleuth
org.springframework.cloud
spring-cloud-sleuth-zipkin
九、最佳实践建议
- 服务粒度控制:按照业务能力划分服务,单个服务代码量控制在5万行以内
- 配置管理规范:
- 基础配置使用shared-configs
- 应用专属配置使用extension-configs
- 环境差异配置使用namespace隔离
- 版本控制策略:
- properties
# 在metadata中定义 version=2.1.0 canary.enabled=true
- 服务鉴权方案:
- yaml
spring:
cloud:
nacos:
discovery:
access-key: AKID
secret-key: SECRET
十、常见问题排查
问题1:服务注册失败
- 检查点:
- Nacos Server是否正常启动
- 网络连通性(telnet测试8848端口)
- 配置的namespace是否存在
- 依赖是否正确引入
问题2:配置读取异常
- 排查步骤:
- 检查bootstrap.yml优先级
- 确认dataId命名规范
- 查看Nacos控制台配置内容
- 开启debug日志:
- properties
logging.level.com.alibaba.nacos=DEBUG
扩展能力集成:
- 分布式事务:接入Seata框架
- 流量防护:集成Sentinel控制台
- 消息驱动:使用RocketMQ实现事件驱动架构
- 服务网格:结合Istio实现双模微服务
通过本方案可快速搭建基于Nacos的Spring Cloud微服务体系,建议在实施过程中:
- 建立完善的配置变更审核流程
- 制定服务命名规范(建议使用小写中线格式)
- 定期进行服务健康度巡检
- 使用Prometheus+Grafana构建监控大盘