简体   繁体   English

提供多个上下文的单个虚拟tomcat应用程序

[英]Single virtual tomcat application which serves multiple contexts

I have multiple clients: 我有多个客户:

  • client 1 - 40 users 客户端1-40个用户
  • client 2 - 50 users 客户端2-50个用户
  • client 3 - 60 users 客户端3-60个用户

And I have a web application that is supposed to serve all the clients. 而且我有一个应该为所有客户提供服务的Web应用程序。 The application is deployed into Tomcat. 该应用程序已部署到Tomcat中。 Each client has it's own database. 每个客户端都有自己的数据库。

What I want to implement is the single web application instance which servers all the clients. 我要实现的是为所有客户端提供服务的单个Web应用程序实例。 The client (and the database to connect to) is identified by the context path from the URL. 客户端(和要连接的数据库)由URL中的上下文路径标识。

Ie I imply the following scenario: 即我暗示以下情况:

  1. Some user requests the http://mydomain.com/client1/ 某些用户请求http://mydomain.com/client1/
  2. Tomcat invokes a single instance of my application (no matter which context is requested) Tomcat调用我的应用程序的单个实例(无论请求哪个上下文)
  3. My application processes the rest of the request thinking that it's deployed to /client1 context path, ie all redirect or relative URLs should be resolved against http://mydomain.com/client1/ 我的应用程序处理了其余的请求,认为已将其部署到/ client1上下文路径,即,应根据http://mydomain.com/client1/解析所有重定向或相对URL。

When the client 2 requests the http://mydomain.com/client2/ , I want my application (the same instance) now process it just like if it was deployed to /client2 context path. 当客户端2请求http://mydomain.com/client2/时 ,我希望我的应用程序(相同的实例)现在像处理部署到/ client2上下文路径一样对其进行处理。

Is this possible with Tomcat? Tomcat有可能吗?

Your application has to do this not tomcat. 您的应用程序必须这样做,而不是tomcat。 Now you could deploy your application in three new contexts (client1, client2, client3) with slightly different configuration for the database, and if you are careful to use relative URLs (ie don't do things like /images) then you can do this without changes. 现在,您可以在数据库配置略有不同的三个新上下文(client1,client2,client3)中部署应用程序,如果您谨慎使用相对URL(即,不要执行/ images之类的事情),则可以执行此操作没有变化。 This is the transparent way of making your application reusable in that your application is unaware of the global picture that you have 3 different instances of itself running. 这是使应用程序可重用的透明方法,因为您的应用程序不了解全局情况,即您自己正在运行3个不同的实例。 That means you can easily deploy more or more without having to change your application. 这意味着您可以轻松部署更多或更多,而无需更改应用程序。 You just configure a new instance and go. 您只需配置一个新实例即可。 This only requires you don't use absolute URLs to resources. 这仅要求您不对资源使用绝对URL。 Using ServletContext.getContextPath() and using .. in your CSS, scripts, etc is helpful as well here. 在这里,使用ServletContext.getContextPath()并在CSS,脚本等中使用..也很有帮助。

Probably one of the biggest advantages working this way is that your app doesn't care about global concerns. 以这种方式工作的最大优势之一可能是您的应用程序不关心全球性问题。 Because its not involved in those decisions you can run 3 instances on one tomcat server, or if one client needs more scaling they can be moved to their own tomcat server easily. 因为它不参与这些决策,所以您可以在一台tomcat服务器上运行3个实例,或者如果一个客户端需要更多扩展,则可以轻松地将它们移动到自己的tomcat服务器。 By making your app portable it has forced you to deal with how to install your app in any environment. 通过使您的应用程序具有便携性,它迫使您应对如何在任何环境中安装应用程序的问题。 This is a pillar of horizontal scaling which your situation could very much take advantage being you can split your DB data without having to rejoin them (huge advantage). 这是水平扩展的支柱,您的情况可能会充分利用您的优势,因为您可以拆分数据库数据而不必重新加入数据库(巨大优势)。 The option you asked about doesn't force you to deal with this so when the time comes to deal with it it will be painful. 您询问的选项不会强迫您处理此问题,因此,当需要处理该选项时,将很痛苦。

The other option is more involved and requires significant changes to your application to handle this. 另一个选项涉及更多,并且需要对您的应用程序进行重大更改才能处理此问题。 This is by parsing the incoming URL and pulling out the name of the client then using that name to look up in a configuration file for the database that should be used for that client. 这是通过解析传入的URL并拉出客户端的名称,然后使用该名称在应该用于该客户端的数据库的配置文件中查找的。 SpringMVC can handle things like extracting variables from URL paths. SpringMVC可以处理诸如从URL路径提取变量之类的事情。 Then making sure you render everything back to them so it points to their portion of the URL. 然后确保将所有内容都呈现给他们,使其指向URL的一部分。 This probably would require a lot of the same requirements as the first. 这可能需要很多与第一个相同的要求。 You can use absolute URLs for things like javascript, CSS, and images, but URLs to your app would have to be rewritten at runtime so that it is relative to the requesting client. 您可以将绝对URL用于javascript,CSS和图像之类的东西,但是应用程序的URL必须在运行时进行重写,以使其与发出请求的客户端有关。 The benefit is that your only load your application once. 好处是您只需加载一次应用程序。

Just as an aside, if you host your CSS, Javascript, images on a CDN in production then both of these options must be relative URL aware. 顺便说一句,如果将CSS,Javascript和图像托管在生产中的CDN上,则这两个选项都必须具有相对URL识别能力。 Upsides and downsides to using CDNs as well. 使用CDN的优缺点。

While that sounds good it might not be a good thing because all clients use the same version of the app. 虽然听起来不错,但这可能不是一件好事,因为所有客户端都使用相同版本的应用程序。 Also if you bring down a the app to fix client1 to do maintenance it affects all clients. 另外,如果您关闭某个应用程序以修复client1进行维护,则会影响所有客户端。 If you think you'll have to do customization per client then this option will get messy quick. 如果您认为必须针对每个客户端进行自定义,则此选项会很快变得混乱。 Upgrading a single client means all clients must upgrade and depending on your business model this might not be compatible. 升级单个客户端意味着所有客户端都必须升级,并且这可能与您的业务模型不兼容。 Furthermore, I'm not entirely sure you'll save a lot of memory either running only a single version of the application because most apps only take up 10MB of code loaded. 此外,我不太确定您会只运行一个版本的应用程序就节省大量内存,因为大多数应用程序仅占用10MB的代码加载量。 A vast majority of the memory is in the VM and processing requests, and using a single Tomcat instance means you share the VM. 绝大部分内存都在VM中并处理请求,使用单个Tomcat实例意味着您共享VM。 And with 1 or 3 instances running you still have the same number of requests. 并且在运行1或3个实例的情况下,您仍然拥有相同数量的请求。 You might see a difference of 30-100MBs which in todays world is chump change, and all of those other concerns aren't addresses if you choose to save only a couple of MB. 您可能会看到30到100 MB的差异,这在当今世界是笨拙的变化,而如果您选择仅保存几MB的话,那么其他所有问题都无法解决。

Essentially there are facilities in Tomcat to aid you in doing this (multiple contexts), but its mostly up to your application to handle this especially if its a single instance. 基本上,Tomcat中提供了一些工具来帮助您执行此操作(多个上下文),但是主要取决于您的应用程序来处理此问题,尤其是在单个实例的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM