2

We have a single application which is used by multiple customers. It's every time the same app, but with a different database.

Our current setup consists of:

  • Apache 2 Web Server
  • Tomcat 8
  • Spring 4.1

The Web Server routes specific URLs to Tomcat, i.e.

  • /customer1 -> tomcat/customer1
  • /customer2 -> tomcat/customer2

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.

Currently one application runs at /webapps/customer1 and another instance at /webapps/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.

Is this possible to achieve?

Benjamin M
  • 23,599
  • 32
  • 121
  • 201

1 Answers1

2

If I understood you correctly, you want single application that uses 2 (or more) databases, depending on the customer. Customer is determined via 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.

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/

Krešimir Nesek
  • 5,302
  • 4
  • 29
  • 56
  • Thank you. That's helpful in terms of Spring. But: How to get the app running on Tomcat, so that multiple Routes point to the application? By default Tomcat routes `customer1.war` to `/customer1` and `customer2.war` to `/customer2`. But I need `myApp.war` to `/customer1` and `/customer2` (Or maybe `/*`). – Benjamin M Jan 31 '15 at 16:08
  • For that I would suggest deploying the app as tomcat's root. Then all requests will go to the app and you can manage `/customer1` and `/customer2` at app level. For this config take a look at http://stackoverflow.com/questions/5328518/deploying-my-application-at-the-root-in-tomcat . Otherwise, you could setup reverse proxy (for example in Apache) to map those URLs to tomcat (which I believe you're already doing). – Krešimir Nesek Jan 31 '15 at 16:35