← 返回学习路线

SYSTEM

第 6 周:微服务完善 + 配置中心 + 服务治理

面试复习第 6 周:微服务完善 + 配置中心 + 服务治理growth
src/content/notes/growth/第-6-周:微服务完善-配置中心-服务治理.md

第 6 周:微服务完善 + 配置中心 + 服务治理

第 6 周 星期一(第 26 天)

  • 今日目标:Nacos 配置中心统一管理各服务配置 + 配置热更新
  • 项目改造任务:
    • 修改文件:blog-cloud/blog-gateway/pom.xml(新增 Nacos Config)
      <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
      </dependency>
    • 新建文件:blog-cloud/blog-gateway/src/main/resources/bootstrap.yml
      spring:
        application:
          name: gateway-service
        cloud:
          nacos:
            config:
              server-addr: localhost:8848
              file-extension: yaml
    • 在 Nacos 控制台 → 配置管理 → 新建配置:
      Data ID: gateway-service.yaml
      配置内容:写 gateway 路由规则
    • 添加 @RefreshScope 注解(后续给配置类加上)
    • 预期效果:修改 Nacos 中的配置,不重启服务即可生效
  • 核心知识点:
    • 学习内容:配置中心的作用(动态配置/环境隔离/配置审计)、bootstrap.yml 加载顺序(比 application.yml 优先)、@RefreshScope 热刷新
    • 学习资源:Nacos Config 官方文档(读 Spring Cloud 整合部分)
  • LeetCode刷题:
  • 今日检查项:
    • Nacos 中修改配置后,服务不重启就生效
    • bootstrap.yml 配置正确,服务能启动
  • 常见问题与解决方案:
    • bootstrap.yml 不加载:Spring Boot 3.x 需手动添加 spring-cloud-starter-bootstrap 依赖
    • 配置没热更新:确认类上有 @RefreshScope 注解

第 6 周 星期二(第 27 天)

  • 今日目标:Sentinel 流量控制 + 熔断降级(保护文章服务)
  • 项目改造任务:
    • 修改文件:blog-cloud/blog-article-service/pom.xml
      <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
      </dependency>
    • 修改文件:blog-cloud/blog-article-service/src/main/resources/application.yml
      spring:
        cloud:
          sentinel:
            transport:
              dashboard: localhost:8080  # Sentinel 控制台地址
            eager: true
    • 新建文件:blog-cloud/blog-article-service/.../config/SentinelConfig.java
      @Configuration
      public class SentinelConfig {
          @Bean
          public SentinelResourceAspect sentinelResourceAspect() {
              return new SentinelResourceAspect();
          }
      }
    • 修改文件:blog-cloud/blog-article-service/.../controller/ArticleController.java
      @GetMapping("/list")
      @SentinelResource(value = "articleList", 
                        blockHandler = "listBlockHandler",
                        fallback = "listFallback")
      public Result<Page<Article>> getAllArticles(...) { ... }
      
      // 限流后执行
      public Result<Page<Article>> listBlockHandler(...) {
          return Result.error("请求太频繁,请稍后重试");
      }
    • 预期效果:文章列表接口被限流时返回友好提示
  • 核心知识点:
    • 学习内容:Sentinel 核心概念(资源/规则/流控模式/熔断降级)、与 Hystrix 对比、Sentinel Dashboard 安装
    • 学习资源:Sentinel 官方文档 - 流量控制(读基本概念 + Spring Cloud 整合)
  • LeetCode刷题:
  • 今日检查项:
    • Sentinel Dashboard 能显示服务接口
    • 配置限流规则后,超限请求返回”请求太频繁”
  • 常见问题与解决方案:
    • Sentinel Dashboard 连接失败:先启动 Dashboard(java -jar sentinel-dashboard.jar)
    • blockHandler 方法签名必须和原方法匹配(加 BlockException 参数)

第 6 周 星期三(第 28 天)

  • 今日目标:Seata 分布式事务实战(评论 → 积分增加 → 异步通知,保证最终一致性)
  • 项目改造任务:
    • 修改文件:blog-cloud/blog-article-service/pom.xml
      <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
      </dependency>
    • 修改文件:blog-cloud/.../service/impl/CommentServiceImpl.java
      @Override
      @GlobalTransactional(name = "create-comment-tx", rollbackFor = Exception.class)
      public Comment createComment(Long articleId, Long userId, String content, Long parentId) {
          // 1. 保存评论
          Comment comment = new Comment();
          comment.setArticleId(articleId);
          comment.setUserId(userId);
          comment.setContent(content);
          comment.setParentId(parentId);
          comment.setCreateTime(LocalDateTime.now());
          comment = commentRepository.save(comment);
          
          // 2. 远程调用用户服务,给评论者加积分(模拟)
          // userFeignClient.addScore(userId, 5);  // 评论一次 +5 分
          
          // 如果上面抛出异常,评论也会回滚
          return comment;
      }
    • 预期效果:评论和加积分在同一个全局事务中,任一失败都回滚
  • 核心知识点:
    • 学习内容:Seata AT 模式原理(全局事务/分支事务/undo log)、TCC 模式、Saga 模式
    • 学习资源:Seata 官方文档 - AT 模式(读核心原理)
  • LeetCode刷题:
  • 今日检查项:
    • Seata Server(TC)启动成功
    • 注释掉的事务管理能正常提交和回滚
  • 常见问题与解决方案:
    • Seata 端口冲突:默认 8091 端口,改 file.conf 中的配置
    • undo_log 表必须存在:在数据库中执行 CREATE TABLE undo_log 建表脚本

第 6 周 星期四(第 29 天)

  • 今日目标:Elasticsearch 全文检索(替换文章搜索的 LIKE %keyword%)
  • 项目改造任务(可选,不熟悉 ES 可用第 9 天 MySQL 搜索实现代替):
    • 修改文件:blog/pom.xml
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
      </dependency>
    • 新建文件:blog/.../entity/ArticleDocument.java
      @Document(indexName = "articles")
      public class ArticleDocument {
          @Id
          private Long id;
          @Field(type = FieldType.Text, analyzer = "ik_max_word")
          private String title;
          @Field(type = FieldType.Text, analyzer = "ik_max_word")
          private String content;
          // ...
      }
    • 新建文件:blog/.../repository/ArticleSearchRepository.java
      public interface ArticleSearchRepository extends ElasticsearchRepository<ArticleDocument, Long> {
          List<ArticleDocument> findByTitleOrContent(String title, String content);
      }
    • 预期效果:搜索接口改为 ES 查询,支持中文分词、相关性排序
  • 核心知识点:
    • 学习内容:ES 倒排索引原理、ik 分词器、ElasticsearchRepository 使用
    • 学习资源:Elasticsearch 官方 - 入门(读前 3 章)
  • LeetCode刷题:
    • 题目:78. 子集(考察:回溯/位运算)
  • 今日检查项:
    • ES 服务运行中(localhost:9200 能访问)
    • 搜索”Spring”能返回文章标题包含”Spring”的结果
    • 中文分词正确(“分布式”不会被切分成”分”、“布”、“式”)
  • 常见问题与解决方案:
    • ES 中文搜索不准确:安装 ik 分词器 elasticsearch-plugin install analysis-ik
    • ES 内存要求高:修改 jvm.options 减小堆内存为 512M

第 6 周 星期五(第 30 天)

  • 今日目标:Phase 3 复盘 + 面试知识体系整理
  • 项目改造任务:无(收尾)
  • 核心知识点:
    • 学习内容:微服务架构整体知识树
      • 服务注册发现(Nacos)
      • 远程调用(Feign)
      • 网关(Gateway)
      • 配置中心(Nacos Config)
      • 流量控制/熔断(Sentinel)
      • 分布式事务(Seata)
      • 全文检索(ES)
    • 学习资源:无(自主整理脑图)
  • LeetCode刷题:
  • 今日检查项:
    • 整理 Spring Cloud 面试题 15 个
    • 能用自己的架构图画出来微服务调用链
    • 写一篇技术笔记:《微服务改造避坑指南》

第 6 周 星期六 & 星期日

  • 复盘任务:
    • 把 blog-cloud 代码推送到 GitHub,打 tag(v3.0-microservice)
    • 准备下阶段 Docker 学习环境

🚀 Phase 4:Docker 部署 + 面试冲刺(第 7-8 周)