问题描述
我们只有一个应用程序,可供多个客户使用.每次都是相同的应用程序,但数据库不同.
We have a single application which is used by multiple customers. It's every time the same app, but with a different database.
我们当前的设置包括:
- Apache 2 Web服务器
- Tomcat 8
- Spring 4.1
Web服务器将特定的URL路由到Tomcat,即
The Web Server routes specific URLs to Tomcat, i.e.
- /customer1-> tomcat/customer1
- /customer2-> tomcat/customer2
现在,我们希望将这些条目保留在Apache Config中,但是以某种方式配置Tomcat和Spring.基本上,Tomcat应该接受每个请求(可能由regex定义-不必要),并将其路由到一个应用程序.
Now we'd like to keep those entries within Apache Config, but somehow configure Tomcat and Spring. Basically Tomcat should take every request (maybe defined by regex - not necessary) and route it to one application.
当前,一个应用程序在/webapps/customer1
上运行,另一个实例在/webapps/customer2
上运行.
Currently one application runs at /webapps/customer1
and another instance at /webapps/customer2
.
将来,该应用程序应该以某种方式在/webapps/*
上运行,因此在春季,我可以查看请求URL并选择 db_customer1
来访问/customer1/...
和 customer2
相同.
In the future the application should somehow run at /webapps/*
, so within spring I can have a look at the request URL and choose db_customer1
for requests at /customer1/...
and the same for customer2
.
这有可能实现吗?
推荐答案
如果我对您的理解正确,那么您希望使用一个应用程序使用2个(或更多)数据库,具体取决于客户.通过URL确定客户.
If I understood you correctly, you want single application that uses 2 (or more) databases, depending on the customer. Customer is determined via URL.
如果是这种情况,则应查看 AbstractRoutingDatasource
并创建您自己的该类的实现.它允许您在单个应用程序中使用不同的数据源.然后,您应该创建一个可以拦截URL的过滤器或拦截器,然后根据URL将路由数据源路由到适当的基础数据源.
If that is the case, you should take a look at AbstractRoutingDatasource
and create your own implementation of that class. It allows you to use different datasources in a single application. You should then create a Filter or maybe Interceptor that would intercept the URL and then, based on the URL, route the routing datasource to the appropriate underlying datasource.
看看下面链接的示例:它有一个客户路由数据源,该数据源对不同的登录用户使用不同的数据库: https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
Take a look at this example linked below: it has a customer routing datasource that uses different database for different logged in user:https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
这篇关于Spring + Tomcat基于请求URL的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!