saksham agarwal
saksham agarwal

Reputation: 260

Can we run Spring REST API on different ports?

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.

Upvotes: 2

Views: 10929

Answers (8)

Sam
Sam

Reputation: 2049

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

Upvotes: 0

The Bitman
The Bitman

Reputation: 1329

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

Upvotes: 1

Vasanth Kumar
Vasanth Kumar

Reputation: 371

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

Upvotes: 0

Usman Ali
Usman Ali

Reputation: 58

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

Upvotes: 0

Nenad Vichentikj
Nenad Vichentikj

Reputation: 111

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.

Upvotes: 0

cassiomolin
cassiomolin

Reputation: 130947

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.

Upvotes: 2

Jignesh M. Khatri
Jignesh M. Khatri

Reputation: 1606

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.

Upvotes: 0

Vishnu
Vishnu

Reputation: 89

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

Upvotes: -1

Related Questions