Skip to content

CP_如何动态支持多个企业号 #2594

Description

@tang226

简要描述

在 CP_如何支持多个企业号的wiki中CP的基本配置信息(secret,token,aesKey)都是配置在配置文件中的,然后启动服务的时候直接生成了多个WxCpService然后存储在map中

@PostConstruct
    public void initServices() {
        cpServices = this.properties.getAppConfigs().stream().map(a -> {
            val configStorage = new WxCpDefaultConfigImpl();
            configStorage.setCorpId(this.properties.getCorpId());
            configStorage.setAgentId(a.getAgentId());
            configStorage.setCorpSecret(a.getSecret());
            configStorage.setToken(a.getToken());
            configStorage.setAesKey(a.getAesKey());
            val service = new WxCpServiceImpl();
            service.setWxCpConfigStorage(configStorage);
            routers.put(a.getAgentId(), this.newRouter(service));
            return service;
        }).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), a -> a));
    }

问题

我的服务中有很多个CP的配置信息(100个)并且都存在数据库中,我感觉在项目启动的时候生成100个WxCpService非常不妥,并且这个配置信息还会动态递增(每接入一个CP用户,我就要在数据库中新增一条配置信息),请问有较好的方式解决这个问题吗。

就比如说我需要调用某个CP用户的接口,就根据数据库里面的配置信息为它生成一个WxCpService,主要的步骤就是每次都需要调用setWxCpConfigStorage把数据库存储的配置注入进去,但是setWxCpConfigStorage方法中有一个initHttp(),频繁的调用initHttp()会有性能影响吗

Activity

  1. didi12121 commented on Apr 18, 2022

    @didi12121

    我是写了一个根据租户id来生成不同的cpServices 的组件然后注入到spring
    image
    然后每次需要cpServices 的时候都通过这个方法拿到对应的cpServices

  2. tang226 commented on Apr 19, 2022

    @tang226
    Author

    这样感觉可以;
    请问每次去获取会很耗时吗

  3. didi12121 commented on Apr 19, 2022

    @didi12121

    目前看来不会

  4. stale commented on Jun 18, 2022

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  5. izerui commented on Jun 23, 2022

    @izerui

    已经实现: #2694

  6. nadirvishun commented on Jul 2, 2022

    @nadirvishun

    可以参考本项目中微信公众号的解决方式,用threadlocal来解决,demo,唯一麻烦点的是本身企业微信就是多个应用的,但如果再加上不同的企业的话话,相当于agentId + corpId才能确定唯一的应用。

  7. stale commented on Aug 31, 2022

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions