带有多租户的Spring Batch [英] Spring Batch with multi tenancy

查看:176
本文介绍了带有多租户的Spring Batch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何定义春季批处理作业以针对多个租户运行?

How do we define spring batch jobs to run against multiple tenants?

我已设置为每天晚上针对当前一个数据库模式按顺序运行一系列作业.当前所有作业都从某个位置读取文件并插入数据库.批处理配置非常基本,其中定义了数据源,事务管理器并将其映射到作业存储库.我的工作将指向此存储库和事务管理器.另外,我目前正在将批处理元数据信息保留在数据库中.

I have setup to run a sequence of jobs in order every night against one database schema currently. All the jobs currently read files from a location and insert to database.The batch configuration was very basic where I defined a data-source, transaction manager and mapped job-repository to it. My jobs will point to this repository and transaction manager. Also I am currently persisting batch meta data information in database.

我的新要求是能够对多个租户运行相同的作业(按顺序执行).每个租户数据可以存在于同一数据库服务器中,但是可以位于不同的架构甚至不同的数据库服务器中.我的问题是

My new requirement is to able to run the same jobs (executed in order) against multiple tenants. Each tenants data can live in same database server but different schema or even different database servers. My questions are

1)我们是否将所有租户的批处理特定的元数据信息存储在一个公共数据库中,或者每个租户数据库都应有自己的批处理元数据信息?

1) Do we store the batch specific metadata information for all the tenants in one common database or each tenant database should have its own?

2)我的理解是,我们需要每个租户一个数据源,以便特定于该租户的作业将有权访问数据库以存储从文件读取的数据.在为该租户执行作业时,spring batch批处理存储库是否也应指向当前数据源?

2) My understanding is that we need a data-source per tenant so that jobs specific to this tenant will have access to database to store data read from files. Does spring batch repository should also point to current data-source when executing jobs for that tenant?

3)我们计划并行启动所有租户[职位],这意味着所有租户都可以同时运行JOB1.目前,我仍然不确定当这些租户运行且每个租户关联到不同的数据源时如何管理作业存储库,数据源,事务管理.

3) We are planning to start all tenants [jobs] paralleled meaning JOB1 can be running at the same time for the all the tenants. At this time I am still not sure how to manage job-repository , data-source, transaction manage when these tenants are running with each associated to a different data-source.

4)首先,我想做的就是为每个租户复制我现有的配置,并用自己的工作库指向租户特定的数据源和事务管理器.如果没有其他方法可以动态地定义相同的对象而没有重复,这将是我要做的最后一件事.

4) At the top of my head all I am thinking is to duplicate my existing configuration for each tenant with own job-repositoyy pointing to tenant specific data-source and transaction manager. This is the last thing I would implement if there is no other way to define the same dynamically with out duplicating.

如果有任何机构解决或对解决方法有任何想法,请分享.一个示例配置应该会有所帮助.

If any body has solved or has any ideas on how to approach to a solution please share. A sample config should help.

推荐答案

我参与了构建SaaS应用程序的工作,在该应用程序中您需要执行类似的操作,但并不完全使用Spring Batch.

I was involved in building a SaaS application where you needed to do something similar but not exactly using Spring Batch.

您的主要想法是:

a.定义一个主数据库,您将在其中存储所有配置特定的数据,假设您有一个表,该表映射了您的租户名称,信息和数据源配置.

a. Define a master database where you would store all the configuration specific data say suppose you have a table which maps your tenant name, information and datasource configuration.

b.启动您的应用程序并读取此数据源,并在服务器端维护本地缓存,并以key作为您的租户名称,并使用值作为租户信息(数据源等)

b. Start your application and read this data source and maintain a local cache at your server end with key as your tenant name and value as the tenant information (data source etc.)

c.与您一起维护本地线程,例如:

c. Maintain a thread local with you, example:

public class TenantThreadLocalContext
{
    public static final ThreadLocal<TenantInformation> threadLocal = new ThreadLocal<TenantInformation>();

    public static void set(TenantInformation tenantInformation)
    {
        threadLocal.set(tenantInformation);
    }

    public static void unset()
    {
        threadLocal.remove();
    }

    public static TenantInformation get()
    {
        return threadLocal.get();
    }

}

d.每当您启动任何线程来开始处理(批处理)时,请将该本地线程与租户信息一起设置,以使每个线程都知道该线程与哪个租户相关联.

d. Whenever you are starting any thread to begin your processing (batch processing)set this thread local with the tenant information so that each of this thread knows that it is associated to which tenant.

e.最终,在数据库处理时,您可以看到线程具有什么数据源,并且可以使用该数据源建立连接.

e. Finally at the time of database processing you could see that the thread has what data source and you could use this data source to make a connection.

如果您正在使用Hibernate,那么您很幸运,因为Hibernate 4已为您完​​成了所有这一切.请参阅:.如果您需要有关休眠配置等方面的帮助,那么也许我也可以为您提供帮助.

If you are using Hibernate then you are lucky as Hibernate 4 has done all this for you. Refer: this. If you need help in hibernate configurations, etc, then probably I could help you out there as well.

这篇关于带有多租户的Spring Batch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