Spring Cloud 2.2.2 源码之六十二nacos数据一致性原理之临时结点数据同步二

本文深入探讨SpringCloud2.2.2中Nacos数据一致性原理,解析临时结点数据同步流程,包括DataStore的batchGet方法如何高效处理重复key,以及NamingProxy的syncData同步机制。同时,分析DistroController的onSyncDatum方法如何调用DistroConsistencyServiceImpl的onPut进行数据保存和更新通知。

Spring Cloud 2.2.2 源码之六十二nacos数据一致性原理之临时结点数据同步二

nacos数据一致性服务执行流程

在这里插入图片描述

DataStore的batchGet

上篇说到要进行数据同步了,我们先来看下任务key=com.alibaba.nacos.naming.iplist.ephemeral.命名空间##分组@@服务名,所以可能队列里会有多个这样的服务名。尽管是有多个线程去队列去取,但是如果同样的key很多的话,可能一个线程会取到同样的key多个,不过没关系,后面获取数据的时候只能获取一份最新的。
在这里插入图片描述
在这里插入图片描述
这里keys可能有相同的,但是dataMap里只能有一份,而且是最新的,所以不用担心。

    public Map<String, Datum> batchGet(List<String> keys) {
        Map<String, Datum> map = new HashMap<>(128);
        for (String key : keys) {
            Datum datum = dataMap.get(key);
            if (datum == null) {
                continue;
            }
            map.put(key, datum);
        }
        return map;
    }

NamingProxy的syncData数据同步

json序列化之后要进行同步啦:
在这里插入图片描述
uri/distro/datum,这里先点到为止,后面会详细说。
在这里插入图片描述

DistroController的onSyncDatum

最终还是调用DistroConsistencyServiceImplonPut,进行数据保存和更新通知:
在这里插入图片描述
在这里插入图片描述
后面就是UDP通知客户端更新了,主要流程可以看上面的图。后面说永久结点的数据同步。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

