2

I have a spring boot application with two services but i need to run one on port 8080 and the other on 8081. Now i'm developing with sts (Spring tool suite) and i run the application with the option "Run as spring boot app", so i don't know where change server configurations. Someone can help me?

  • 1
    Check this duplicated [post](https://stackoverflow.com/questions/21083170/how-to-configure-port-for-a-spring-boot-application) You can do it via application.properties – pbuzulan Mar 07 '19 at 10:18
  • 1
    I've seen that but i need to put one service in 8080 and another in 8081 with the same application – Alessio Pascucci Mar 07 '19 at 10:20
  • 1
    Possible duplicate of [How to configure port for a Spring Boot application](https://stackoverflow.com/questions/21083170/how-to-configure-port-for-a-spring-boot-application) – Mickael Mar 07 '19 at 10:21
  • 1
    It is discussed here: https://stackoverflow.com/questions/36357135/configure-spring-boot-with-two-ports/69190413 – Karmakulov Kirill Sep 15 '21 at 09:34

6 Answers6

1

You can't run two different services under the same spring boot application in two different ports. If you want you can move one service to another spring boot application. But port number will not be same for both services.

Pearl
  • 364
  • 1
  • 6
  • 14
0

The port can be defined on the application.properties or the application.yaml configuration file that you use.

In application.properties file, define it as below:

server.port=8090

Or in case you are using a application.yaml config, define it as below:

server:
    port: 8090
Madhu Bhat
  • 11,063
  • 1
  • 29
  • 49
  • So, if i need to access two different ports i need to run the application on two server placed on two different ports? – Alessio Pascucci Mar 07 '19 at 10:40
  • 1
    @AlessioPascucci in case you must have two services on different ports, you need to run it as two different applications, each with it's own different port. – Madhu Bhat Mar 07 '19 at 11:05
0

you can configure SprintBoot support two ports.The common method configure is use application.properties or the application.yaml as @Madhu Bhat answer. In SprintBoot to configure another port code like this:

create a connector

        int port = Integer.parseInt(probePort);
        Connector httpConnector = new Connector(HTTP_PROTOCOL);
        httpConnector.setPort(port);
        Http11NioProtocol handler = (Http11NioProtocol) httpConnector.getProtocolHandler();
        handler.setMaxThreads(10);
        handler.setMinSpareThreads(4);
        //handler.setAddress(InetAddress.getLocalHost());
        handler.setAddress(StringTool.getInetAddress());
        return httpConnector;

configure connector to

((TomcatEmbeddedServletContainerFactory) container).addAdditionalTomcatConnectors(connector);
TongChen
  • 1,326
  • 1
  • 9
  • 18
0

If you use Docker (most common solution), you can add the port or its full address as an Environment Variable

docker-compose.yml file like this:

application1:
    image: 'application1:latest'
    build:
      context: ./
    container_name: application
    environment:
      - HOST-APP2=localhost:8082
    ports:
      - 8091:8080

application2:
    image: 'application2:latest'
    build:
      context: ./
    container_name: application
    environment:
      - HOST-APP1=localhost:8081
    ports:
      - 8092:8080

or straight in the Dockerfile while building containers

check out here : https://vsupalov.com/docker-arg-env-variable-guide/ it's a good article

-1

you can write below line in application.properties or application.yml

server.port=8080

vivekdubey
  • 454
  • 2
  • 7
-1

Sure. You can do it in the application.properties file of spring boot project via setting server.port=number for each service.

Emin Javadov
  • 144
  • 1
  • 8