tomcat上的多租户

时间:2014-11-17 07:02:59

标签: java json tomcat

我设置了在Tomcat上公开和实现的JSON API。 我想使用以下URL方法在Tomcat上实现这些API的多租户:

companyname1.domain.com/api/getUsers...
companyname2.domain.com/api/getUsers...
companyname3.domain.com/api/getUsers...

让我知道是否有使用上下文或其他机制实现它的最佳实践。我不想为每个公司创建一个单独的Tomcat实例。 此外,还有办法在公司注册后动态创建它。

提前谢谢你, 摩西

4 个答案:

答案 0 :(得分:1)

这可以使用为Tomcat webapp提供不同参数的多个反向代理来完成。最简单的设置(使用Apache HTTP和mod_proxy_ajp)可能是为了保留原始请求的主机并在Web应用程序内解析它。

<VirtualHost *:80>
    ServerName companyname1.domain.com

    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
    ProxyPreserveHost On
</VirtualHost>

<VirtualHost *:80>
    ServerName companyname2.domain.com

    ProxyPass /api ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
    ProxyPreserveHost On
</VirtualHost>

<VirtualHost *:80>
    ServerName companyname3.domain.com

    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
    ProxyPreserveHost On
</VirtualHost>

答案 1 :(得分:1)

在您的Tomcats server.xml 中创建多个virtual hosts,以便收听您的域名。这些应指向不同的webapps目录,在 ROOT 目录中托管您的特定应用程序:

<Host name="localhost" appBase="domain1-webapps" autoDeploy="true" unpackWARs="true"></Host>
<Host name="companyname1.domain.com" appBase="domain1-webapps" autoDeploy="true" unpackWARs="true"></Host>
<Host name="companyname2.domain.com" appBase="domain2-webapps" autoDeploy="true" unpackWARs="true"></Host>
...

答案 2 :(得分:0)

I can not comment because of my reputation is below than 50.

if URI is same like 
companyname1.domain.com/api/getUsers
companyname2.domain.com/api/getUsers
companyname3.domain.com/api/getUsers

then Apache web server found domain companyname1.domain.com and from httpd file, it send call to tomcat application server through AJP connector. But problem is that how can it found which war file to run. 

因此URL中需要应用程序名称

companyname1.domain.com/abc/api/getUsers
companyname2.domain.com/xyz/api/getUsers
companyname3.domain.com/fgf/api/getUsers

答案 3 :(得分:0)

We can use server context setting. like as per tomcat server specification. 
The Host element represents a virtual host, which is an association of a network name for a server (such as "www.mycompany.com") with the particular server on which Tomcat is running. For clients to be able to connect to a Tomcat server using its network name, this name must be registered in the Domain Name Service (DNS) server that manages the Internet domain you belong to - contact your Network Administrator for more information.

In many cases, System Administrators wish to associate more than one network name (such as www.mycompany.com and company.com) with the same virtual host and applications. This can be accomplished using the Host Name Aliases feature discussed below.

One or more Host elements are nested inside an Engine element. Inside the Host element, you can nest Context elements for the web applications associated with this virtual host. Exactly one of the Hosts associated with each Engine MUST have a name matching the defaultHost attribute of that Engine.

Clients normally use host names to identify the server they wish to connect to. This host name is also included in the HTTP request headers. Tomcat extracts the host name from the HTTP headers and looks for a Host with a matching name. If no match is found, the request is routed to the default host. The name of the default host does not have to match a DNS name (although it can) since any request where the DNS name does not match the name of a Host element will be routed to the default host.

深入了解详情。通过这个链接。 :https://tomcat.apache.org/tomcat-8.0-doc/config/host.html