**Spring Cloud Netflix 2.2.2** 是 Spring Cloud 生态中用于集成 **Netflix OSS(Open Source Software)组件** 的官方模块集合,旨在为基于 JVM 的微服务架构提供一套完整的分布式系统解决方案。该版本发布于 2020 年底,属于 **Spring Cloud Netflix 项目活跃维护的最后阶段之一**,之后该项目进入维护模式,官方推荐迁移到更现代的替代方案。 > ✅ 对应 Spring Boot 版本:**2.4.x** > 📦 所属版本线:**Spring Cloud 2020.0 (Ilford)** 的一部分 > 🔗 官方仓库:[https://github.com/spring-cloud/spring-cloud-netflix](https://github.com/spring-cloud/spring-cloud-netflix) --- ### 🧩 核心组件一览 | 组件 | 功能说明 | |------|----------| | **Eureka Server / Client** | 服务注册与发现(Service Registry and Discovery) | | **Ribbon** | 客户端负载均衡(Client-side Load Balancing) | | **Hystrix** | 断路器模式实现,防止雪崩效应 | | **Hystrix Dashboard** | 实时监控 Hystrix 指标面板 | | **Zuul 1.x** | 边缘网关(API Gateway),支持路由和过滤 | | **Archaius** | 外部化配置管理(已被 Spring Cloud Config 取代) | --- ### 💡 典型微服务架构图(使用 Spring Cloud Netflix) ```text +------------------+ | Client | +--------+---------+ | +-------v--------+ | Zuul (API Gateway) +-------+--------+ | +---------v-----------+ +------------------+ | Eureka Server |<--->| Service Registry | +---------+-----------+ +------------------+ | +-------------+--------------+--------------+ | | | | +----v----+ +----v-----+ +----v-----+ +----v-----+ | OrderSvc| | UserSvc | | PaySvc | | LogSvc | | @Port 81| | @Port 82 | | @Port 83 | | @Port 84 | +---------+ +----------+ +----------+ +----------+ ↑ ↑ | Ribbon | Hystrix Command | 负载均衡 | 熔断/降级 ↓ ↓ +--------------------------------------------------+ | Hystrix Dashboard | | 监控各服务调用延迟、失败率等指标 | +--------------------------------------------------+ ``` --- ## 🔧 各组件详解(以 2.2.2 版本为准) ### 1. **Eureka Server(服务注册中心)** #### 添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> ``` #### 启用注解: ```java @SpringBootApplication @EnableEurekaServer public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); } } ``` #### 配置文件 `application.yml`: ```yaml server: port: 8761 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ ``` > ✅ 生产建议:搭建高可用集群,相互注册。 --- ### 2. **Eureka Client(服务提供者 & 消费者)** #### 添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ``` #### 自动注册到 Eureka: ```yaml spring: application: name: order-service server: port: 8081 eureka: client: service-url: defaultZone: http://localhost:8761/eureka ``` 无需额外代码即可被发现。 --- ### 3. **Ribbon(客户端负载均衡)** > ⚠️ 在 2.2.2 中仍默认启用,但已标记为 **DEPRECATED** #### 使用方式(RestTemplate + @LoadBalanced): ```java @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } ``` ```java @Service public class OrderService { @Autowired private RestTemplate restTemplate; public String callUserService() { // 自动通过 Eureka 查找 user-service 实例并负载均衡 return restTemplate.getForObject("http://user-service/api/user", String.class); } } ``` > ✅ 替代方案:**Spring Cloud LoadBalancer**(从 2020.0 开始默认) --- ### 4. **Hystrix(断路器)** #### 添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> ``` #### 启用熔断: ```java @SpringBootApplication @EnableCircuitBreaker public class OrderApp { ... } ``` 或使用组合注解: ```java @EnableHystrix ``` #### 编写容错逻辑: ```java @HystrixCommand(fallbackMethod = "getDefaultUser") public String getUser() { return restTemplate.getForObject("http://user-service/api/user", String.class); } public String getDefaultUser() { return "Fallback User"; } ``` --- ### 5. **Hystrix Dashboard(监控面板)** #### 添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` #### 启用仪表盘: ```java @EnableHystrixDashboard ``` 访问地址:`http://localhost:8081/hystrix` 输入流地址:`http://user-service/actuator/hystrix.stream` > ⚠️ 注意:需每个服务暴露 `/actuator/hystrix.stream` 端点。 --- ### 6. **Zuul 1.x(边缘网关)** #### 添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> ``` #### 启用网关: ```java @EnableZuulProxy ``` #### 路由配置: ```yaml zuul: routes: order: path: /api/order/** service-id: order-service user: path: /api/user/** url: http://localhost:8082 ``` > ❌ 局限性:Zuul 1.x 基于阻塞 I/O,性能不如 Zuul 2.x 或 Spring Cloud Gateway。 --- ### ✅ 优势总结(截至 2.2.2) | 优点 | 说明 | |------|------| | 🚀 快速构建微服务骨架 | 几行配置即可完成服务注册、发现、调用 | | 🔗 组件完整闭环 | 提供从网关 → 注册中心 → 熔断 → 监控全链路支持 | | 📊 易于调试与可视化 | Eureka UI、Hystrix Dashboard 提供直观视图 | | 🧪 成熟稳定 | 经过大规模生产验证(如 Netflix 自身使用) | --- ### ⚠️ 局限性与现状 | 问题 | 说明 | |------|------| | ❌ **项目进入维护模式** | 自 Spring Cloud 2020.0 起,Netflix 组件不再积极开发 | | ❌ **Ribbon 和 Hystrix 已停更** | Netflix 官方停止维护这两个库 | | ❌ **Zuul 1.x 性能瓶颈** | 不支持响应式编程,无法应对高并发场景 | | ❌ **缺乏对 WebFlux 支持** | 与 Spring WebFlux、Reactor 不兼容 | | ❌ **内存占用较高** | 尤其是 Hystrix 线程池模型导致资源消耗大 | --- ### 🔁 官方推荐替代方案(Spring Cloud 2020+) | 原组件 | 推荐替代 | |--------|-----------| | **Eureka Server** | 可继续使用(社区维护),或替换为 **Consul / Nacos** | | **Ribbon** | → **Spring Cloud LoadBalancer**(基于 Reactor) | | **Hystrix** | → **Resilience4j**(轻量、函数式、无反射开销) | | **Zuul 1.x** | → **Spring Cloud Gateway**(基于 WebFlux,高性能) | | **Hystrix Dashboard** | → **Micrometer + Prometheus + Grafana** | | **Archaius** | → **Spring Cloud Config + Bootstrap 配置迁移** | --- ### ✅ 迁移示例:Zuul → Spring Cloud Gateway #### 添加依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> ``` #### 配置路由: ```yaml spring: cloud: gateway: routes: - id: order_route uri: lb://order-service predicates: - Path=/api/order/** filters: - AddRequestHeader=X-Trace-ID, {uuid} ``` > ✅ 支持异步非阻塞、动态路由、限流、熔断集成(通过 Resilience4j)。 --- ### ✅ 总结:Spring Cloud Netflix 2.2.2 的定位 | 项目 | 结论 | |------|------| | **历史地位** | 微服务革命的关键推动者,定义了早期 Spring 微服务标准 | | **当前状态** | **维护模式(Maintenance Mode)**,仅修复严重 Bug | | **适用场景** | - 现有老系统维护<br>- 学习微服务核心概念(服务发现、熔断、网关) | | **新项目建议** | 使用 **Spring Cloud Gateway + LoadBalancer + Resilience4j + Nacos/Consul** | | **学习价值** | 极高 —— 理解分布式系统设计思想的基础 | > 📣 **一句话总结:它是“微服务时代的奠基者”,虽已退出舞台中央,但仍是理解云原生架构演进的重要里程碑。** ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值