1

I have a Spring application with two controllers. I want to run one controller on localhost:8080 and second controller on localhost:8081.

Am I able to configure Tomcat to serve two ports simultaneously i.e 8080 and 8081? Is it possible? How?

Please note that it is not a Spring Boot application.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
saksham agarwal
  • 250
  • 3
  • 14

8 Answers8

2

It sounds like two completely different applications.

You certainly could configure your Tomcat's server.xml file to have multiple HTTP connectors running on different ports. But you'll find it much easier and hassle-free to deal with two different Tomcat instances.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • I appreciate your answer of having different application but my use case is to run controller of same application over different ports because of specific business needs. Is it possible? – saksham agarwal Oct 04 '18 at 09:05
  • @sakshamagarwal There might be some _obscure workaround_ to achieve it. But I'm not aware of and wouldn't even recommend it. – cassiomolin Oct 04 '18 at 09:26
  • Any idea of how can I make an API as a private API and no-one can access that particular api/controller from outside world? Provided that all API are public already. – saksham agarwal Oct 04 '18 at 09:52
  • @sakshamagarwal It could be handled by a reverse proxy, restricting the access to that particular endpoint. – cassiomolin Oct 04 '18 at 10:24
1

The App Server (Tomcat, JBoss,Glassfish) run on / watch one port. You can run multiple app servers on a single node (computer) with different port numbers for this reason. They could be the same (Tomcat+Tomcat) or different ones as well (Tomcat+Glassfish)

But in this case you need to split the controllers into 2 different applications and deploy them on the app server instances.

This is the MicroServices architectural desing style. When you run a separate app server for every service. Microservices services most of the cases use REST over HTTP to communicate to each other.

But in case of Tomcat (maybe not by all of the products) it is possible : Running Tomcat server on two different ports

The Bitman
  • 1,279
  • 1
  • 11
  • 25
0

Spring itself doesnot run on any port. It is just a technology to create APIs. Port binds with server (like Tomcat, JBoss, etc). So if you want to use different ports for different controllers, then you need to deploy multiple applications across multiple servers and make those servers listen different ports.

Jignesh M. Khatri
  • 1,407
  • 1
  • 14
  • 22
0

On the application that should be on 8081, in the application.properties file add the following line:

  • server.port=8081

Then Just run both of them...

Otherwise in the TomcatConfiguration set the port to 8081, and again run both of them.

Nenad Vichentikj
  • 111
  • 2
  • 10
0

You can find the perfect example in below link. They use different port for different resources. It uses port binding with embedded tomcat in spring boot. Hope this helps you.

https://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html

Usman Ali
  • 58
  • 4
0

Yes, you can, but they will behave like two separate applications and are independent of each other. However they can share common resources like databases, Password directories etc. However for a use case such as this I would recommend to look into microservices. Read more about microservices here

Vasanth Kumar
  • 311
  • 1
  • 3
  • 9
0

One approach is to create additional org.apache.catalina.connector.Connector and route requests from it with org.springframework.web.servlet.mvc.condition.RequestCondition https://stackoverflow.com/a/69397870/6166627

Sam
  • 1,832
  • 19
  • 35
-1

No. spring runs on a specific port and that will be port for both rest controllers . You can have different URLS for them though.

Vishnu
  • 89
  • 1
  • 7
  • https://www.tecmint.com/run-deploy-multiple-web-applications-instances-tomcat-centos-ubuntu/ says that we can run tomcat on two ports simultaneously, then why we cannot run two controller on those ports? – saksham agarwal Oct 04 '18 at 07:57
  • That is like running two tomcat copies on different ports. I did not say that is not possible. You can deploy your wars in two different tomcats running on different ports but that was not originally asked by the questioner. – Vishnu Oct 04 '18 at 10:27