Skip to content

Conversation

@luky116
Copy link
Contributor

@luky116 luky116 commented Dec 26, 2025

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

本次 PR 修复了 HTTP TPS(每秒事务数)控制相关的两个问题:

  1. 修复 Spring Boot 中 TPS 点重复注册问题:在 Spring Boot 应用中,ContextRefreshedEvent 会被触发两次 - 一次是根 ApplicationContext,一次是 Web ApplicationContext。之前的方法,优先注册的是 根 context,导致本地带了 @TpsControl 注释的方法没注册上。本次修复确保只处理根上下文,并添加了异常处理和重试能力。

  2. 使用正确的 HTTP 状态码:当 TPS 超过限制时,将 HTTP 状态码从 503 (Service Unavailable) 改为 429 (Too Many Requests),这是根据 RFC 6585 标准用于限流场景的标准 HTTP 状态码。

  3. 添加 TPS 超限的 HTTP 指标记录:为 TPS 超限场景添加了 Micrometer 指标记录,以支持更好的监控和可观测性;按照之前的逻辑,如果被限流拦截,并不会记录 metrics

Brief changelog

  • HttpTpsPointRegistry.java:

    • 通过检查上下文是否为根上下文(parent == null)修复重复注册问题
    • 添加异常处理和重试能力(失败时重置 isInit 标志)
  • NacosHttpTpsFilter.java:

    • 将 TPS 超限响应的 HTTP 状态码从 503 改为 429
    • 添加 recordHttpMetrics() 方法使用 Micrometer 记录指标
    • generate503Response() 重命名为 generate429Response() 以提高可读性

Verifying this change

  1. 测试重复注册修复:

    • 在 Spring Boot 环境中启动 Nacos 服务器
    • 验证 TPS 控制点只注册一次,而不是两次
    • 检查日志确保没有重复注册警告
  2. 测试 HTTP 状态码:

    • 发送超过 TPS 限制的请求
    • 验证响应状态码是 429 (Too Many Requests) 而不是 503
    • 验证响应体包含适当的错误消息
  3. 测试指标记录:

    • 发送超过 TPS 限制的请求
    • 检查 Micrometer 指标端点(如果可用)或指标注册表
    • 验证 http.server.requests 计时器被记录,包含:
      • status=429
      • exception=TpsOverLimitException
      • outcome=TPS_OVER_LIMIT_ERROR
      • 正确的 methoduri 标签

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.

@luky116 luky116 changed the title fix: update HttpTpsPointRegistry and NacosHttpTpsFilter bugfix:修复 HTTP TPS 限流时,metrics 采集的问题 Dec 26, 2025
@luky116 luky116 changed the title bugfix:修复 HTTP TPS 限流时,metrics 采集的问题 bugfix:修复 HTTP TPS 限流时 metrics 采集的问题 Dec 26, 2025
@github-actions
Copy link

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API新增配置参数修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

@NacosWebBean
public class HttpTpsPointRegistry implements ApplicationListener<ContextRefreshedEvent> {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't change the indent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wuyfee
Copy link

wuyfee commented Jan 9, 2026

$\color{red}{FAILURE}$
DETAILS
✅ - docker: success
❌ - deploy (standalone & cluster & standalone_auth): failure
❌ - e2e-java-test (standalone & cluster & standalone_auth): skipped
❌ - e2e-go-test (standalone & cluster): skipped
❌ - e2e-cpp-test (standalone & cluster): skipped
❌ - e2e-csharp-test (standalone & cluster): skipped
❌ - e2e-nodejs-test (standalone & cluster): skipped
❌ - e2e-python-test (standalone & cluster): skipped
✅ - clean (standalone & cluster & standalone_auth): success

@KomachiSion
Copy link
Collaborator

@luky116 CI can't pass please fix it.

@KomachiSion KomachiSion closed this Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants