-
-
Notifications
You must be signed in to change notification settings - Fork 9k
添加多租户共享模式以优化资源使用 #3840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
添加多租户共享模式以优化资源使用 #3840
Conversation
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
|
auggie review |
🤖 Augment PR SummarySummary: 本 PR 为 WxJava 的 MiniApp/MP 多租户 Spring Boot Starter 新增可配置的“共享模式”,在大量租户场景下复用 SDK 的 Changes:
Technical Notes: 共享模式依赖 ThreadLocal 进行租户切换,同一线程内务必在每次调用前完成切换;异步/线程池场景需显式传递必要信息以避免错用租户配置。 🤖 Was this summary useful? React with 👍 or 👎 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| ## 问题背景 | ||
|
|
||
| 用户在 issue #3909 中提出了一个架构设计问题: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return null; | ||
| } | ||
| // 切换到指定租户的配置 | ||
| return sharedWxMaService.switchoverTo(tenantId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里使用 switchoverTo 在租户不存在时会抛 WxRuntimeException,与现有 WxMaMultiServicesImpl#getWxMaService 未命中时返回 null 的行为不一致,可能破坏“共享/隔离模式 API 一致”的预期。建议明确并统一“tenantId 不存在”时的返回/异常约定(MP 的 shared 实现也同理)。
Other Locations
spring-boot-starters/wx-java-mp-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/service/WxMpMultiServicesSharedImpl.java:29
🤖 Was this useful? React with 👍 or 👎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return null; | ||
| } | ||
| // 切换到指定租户的配置 | ||
| return sharedWxMaService.switchoverTo(tenantId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里调用 switchoverTo(tenantId) 在 tenantId 未配置/为空字符串时会直接抛 WxRuntimeException,而隔离模式 WxMaMultiServicesImpl#getWxMaService 会返回 null。这会导致两种模式在“租户不存在”场景下的行为不一致(也适用于 WxMpMultiServicesSharedImpl),建议在对外契约/文档中明确该差异以避免兼容性踩坑。
Other Locations
spring-boot-starters/wx-java-mp-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/service/WxMpMultiServicesSharedImpl.java:29
🤖 Was this useful? React with 👍 or 👎
|
|
||
| ## 问题背景 | ||
|
|
||
| 用户在 issue #3909 中提出了一个架构设计问题: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## 相关链接 | ||
|
|
||
| - Issue: [#3909](https://github.com/binarywang/WxJava/issues/3909) | ||
| - Pull Request: [待填写] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
本 PR 为 WxJava 的 Spring Boot Starter 多租户实现添加了可配置的共享模式,以优化大规模租户场景下的资源使用。
主要变更
- 新增
MultiTenantMode枚举,提供 ISOLATED(隔离)和 SHARED(共享)两种模式 - 新增共享模式实现类,使用单个 WxService 实例和 configMap 管理多租户配置
- 重构配置类以支持模式选择,同时保持向后兼容(默认使用 ISOLATED 模式)
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| WxMpMultiServicesSharedImpl.java | 新增公众号共享模式实现类,通过单个 WxMpService 实例管理多租户配置 |
| WxMaMultiServicesSharedImpl.java | 新增小程序共享模式实现类,通过单个 WxMaService 实例管理多租户配置 |
| WxMpMultiProperties.java | 添加 MultiTenantMode 枚举和 multi-tenant-mode 配置属性 |
| WxMaMultiProperties.java | 添加 MultiTenantMode 枚举和 multi-tenant-mode 配置属性 |
| AbstractWxMpConfiguration.java | 重构以支持两种多租户模式,提取通用方法以减少代码重复 |
| AbstractWxMaConfiguration.java | 重构以支持两种多租户模式,修正日志消息中的模块名称 |
| MULTI_TENANT_MODE.md | 详细说明小程序多租户模式的使用方法和注意事项 |
| MULTI_TENANT_MODE_IMPROVEMENT.md | 总览性文档,说明多租户模式改进的背景和使用指南 |
spring-boot-starters/wx-java-miniapp-multi-spring-boot-starter/MULTI_TENANT_MODE.md
Outdated
Show resolved
Hide resolved
|
|
||
| ## 相关链接 | ||
|
|
||
| - Issue: [#3909](https://github.com/binarywang/WxJava/issues/3909) |
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
文档中引用的 issue 编号不正确。根据 PR 描述,应该引用的是 issue #3835,而不是 #3909。请修正文档中的 issue 引用链接。
Co-authored-by: Copilot <[email protected]>
|
auggie review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /** | ||
| * 微信小程序 {@link WxMaMultiServices} 共享式实现. | ||
| * <p> | ||
| * 使用单个 WxMaService 实例管理多个租户配置,通过 switchoverTo 切换租户。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spring Boot Starter 的多租户实现为每个租户创建独立的 WxService 实例和 HTTP 客户端,而基础 SDK 类已支持通过
configMap管理多配置。这导致 100 个租户需要 100 个 HTTP 客户端(~500MB)。解决方案
新增可配置的多租户模式,默认保持现有行为:
实现
配置属性:
代码使用(两种模式 API 完全一致):
变更范围
新增类:
WxMaMultiServicesSharedImpl/WxMpMultiServicesSharedImpl新增配置:
MultiTenantMode枚举(ISOLATED, SHARED)multi-tenant-mode配置项重构:
AbstractWxMaConfiguration/AbstractWxMpConfiguration支持模式选择TreeMap保证默认租户选择的确定性支持模块
注意事项
共享模式依赖
ThreadLocal切换配置,异步场景需预先获取配置信息:向后兼容
完全兼容。未配置
multi-tenant-mode时使用 ISOLATED 模式,行为与旧版本一致。Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.