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?
-
1Check 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
-
1I'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
-
1Possible 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
-
1It is discussed here: https://stackoverflow.com/questions/36357135/configure-spring-boot-with-two-ports/69190413 – Karmakulov Kirill Sep 15 '21 at 09:34
8 Answers
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.

- 384
- 1
- 8
- 15
-
1from this [baeldung article](https://www.baeldung.com/spring-boot-context-hierarchy), it seems to be possible to do, using SpringApplicationBuilder API. I have added it as answer also. – Manohar Bhat Mar 22 '23 at 20:21
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);

- 1,414
- 1
- 11
- 21
-
1But this need to run two different application onto two different ports? – Alessio Pascucci Mar 07 '19 at 14:42
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

- 326
- 4
- 15
I have not tried this, but from this baeldung article, it seems to be possible using SpringApplicationBuilder API.
In summary, following are the high level steps
- Create 2 properties file with different ports. Refer github repo from article.
- Create 2 separate configurations by injecting these properties file.
- Use SpringApplicationBuilder to create parent, child or sibling context hierarchy.

- 95
- 7
You can create two different child application context's in single application and run two different ports with different services on those ports. Reference : https://www.baeldung.com/spring-boot-context-hierarchy
Following is a sample I used in one of my application.
Main Class of parent context.
package com.kst.gateway;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import com.kst.gateway.inbound.InboundConfig;
import com.kst.gateway.outbound.OutboundConfig;
//@formatter:off
public class GatewayApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.parent(GatewayConfig.class)
.web(WebApplicationType.NONE)
.child(OutboundConfig.class)
.web(WebApplicationType.REACTIVE)
.sibling(InboundConfig.class)
.web(WebApplicationType.REACTIVE)
.run(args);
}
}
Child 1 Configuration :
package com.kst.gateway.inbound;
@Configuration
@ComponentScan("com.kst.gateway.inbound")
@PropertySource("classpath:inbound.properties")
@EnableAutoConfiguration
public class InboundConfig {
@Autowired
public GatewayConfig config;
@Value("${server.port}")
public Integer serverPort;
@Bean(name = "issuer_routes")
public RouteLocator routes(RouteLocatorBuilder routeLocatorBuilder) {
String issuerUri = config.switchDestinationScheme+"://"+config.switchDestinationIp+":"+config.switchDestinationPort;
return routeLocatorBuilder.routes()
.route("issuer_request", routeSpec -> routeSpec
.remoteAddr(config.npciSourceIps.toArray(new String[0]))
.and().path("/Req**")
.uri(issuerUri)
).route("acquirer_response", routeSpec -> routeSpec
.remoteAddr(config.npciSourceIps.toArray(new String[0]))
.and().path("/Resp**")
.uri(issuerUri)
).build();
}
}
Child 2 configuration :
package com.kst.gateway.outbound;
@Configuration
@ComponentScan("com.kst.gateway.outbound")
@PropertySource("classpath:outbound.properties")
@EnableAutoConfiguration
public class OutboundConfig {
@Autowired
public GatewayConfig config;
@Autowired
public OutboundRequestUrlFilter acqReqUrlFilter;
@Autowired
public OutboundResponseUrlFilter acqRespUrlFilter;
@Value("${server.port}")
public Integer serverPort;
@Bean(name = "acquirer_routes")
public RouteLocator routes(RouteLocatorBuilder routeLocatorBuilder, NpciEndpointProvider npciEndpointProvider) {
return routeLocatorBuilder.routes()
.route("acquirer_request", routeSpec -> routeSpec
.remoteAddr(config.switchSourceIps.toArray(new String[0]))
.and().path("/Req**")
.filters(fs -> fs.filter(acqReqUrlFilter.apply(npciEndpointProvider)))
.uri("no:/op")
).route("issuer_response", routeSpec -> routeSpec
.remoteAddr(config.switchSourceIps.toArray(new String[0]))
.and().path("/Resp**")
.filters(fs -> fs.filter(acqRespUrlFilter.apply(npciEndpointProvider)))
.uri("no:/op")
)
.build();
}
}
Parent application.properties which will be inherited by both the child context's
spring.application.name=gateway
server.ssl.key-store=classpath:ssl.pfx
server.ssl.key-store-password=Ippb@2023
server.ssl.key-store-type=pkcs12
server.ssl.key-alias=ssl
spring.cloud.gateway.httpclient.ssl.use-insecure-trust-manager=true
spring.cloud.service-registry.auto-registration.enabled=false
spring.cloud.discovery.enabled=false
spring.cloud.config.discovery.enabled=false
Child 1 inbound.properties file
spring.application.name=inbound
server.port=8443
Child 2 outbound.properties file
spring.application.name=outbound
server.port=443

- 425
- 4
- 14
-
Please add code and data as text ([using code formatting](/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – Suraj Rao Apr 08 '23 at 07:54
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

- 13,559
- 2
- 38
- 54
-
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
you can write below line in application.properties or application.yml
server.port=8080

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

- 140
- 1
- 8