Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 9, 2026

Spring Boot Starter 的多租户实现为每个租户创建独立的 WxService 实例和 HTTP 客户端,而基础 SDK 类已支持通过 configMap 管理多配置。这导致 100 个租户需要 100 个 HTTP 客户端(~500MB)。

解决方案

新增可配置的多租户模式,默认保持现有行为:

  • ISOLATED(默认):每租户独立实例,线程安全,适合异步场景
  • SHARED(新增):单实例 + configMap,共享 HTTP 客户端,节省 ~90% 内存

实现

配置属性

wx:
  ma:  # 或 mp
    apps:
      tenant1:
        app-id: wx123
        app-secret: secret1
      tenant2:
        app-id: wx456
        app-secret: secret2
    config-storage:
      multi-tenant-mode: shared  # isolated | shared

代码使用(两种模式 API 完全一致):

@Autowired
private WxMaMultiServices services;

WxMaService wxService = services.getWxMaService(tenantId);
wxService.getUserService().getUserInfo(...);

变更范围

新增类

  • WxMaMultiServicesSharedImpl / WxMpMultiServicesSharedImpl

新增配置

  • MultiTenantMode 枚举(ISOLATED, SHARED)
  • multi-tenant-mode 配置项

重构

  • AbstractWxMaConfiguration / AbstractWxMpConfiguration 支持模式选择
  • 使用 TreeMap 保证默认租户选择的确定性

支持模块

  • ✅ 小程序(MiniApp)
  • ✅ 公众号(MP)

注意事项

共享模式依赖 ThreadLocal 切换配置,异步场景需预先获取配置信息:

// ❌ 错误
CompletableFuture.runAsync(() -> wxService.getAccessToken());

// ✅ 正确
String token = wxService.getAccessToken();
CompletableFuture.runAsync(() -> process(token));

向后兼容

完全兼容。未配置 multi-tenant-mode 时使用 ISOLATED 模式,行为与旧版本一致。

Original prompt

This section details on the original issue you should resolve

<issue_title>关于源码的请教</issue_title>
<issue_description>Image

Image Image Image Image

基础Wx实现类中已经有configMap了,可以用configMap来存储不同的小程序配置。不同的配置,都是复用同一个http客户端。为什么在各个spring-boot-starter中又单独创建类来存储不同的配置?从spring的配置来看,http客户端只有一个,不同小程序配置可以实现多租户,所以似乎没必要单独再建新类存放?重复创建,增加了http客户端的成本?直接使用Wx实现类中已经有configMap不是更好吗?是有什么我没有考虑到的bug吗?

ps:因为我的项目要引入该开源项目,但提供的标准starter不能完全满足我们的要求。我在改造的时候看到有这个写法,所以有疑惑,希望有大佬看见了,可以解答下。我更倾向直接服用Wx实现类中已经有configMap
</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix issue with Spring Boot starter configuration 添加多租户共享模式以优化资源使用 Jan 9, 2026
Copilot AI requested a review from binarywang January 9, 2026 06:38
@binarywang binarywang marked this pull request as ready for review January 9, 2026 07:12
Copilot AI review requested due to automatic review settings January 9, 2026 07:12
@binarywang
Copy link
Owner

auggie review

@augmentcode
Copy link

augmentcode bot commented Jan 9, 2026

🤖 Augment PR Summary

Summary: 本 PR 为 WxJava 的 MiniApp/MP 多租户 Spring Boot Starter 新增可配置的“共享模式”,在大量租户场景下复用 SDK 的 configMap 来减少 HTTP 客户端与内存占用。

Changes:

  • 新增多租户模式枚举 MultiTenantModeISOLATED 默认 / SHARED 新增),并通过 config-storage.multi-tenant-mode 暴露配置项
  • 新增 WxMaMultiServicesSharedImpl / WxMpMultiServicesSharedImpl:单个 Service 实例 + 多配置(ThreadLocal 切换),共享同一个 HTTP 客户端
  • 重构 AbstractWxMaConfiguration / AbstractWxMpConfiguration:按模式构建隔离式或共享式的 MultiServices
  • 初始化共享模式时批量注入多配置,并用 TreeMap 选择默认租户以保证确定性
  • 补充多租户模式说明文档与迁移/注意事项(特别强调共享模式下异步 ThreadLocal 的限制)

Technical Notes: 共享模式依赖 ThreadLocal 进行租户切换,同一线程内务必在每次调用前完成切换;异步/线程池场景需显式传递必要信息以避免错用租户配置。

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

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

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.


## 问题背景

用户在 issue #3909 中提出了一个架构设计问题:
Copy link

Choose a reason for hiding this comment

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

这里引用的 issue #3909 在仓库中不存在,而本 PR 关联的是 #3835;建议修正编号/链接以免读者困惑(同样问题也出现在本 PR 新增的其他多租户说明文档中)。

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

return null;
}
// 切换到指定租户的配置
return sharedWxMaService.switchoverTo(tenantId);
Copy link

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

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

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

Review completed. 3 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

return null;
}
// 切换到指定租户的配置
return sharedWxMaService.switchoverTo(tenantId);
Copy link

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

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎


## 问题背景

用户在 issue #3909 中提出了一个架构设计问题:
Copy link

Choose a reason for hiding this comment

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

文档这里引用 issue #3909,但本 PR 描述里标注的是 Fixes #3835;两处不一致会影响读者追溯问题来源(同样出现在 miniapp 的 MULTI_TENANT_MODE.md)。建议统一为实际关联的 issue 编号/链接。

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

## 相关链接

- Issue: [#3909](https://github.com/binarywang/WxJava/issues/3909)
- Pull Request: [待填写]
Copy link

Choose a reason for hiding this comment

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

这里仍保留 Pull Request: [待填写] 占位符,合入后会留下不完整的参考链接。建议补齐对应 PR 链接或移除该条目。

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

Copy link

Copilot AI left a 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 总览性文档,说明多租户模式改进的背景和使用指南


## 相关链接

- Issue: [#3909](https://github.com/binarywang/WxJava/issues/3909)
Copy link

Copilot AI Jan 9, 2026

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 引用链接。

Copilot uses AI. Check for mistakes.
@binarywang
Copy link
Owner

auggie review

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

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

Review completed. 1 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

/**
* 微信小程序 {@link WxMaMultiServices} 共享式实现.
* <p>
* 使用单个 WxMaService 实例管理多个租户配置,通过 switchoverTo 切换租户。
Copy link

Choose a reason for hiding this comment

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

这里的类注释写的是“通过 switchoverTo 切换租户”,但实际实现使用的是 switchover(tenantId)(返回 boolean);建议同步更新注释避免误导(同样适用于 WxMpMultiServicesSharedImpl)。

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

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.

关于源码的请教

2 participants