Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Spring Boot with two ports

Tags:

I'm trying configure an application in Spring Boot with two differents ports, but I haven't got still. My first aproximation has been with two controllers and I have defined a @Bean inside the two controller with container.setPort(8080); And my second aproximation has been add the actuator dependency and change the port of the managament, but my application don't run. "Address already in use: bind", How can I confiure an application with two ports? I want one port for admin and the other port is for consults of my api.

like image 288
nole Avatar asked Apr 01 '16 13:04

nole


People also ask

How do I run a spring boot program on two different ports?

We can change the port of the Spring Boot in the following ways: By Adding the configuration in the application properties of the Spring Boot project. By Implementing the WebServerFactoryCustomizer interface in the component class. Changing the configuration of VM options.

How do I enable http and https in spring boot?

To enable support for HTTP and HTTPS in Spring Boot 2, we need to register an additional connector with Spring Boot application. First, enable SSL/HTTPS for Spring Boot, for example by following the HTTPS using Self-Signed Certificate in Spring Boot tutorial. Now, add server. http.


1 Answers

As is has been mentioned before, server.port and management.port along with management.context-path properties could be set to make the embedded container to listen on different ports (management-related properties to access Actuator endpoints).

To listen on ports other than server.port and management.port:

@Configuration public class EmbeddedTomcatConfiguration {      @Value("${server.additionalPorts}")     private String additionalPorts;      @Bean     public EmbeddedServletContainerFactory servletContainer() {         TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();         Connector[] additionalConnectors = this.additionalConnector();         if (additionalConnectors != null && additionalConnectors.length > 0) {             tomcat.addAdditionalTomcatConnectors(additionalConnectors);         }         return tomcat;     }      private Connector[] additionalConnector() {         if (StringUtils.isBlank(this.additionalPorts)) {             return null;         }         String[] ports = this.additionalPorts.split(",");         List<Connector> result = new ArrayList<>();         for (String port : ports) {             Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");             connector.setScheme("http");             connector.setPort(Integer.valueOf(port));             result.add(connector);         }         return result.toArray(new Connector[] {});     } } 

application.yml

server:   port: ${appPort:8800}   additionalPorts: 8881,8882 

Application.java

@SpringBootApplication @ComponentScan(...) @Import(EmbeddedTomcatConfiguration.class) public Application {      public static void main(String[] args) {         SpringApplication.run(Application .class, args);     } } 

I recently blogged about this topic at http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html

like image 133
ootero Avatar answered Sep 19 '22 11:09

ootero