Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple Java webapps with overlapping paths?

I have a large collection of different independent (stateless) web services written in Java and compiled as WAR files. I want to deploy them to a single web application server.

If the URIs handled by the services in each WAR file began with a prefix I could use as a web app name, then this would be easy. I could, for instance, have

SALES WAR FILE: contains code for the following:
GET http://example.com/sales/widgets
POST http://example.com/sales/widgets
GET http://example.com/sales/sky-hooks

MARKETING WAR FILE: contains code for the following:
GET http://example.com/marketing/widgets
PUT http://example.com/marketing/sky-hooks

...in which case I would simply deploy two WAR files under the names "sales" and "marketing". However, I am not that fortunate. Instead, the URI paths handled by the components overlap. Something like this:

SALES WAR FILE: contains code for the following:
GET http://example.com/widgets/sales
POST http://example.com/widgets/sales
GET http://example.com/sky-hooks/sales

MARKETING WAR FILE: contains code for the following:
GET http://example.com/widgets/marketing
PUT http://example.com/sky-hooks/marketing

My question is how (if at all) I can deploy these on a single web application server.

I am open to suggestions that require a significant amount of work. For instance, my best-so-far idea is to build services that expect a component-name prefix before the regular URI path, then pipe all incoming traffic through a different server that knows what component each URI pattern falls into and modifies the URI to add that prefix. The difficulty with this approach is that tools like Swagger that read my source code will have a mistaken idea of what the URIs look like.

Any ideas?

like image 820
mcherm Avatar asked Mar 20 '26 09:03

mcherm


2 Answers

If you're willing to put apache in front of your web container, you can use apache's mod_proxy to forward request to the right place.

One way this could work, would be deploy the separate wars at separate prefixes as in your first case (sales and marketing) and then use ProxyPass to send the requests to the correct place:

ProxyPass /widget/sales http://example.com/sales/widget
ProxyPass /sky-hooks/sales http://example.com/sales/sky-hooks

ProxyPass /widget/marketing http://example.com/marketing/widget
ProxyPass /sky-hooks/marketing http://example.com/marketing/sky-hooks

Its probably a better idea to just refactor your routing though - it might be hard to maintain.

(EDIT: I originally suggested mod_rewrite, but I wanted to make my answer more specific, and it looks like this could be done purely with proxying)

like image 63
nont Avatar answered Mar 22 '26 21:03

nont


If I understand your question correctly, one of the solutions would be (I am assuming Tomcat is used but this should apply to most of the modern servlet containers):

1) Deploy your sales and marketing wars with different prefixes. I.e., using your example, they should be able to serve the following urls:

GET http://example.com/sales/widgets/sales
POST http://example.com/sales/widgets/sales 
GET http://example.com/sales/sky-hooks/sales

GET http://example.com/marketing/widgets/marketing
PUT http://example.com/marketing/sky-hooks/marketing

2) Use UrlRewriteFilter to craft lightweight web application that will be deployed to your servlet container root prefix (for Tomcat it is called ROOT.war) and will rewrite urls in incoming requests to point to relevant web application.

In other words, incoming request like:

/widgets/sales

will be transformed to:

/sales/widgets/sales

... and delivered to sales webapp.

Similarly, in response urls like:

/sales/widgets/sales

will be rewritten to:

/widgets/sales

3) Deploy this war to root of your servlet container.

This approach is somewhat similar to the one suggested by @nont but does not require apache as a frontend as the rewriting functionality will be handled by root web application (UrlRewriteFilter basically implements mod_rewrite functionality).

In other words you'll be able to deploy all your applications (including this rewrite application that is deployed to the root prefix) to single server alleviating need for extra intermediate proxy/rewrite servers.

like image 43
Eugene Loy Avatar answered Mar 22 '26 21:03

Eugene Loy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!